Skip to content

🔐 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.

License

Notifications You must be signed in to change notification settings

savaryna/git-add-account

Repository files navigation

@savaryna/add-git-account

🔐 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.

Usage

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:

  1. Go to your account settings / keys
  2. Click on New SSH key
  3. Give it a title
  4. Choose Authentication Key for key type
  5. Paste in the public SSH key copied earlier in the key field
  6. Click on Add SSH key
  7. Repeat steps 2 through 6 to add a Signing Key key type, if you chose to sign your work (Commits, Tags, Pushes)2
  8. Done! Now, you can go to the workspace you chose for the account, ex: cd /Users/john/code/work, and all the git commands issued from this, or any other subdirectory, will automatically use the correct account/ssh keys.

Example of how it works

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:

  1. 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.

  2. 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
  3. 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
  4. 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.

  5. And finally, it presents you with your public SSH key so you can copy it and add it to your GIT provider of choice.

License

MIT © Alex Tofan

Footnotes

  1. https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account?tool=webui

  2. https://docs.github.com/en/authentication/managing-commit-signature-verification

  3. https://git-scm.com/docs/git-config#_conditional_includes

About

🔐 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.

Topics

Resources

License

Stars

Watchers

Forks