跳到主要内容

Git SSH Configuration Once and for All

· 阅读需 5 分钟

It took me countless times to google “SSH configuration for Git”, before I really decide to write down the steps, for good.

If you have ever done this as well, or if you’ve ever wondered why we need to go through the tedious steps to set it up, or if you are simply interested in knowing how SSH and Git fit together, and make your life with Git enjoyable, this article is for you.

The content of this article will be as follows:

First, I will give a very brief review on how to configure SSH for Git, so if you are in a hurry, don’t hesitate to copy & paste the commands and just use them Second, the basic ideas of SSH and Git are talked about, giving you a pretty good overall image of how and why SSH is a good fit for Git Last but not least, some common topics will be talked about, giving your a more flexible user experience

  1. The Quick “SSH Config for Git” handbook Configuring SSH for Git is mainly about The Key Pair (private and public).

  2. You need to generate The Key Pair.

$ cd ~/.ssh $ ssh-keygen -t ed25519 -C "some comment to identify your key" then follow the interactive instructions for the key file name, and optionally a passphrase.

  1. You need to tell your Git client to use The Private Key by editing the configuration file located at ~/.ssh/config .

~/.ssh/config

Host github.com HostName github.com IdentityFile ~/.ssh/key_file_you_generated 3. Copy and paste The Public Key to Github Settings.

on MacOS, run this to copy content of the public key file you generated

$ cat key_file_you_generated.pub | pbcopy Go to GitHub and click “new SSH key” button, paste your Public Key into “Key” and fill in the “Title”, then click “Add SSH key”. (quick link: https://github.com/settings/ssh/new)

  1. You need to tell Git repository to use The Private Key.

Normally this should be optional if Host and HostName use the same value in your configuration. To verify that your repository is set with the right protocol and host, use git remote -v in your repository directory, it should show something like this:

origin git@github.com:your_account/your_repo.git (fetch) origin git@github.com:your_account/your_repo.git (push) 5. Using SSH for multiple GitHub accounts (see section 3.2 for details)

~/.ssh/config

Host work.github.com HostName github.com IdentityFile ~/.ssh/key_file_for_work_account Host me.github.com HostName github.com IdentityFile ~/.ssh/key_file_for_your_own_account 2. Concepts Explained 2.1 What is SSH SSH stands for Secure Shell. It is a protocol that your computer uses to communicate with remote servers such as github.com or gitlab.com .

Similarly, you can you HTTPS in place of SSH for Git to do the “talking” as well.

2.2 Why Use SSH Over HTTPS Pro 1. HTTPS requires username and password on every connection, which is pretty inconvenient.

It’s true you can use a GitHub client, but CLI has been a faster and more powerful medium, so maybe give it a try.

Pro 2. Multiple accounts support.

Yes, you can easily use multiple accounts with SSH, see section 3.2 for details.

Pro 3. Same routine for servers.

SSH is also the default way to connect to a remote server, which — as a developer — you will if not already, eventually possess.

  1. Common topics about SSH 3.1 Verifying the Configuration To verify if your setting is working, open a terminal and type, this command will attempt to connect to GitHub.

ssh -T git@github.com git@github.com is actually User@Host. Git host services like GitHub uses git as username for receiving SSH connections, that’s why your Git repository’s origin address says git@xxx.

If successful, a response of text should show:

Hi USERNAME! You’ve successfully authenticated, but GitHub does not provide shell access. 3.2 Using Multiple GitHub Accounts with SSH For those with more than one GitHub accounts — typically a work account and a personal account for instance — using SSH is just as easy as configuring two keys, all you need to do is use different Host values.

Step 1, use different config for Host and IdentityFile.

A sample ~/.ssh/config file should look like this:

~/.ssh/config

personal account, for side hustles and experiments

Host me.github.com HostName github.com IdentityFile ~/.ssh/your_key_file_for_personal_account

work account, use with caution

Host work.github.com HostName github.com IdentityFile ~/.ssh/your_key_file_for_work_account Step 2, paste public key content to GitHub.

This should be easy.

Step 3, make sure your repository’s configuration is correct.

Notice you can write any value for Host , but you do need to use that value in your repository’s Git config. Check it by:

$ cd $YOUR_PERSONAL_REPO $ git remote -v

origin git@me.github.com:account/your_personal_repo.git (fetch) origin git@me.github.com:account/your_personal_repo.git (push) ################################################################ $ cd $YOUR_WORK_REPO $ git remote -v origin git@work.github.com:account/your_personal_repo.git (fetch) origin git@work.github.com:account/your_personal_repo.git (push) Noting down and building a broader knowledge is sometimes more efficient than just knowing. I am a programmer who write about practical tech blogs, thanks for reading.