Last active
October 6, 2022 09:38
-
-
Save BugBuster1701/80968cfa44b7c1730548d64fdaa30c17 to your computer and use it in GitHub Desktop.
Automatically use correct SSH key for remote Git repo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://superuser.com/questions/232373/how-to-tell-git-which-private-key-to-use | |
Speziell dort Environment variable GIT_SSH_COMMAND | |
Configuration core.sshCommand: | |
From Git version 2.10.0, you can configure this per repo or globally, | |
so you don't have to set the environment variable any more! | |
git config core.sshCommand "ssh -i ~/.ssh/id_rsa_example -F /dev/null" | |
git pull | |
git push | |
Idee: den normalen Account über ~/.ssh/config definieren, siehe obigen Link den Eintrag drüber, | |
den speziellen für components mittels core.sshCommand | |
-- | |
http://www.bennyn.de/programmierung/mehrere-github-repositories-mit-verschiedenen-ssh-keys-benutzen.html | |
-- | |
https://www.keybits.net/post/automatically-use-correct-ssh-key-for-remote-git-repo/ | |
Automatically use correct SSH key for remote Git repo | |
2013-10-07 | |
If you have more than one GitHub account, or push to remote repositories using various accounts, it’s handy if the correct SSH key will automatically be used for the project you are working on. This post explains how to achieve that. | |
Create keys for each GitHub account | |
Generate a new key pair: | |
ssh-keygen -t rsa -C "github-user1" -f "github-user1" | |
The -C option is just a comment to help you identify the key. The -f option specifies the file name for the key pair. You can choose how to name the key pair - I’d suggest something like github-user1 where user1 is your GitHub username. | |
Enter a strong passphrase when prompted and make sure to remember it! | |
You’ll now have a public and private key in your ~/.ssh/ folder. | |
Repeat the above for each GitHub account you want to use. | |
Add the public key to the relevant GitHub account | |
The GitHub documentation will help you with this. | |
Configure SSH | |
In ~/.ssh/ create a file called config with contents based on this: | |
#user1 account | |
Host github.com-user1 | |
HostName github.com | |
User git | |
IdentityFile ~/.ssh/github-user1 | |
#user2 account | |
Host github.com-user2 | |
HostName github.com | |
User git | |
IdentityFile ~/.ssh/github-user2 | |
Replace user1 or user2 with your GitHub usernames | |
[optional] Getting your keys on a keyring | |
Depending on your operating system you’ll need to find out how best to do this. Using a keyring saves typing passphrases all the time. | |
I use Xubuntu which comes with gnome-keyring. This starts on system startup (Settings > Session and Startup > Advanced > Launch Gnome services on startup) and adds all keys in ~/.ssh/ when I log in to the system. | |
You can check the keys on your keyring with ssh-add -L | |
Configure your Git repo | |
You can clone an existing GitHub repo if you don’t already have a local copy. The following syntax will ensure the remote is configured correctly to work with our setup: | |
git clone [email protected]:user1/your-repo-name.git your-repo-name_user1 | |
Go to the local Git repo that you want to configure (or the one you just cloned) and enter: | |
git config user.name "user1" | |
git config user.email "[email protected]" | |
Where user1 matches the values you used earlier in your ssh config. | |
If you cloned using the above command then you’re all set. If the repo was already present you might need to customise your Git remote. Check your current remotes with git remote -v | |
Assuming you want to update origin you’ll need to do: | |
git remote set-url origin [email protected]:user1/your-repo-name.git | |
Make sure that the bit between : and @ matches the relevant Host defined earlier in your ssh config. This will make sure that Git uses the correct key. | |
You can now git push as normal and the correct key will automatically be used. | |
Credit: most of the information in this post is gleaned from this Gist. I’ve rewritten for clarity and added some information about using keyrings. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment