If you are tired of typing username and password in git cli then you are at right place. In this post I would be sharing how you can improve your productivity while using git. I will be writing how you can set something so that you don’t have to write username and password again and again while using git cli.
Assumptions
I have assumed that you know how to use git or basically know what is git.
SSH
SSH is a protocol which is mostly used to connect to remote systems. Using the SSH protocol, we can connect and authenticate to remote servers and services. If you are a developer and have your own remote server than you probably know SSH more. But for the rest of the readers I am explaining what is SSH. You may have heard of FTP ever. Basically it is like accessing your file from a server. FTP stands for File Transfer Protocol so if we want to access any files from server we use FTP. It is like a file browser which can access files from remote. Core.ftp, Filezilla are few are the famous clients used for FTP. Now SSH is more than it. You have terminal access of the remote system meaning you have everything to access the system. Well if you have been using linux than you probably know the power of terminal. It can do everything from the smallest tasks to the biggest one.
So basically what matters for us now to setup such system are the keys. So its like keys are the replacement for your username and password. And moreover you have to set is once on a system and it will work on that system forever.
Setting SSH in Linux
Open a terminal and run this command on it.
ssh-keygen -t rsa -b 4096 -C "[email protected]"
Well please replace [email protected] with your email ID. It may look something like this on Ubuntu and similar on other linux flavours too.

Now it is asking you to enter the location where you want to save it. By default it will save in your home directory if you don’t give a specific location.

So now it is asking for a password. So the basic philosophy behind the passphrase concept is that if you are using the generated keys anywhere it is going to ask you the passphrase which you will enter now. If you enter your password now than while performing git operations like push it will ask for the password which you are entering now. And no doubt you can have no password option too which will never ask your password while performing git operations. So do whatever you like.

So in the above screenshot you can see it clearly that I have created key pairs for using SSH in git. And these are saved in /home/kdpisda/.ssh/git_tutorial.pub
in my system.
Set GitHub
Now open GitHub website i.e. https://github.com and click on the top right corner on your profile. Make sure you have logged in. Click on settings or simply visit https://github.com/settings/profile after logging in at GitHub website. You will see a list menu on the left side similar to this.

Now click on SSH and GPG keys and a new page will open. Find New SSH Key button and click on it. Now again a new page will open which will look similar to this.

Now add some title as you want it doesn’t matter much. Now we have to copy the key which we generated recently and paste it one the key field on the page. So for that open the file and copy the whole content of it. And it really doesn’t matter which tool you use for copying like nano, vim, gedit or any copying tool like xclip. Our final goal is to copy the content of the generated key.

Copy it and paste the whole content and click Add SSH Key button on the page.
How to use SSH
Now we are ready to use our SSH keys for that you don’t have to do anything. Just keep in mind that while cloning a repo click on use SSH and then same commands like
git clone ....


Chaning from HTTPS to SSH on existing projects
CD to your existing project. Git uses a .git folder to keep tracking of what you do on the repo and git settings too are in that folder. There is a file config inside .git folder. Normally you can’t see .git folder in a file manager since it is hidden. So type nano .git/config
inside the root directory of the repo and it will open that file. You may replace nano with your favourite text editor. It looks something like this.

I think you got the hack. It’s all about
url = https://...
Change it to a ssh url which you may find in the above images i.e. click on Use SSH and you will get a ssh url for the repo. so change url in this file and you may switch easily from https to ssh and vice versa. Hope you loved it.
Follow me on GitHub.
Connect with me on LinkedIn.
Subscribe for the latest posts. :-)
Originally published at www.kdpisda.tech on April 25, 2018.
Comments