If you are a developer, version control has a major role in your life. You may have different version control systems like github, bitbucket etc. Remembering username and password of each version control and enter them each and every time you perform any git action is hard.
Thankfully github and Bitbucket has ssh key integration to connect to your repository. Here I am explaining how you can connect to your github repository using SSH key.
Note: My OS is Ubuntu 18.04. The command may differ in other OS. Please cross check the commands.
First you have to create new ssh key
ssh-keygen -t rsa -b 4096 -C "[email protected]"
Add your github email address in the above command.
This creates a new ssh key, using the provided email as a label.
Enter a file in which to save the key (/home/you/.ssh/id_rsa): [Press enter]
Press enter to proceed
Enter a password when prompted. This password will be required for authenticating once you logged in.
Now your ssh key will get generated. You have to add the public key to your github account.
Copy the public key to clipboard. You can try any of the following methods
$ 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
#Copy the content
#opens the text editor. You can copy the key information from there.
Login to your github account, got to settings page
In the user settings sidebar, click SSH and GPG keys.
In the “Title” field, add a descriptive label for the new key. For example, if you’re using a personal lap, you might call this key “Personal Lap”.
Paste your key into the “Key” field.
Click Add SSH key.
If prompted, confirm your GitHub password.
After you’ve set up your SSH key and added it to your GitHub account, you can test your connection.
ssh -T [email protected]
# Attempts to ssh to GitHub
Type yes when prompted for ‘continue connection’
Now you may see a message like this.
Hi username! You've successfully authenticated, but GitHub does not
provide shell access.
Congratulations, now you can use your connect to your github account using the ssh key !!!
Now you can clone your github repository using ssh command.
In github, copy the ssh clone link for a repository.
Open terminal and go to the folder where you want to clone the repository. Then clone the repository using the clone command as below.
git clone [email protected]:git/git.git
Now you can do git operations like push, pull, fetch etc without prompting for your username and password.
Note: If you restart your system, when you try to do any git operation, it will prompt for your passphrase for first time. Enter the passphrase to continue. You can save the password permanently to avoid asking for passphrase every time you log on the system.
If you have already a local repository, you have to run one more line of code to link your ssh key with the local repository.
git remote set-url origin firstname.lastname@example.org:git/git.git
Now you can continue using already cloned repository also with ssh.
This article has already been published on https://email@example.com/how-to-connect-your-github-repository-using-ssh-key-fcfbf0d62eb6