🔐 A small CLI app that allows you to easily add multiple GIT accounts on one machine. It switches between accounts automatically based on the workspace (directory/subdirectory) you are in.
Run the command direcly with
npx @savaryna/git-add-account
or if you want, first install it globally
npm i -g @savaryna/git-add-account
then you can run it using
git-add-account
or
gaa
After going through all the steps, you will be presented with your public SSH key so you can copy, and add it to your GIT provider. For example GitHub1:
- Go to your account settings / keys
- Click on
New SSH key
- Give it a title
- Choose
Authentication Key
for key type - Paste in the public SSH key copied earlier in the key field
- Click on
Add SSH key
- Repeat steps 2 through 6 to add a
Signing Key
key type, if you chose to sign your work (Commits, Tags, Pushes)2 - Done! Now, you can go to the workspace you chose for the account, ex:
cd /Users/john/code/work
, and all thegit
commands issued from this, or any other subdirectory, will automatically use the correct account/ssh keys.
A simple way to use multiple git accounts on one machine is to use different SSH configs based on the directory you are in. The way @savaryna/add-git-account works is, it asks you for some basic information and then it creates some files under .config
in the workspace directory you specified. Ex:
-
It creates a (private/public) SSH keypair using
ssh-keygen -t ed25519 -C "[email protected]" -f /Users/john/code/work/.config/id_ed25519_git_github_com
. See code. -
It creates a
sshconfig
file. See code.# File at /Users/john/code/work/.config/sshconfig # Config for GIT account [email protected] Host github.com HostName github.com User git AddKeysToAgent yes UseKeychain yes IdentitiesOnly yes IdentityFile /Users/john/code/work/.config/id_ed25519_git_github_com
-
It creates a
gitconfig
file. See code.# File at /Users/john/code/work/.config/gitconfig # Config for GIT account [email protected] [user] name = John Doe email = [email protected] [core] sshCommand = ssh -F /Users/john/code/work/.config/sshconfig [gpg] format = ssh [commit] gpgsign = true [push] gpgsign = if-asked [tag] gpgsign = true [user] signingkey = /Users/john/code/work/.config/id_ed25519_git_github_com
-
It runs
git config --global includeIf.gitdir:/Users/john/code/work/.path /Users/john/code/work/.config/gitconfig
, this makes sure that as long as you are in the workspace created earlier, or any other subdirectory, git will use the config from step 3 automatically3. See code. -
And finally, it presents you with your public SSH key so you can copy it and add it to your GIT provider of choice.