Mac ssh Configuration for Connecting Github
When you buy a new Macbook Pro or a new Mac-mini, your ssh configuration will be invalid to your github account before. So you need to make a new config.
Generate ssh keys
To communicate with your own Github account by ssh(Secure Shell), first you need to do is to generate a ssh key, including private key and public key.
Done by commands below:
ssh-keygen -t rsa -C "Your-Github-Email" -f "~/.ssh/id_rsa_Your-Github-Username"
Then the prompt hints you to set a passphrase, which makes your ssh key more secure. You can input any sequences of words, characters, or other text used to control access to your SSH key.
After that, you can gain your own ssh keys in the “~/.ssh” directory, which contains id_rsa_Your-Github-Username and id_rsa_Your-Github-Username.pub file. And the former is your own private key, while the later is the public key.
Add the SSH key to the SSH agent
Start the SSH agent in the background:
eval "$(ssh-agent -s)"
Add the ssh key:
ssh-add "~/.ssh/id_rsa_Your-Github-Username"
Add the SSH key to your Github Account
- Copy the ssh key(public key)
pbcopy < "~/.ssh/id_rsa_Your-Github-Username.pub"
- Go to GitHub -> Settings -> SSH and GPG keys -> New SSH key, and paste the key.
Config Git Details for your repo
After you clone a repo from your own Github account and made some changes in your local workspace, you need to configure your git username and email to this local repo.
git clone "https://github.com/Git-Username/Git-reponame.git"
git config user.name "Your-Github-Username"
git config user.email "Your-Github-Email"
In my opinon, till now, you can use your local mac computer to manage your github repo. But if you have more than one Github accounts, you need still to make some configurations to seperate different accounts with different ssh keys.
Configure SSH to use different keys for different Github accounts
- Create the ssh config file
touch "~/.ssh/config"
- Edit the config file, and add the configuration content to it
vim "~/.ssh/config"
# Account1
Host github-account1
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_account1
# Account2
Host github-account2
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_account2