we want to update code to Github
First, Generating a new SSH key
- Open Terminal.
- Paste the text below, substituting in your GitHub email address.
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
This creates a new ssh key, using the provided email as a label. And you will see:
Generating public/private rsa key pair.
When you’re prompted to “Enter a file in which to save the key,” press Enter. This accepts the default file location.
Enter a file in which to save the key (/home/you/.ssh/id_rsa): [Press enter]
At the prompt, type a secure passphrase. For more information, see “Working with SSH key passphrases”.
Enter passphrase (empty for no passphrase): [Type a passphrase] Enter same passphrase again: [Type passphrase again]
Second, Adding your SSH key to the ssh-agent
Before adding a new SSH key to the ssh-agent to manage your keys, you should have checked for existing SSH keys and generated a new SSH key.
- Start the ssh-agent in the background.
$ eval "$(ssh-agent -s)" #you would get: Agent pid xxxxx
- Add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_rsa in the command with the name of your private key file.
ssh-add ~/.ssh/id_rsa # Type a passphrase which setting in first step
Third, Add the SSH key to your GitHub account
- copy the public key
$ sudo apt-get install xclip # Downloads and installs xclip. If you don't have `apt-get`, you might need to use another installer (like `yum`) $ xclip -sel clip < ~/.ssh/id_rsa.pub # Copies the contents of the id_rsa.pub file to your clipboard
- Add the SSH key to your GitHub account
- Github.com > Log in > Setting > click label ‘SSH and GPG keys’ > click button ‘New SSH key ‘
- Type title of the key ( For example, if you’re using a personal Mac, you might call this key “Personal MacBook Air”.)
- Paste the public key into the “Key” field.
- Click button ‘Add SSH key’.
4th, remote to Github
- git clone your project
git clone git@github.com:<UserName>/<ProjectName>.git
- Add remote
cd <ProjectName> git remote set-url origin git@github.com:<UserName>/<ProjectName>.git
- Sometimes, you have to setup your git data
git config --global user.name "YourName"
- Do some changes in your project
- Update to GitHub
git add . git commit -m "something" git push
if you get below error:
Q1: fatal: Could not read from remote repository.
fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists
A1:
$ ssh -T git@github.com #then you would get : Are you sure you want to continue connecting (yes/no)? [yes] Hi username! You've successfully authenticated, but GitHub does not provide shell access.
Q2: Permission denied (publickey).
sign_and_send_pubkey: signing failed: agent refused operation Permission denied (publickey).
A2:
eval "$(ssh-agent -s)" ssh-add
****if you until get the error, make sure your “git push” command without sudo