Skip to content

Instantly share code, notes, and snippets.

@aeros281
Last active July 2, 2024 10:40
Show Gist options
  • Save aeros281/7ef1417a8b63d930f0cae60c95e658be to your computer and use it in GitHub Desktop.
Save aeros281/7ef1417a8b63d930f0cae60c95e658be to your computer and use it in GitHub Desktop.
Multiple github accounts using SSH keys

Here is the general step requires to automatically switch to different accounts of GitHub based on the current working directory:

  1. Create a different set of private-public key using ssh-keygen command.
  2. Add the new public key to the other Github account's SSH keys.
  3. If there's no ~/.gitconfig file presented, create a new file with content that will be described in later section.
  4. Create extend config for git that will only applied when go to a specific folder, the content will be described in later section.
  5. Clone the fork repository, then add new remote using git remote add command.

To create a different set of private-public key, use the following command:

cd ~/.ssh
ssh-keygen -f my_other_key

You can use different name for my_other_key, after this we will have two new files:

  1. ~/.ssh/my_other_key -> this is the private key.
  2. ~/.ssh/my_other_key.pub -> this is the public key.

Copy the content of my_other_key.pub to the target account (it should be different account you want to be indentified as, if this account belongs to other user please send the public key content to them).

The below is the content of ~/.gitconfig file, please note that if the file is not existed please create it.

Please note:

  1. Use your normal email + name for the field of email and name. It should the be the normal value since this config is applied globally.
  2. For the includeIf part, it is assumed that you have 3 git repo that you want to use different github account for:
    1. ~/Projects/my_other_workspaces/project1
    2. ~/Projects/my_other_workspaces/project2
    3. ~/Projects/my_other_workspaces/project3

Remember to use the parent folder (the folder that contains the repo folder), not the repo folder. Also make sure that there's a end / character at the end.

[user]
email = <your email>
name = <your name>
[includeIf "gitdir:~/Projects/my_other_workspace/"]
path = .gitconfig-my_other_workspace

Assume that the value of path you have in the ealier section is .gitconfig-my_other_workspace and the private key created name my_other_key, for this step you need to create a new file in ~/.gitconfig-my_other_workspace. The content of that file is described in the next section.

Please note: if you want to keep your user to be the same as the global user then just remove the [user] section.

[user]
name = <your other user>
email = <your other user>
[core]
sshCommand = "ssh -i ~/.ssh/my_other_key -F /dev/null"
Even if you did all the steps above, you still need to add the other repo remote into your current repo. The command is like this:
`git remote add <short_name> <ssh_link>`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment