This article is not about what git is. You can learn about git in this article. That article should suffice to get you started on git. It’s very important that you know what git is and why it is so crucial. It is one of the core tools in the toolkit of a professional app developer. This article shows how to access a remote repository using SSH. If you just want the solution, you can skip everything and go to summary, though I recommend reading the article line by line.
Two Ways to Access a Remote Repository
where <git_user_name> represents your username for git repository hosting service. If you prefer to use HTTPS URL then every time you do a transaction with the remote repository you will be asked to enter your login credentials.
This article explains how to use a SSH URL to access your remote repository on a git repository hosting service. Personally I prefer this one, since I do not have to enter my credentials every-time I transact with the remote repository and it saves lot of time.
I will use GitHub repository hosting service for this article. Same steps can be applied to other repository hosting services (either open source or commercial ones).
SSH into GitHub — Everything explained in one place.
Once you have initialised a git repository for your project in your desktop/laptop and added and committed the changes to local repository, it’s about time you create a remote repository on GitHub.
Once you create your remote repository you will get two URLs as mentioned before (This article is applicable to already existing repositories as well).
Copy the SSH URL and add it as remote URL using this command in your terminal (NOTE: you should be in the same directory where you executed git init command):
git remote add origin email@example.com:<git_user_name>/hello-world.git
Then you can execute the command:
git push -u origin master.
If previous command executes without any errors, then you have created remote repository of your project on GitHub. NOTE: Pushing to master directly is fatal in professional development and NOT recommended.
If the previous command gives you an error then continue reading…
If you see an error message like the one below or any other error message for that matter, then continue reading…..
ERROR: Repository not found.
fatal: Could not read from remote repository
Please make sure you have the correct access rights
and the repository exists.
The answer to resolving this error lies in the error message itself ( Hint: Just read the last two lines. ) Again I am not going to go into why you get that error. In order to keep things simple, let’s just say your GitHub account is not configured for remote access via SSH using your computer/laptop.
Resolving the ERROR
All it takes is 3 steps:
- Generate a ssh key.
- Add ssh key to ssh-agent.
- Finally add your ssh key to your GitHub account.
1.Generate a ssh key.
Many articles on the web including the one on the GitHub docs page asks the user to check if an ssh-key already exists on the user’s computer. Personally I prefer creating a new key. If you prefer to use an existing one that’s also fine.
Open terminal (Ctrl+Alt+T)command and execute cd command to make sure you are in your /home/<user_name> directory. By default on Linux based system terminal always opens in /home/<user_name> folder. To generate a new ssh-key the command is:
$ ssh-keygen -t rsa -b 4096 -C "firstname.lastname@example.org"
Just replace email@example.com with your GitHub email. Also do not forget the quotation. Refer the following screenshot:
You can now see in your terminal, that there is a cursor prompting you to enter the name of the file where you want the ssh key to be saved. Just press enter.
Then you will be prompted to enter a passphrase if you wish to enter one, else you can leave it empty which means there wont be any pass phrase. Just press enter twice in order to proceed (in this case no passphrase will be created). At this stage your ssh key is generated and the output on your terminal will be similar to:
2. Add SSH Key to ssh-agent.
Upon successful completion of previous step, you can execute the following command:
$ ls -la .ssh, the output of which will list the ssh key generated. Again you can refer the following screenshot:
There are two versions of ssh key here: 1) private called with name id_rsa and 2)id_rsa.pub
cd into .ssh folder (full path /home/<user_name>/.ssh) and change the permission of id_rsa by executing the following command:
$ sudo chmod 400 id_rsa
Start the ssh-agent in the background using the command:
$ eval "$(ssh-agent -s)"
In order to add the ssh-key to the ssh-agent you can use the command:
$ ssh-add ~/.ssh/id_rsa
Don’t forget to replace id_rsa with the file name you provided in step 1. If you followed the article as it is then you will have the same ssh-key names viz, id_rsa and id_rsa.pub.
3. Add the SSH Key to GitHub
In order to finish this step, you will need a command line tool called xclip. You can check if it is installed on your OS by checking if man pages exist for it.
$ man xclip
If you see the message “No manual entry for xclip”, then install xclip using the command:
sudo apt-get install xclip
Once xclip is installed, log into your GitHub account. Click in the upper right corner where you see your profile pic and go to Settings (Again you can refer the screenshot below).
In settings on the left navigation menu you will see SSH and GPG keys. Click on it. At this stage return to your terminal and copy the ssh key using the command:
$ xclip -sel clip < ~/.ssh/id_rsa.pub
Come back to your GitHub SSH and GPG keys page and click on the button which says “New SSH key” ( Refer screenshot ).
Then you will be directed to the following page:
There you can give any title for your ssh key and in text-area where it says Key, you can right click and paste (If paste does nothing, then execute the xclip command once more). Now you should be able to see ssh key text which begins with ssh-rsa and ends with your GitHub email. Go ahead and click on the “Add SSH key”.
Congrats! Now you can access all your repositories on GitHub using SSH URL which does not require any credentials. ( because we have added ssh-key as a security layer.)the
Using SSH git URL involves three steps:
- Generate ssh key using the command:
ssh-keygen -t rsa -b 4096 -C "firstname.lastname@example.org" where email@example.com is your GitHub email.
- Add ssh key to ssh agent using the commands:
$sudo chmod 400 id_rsa
$eval "$(ssh-agent -s)" and finally $ssh-add ~/.ssh/id_rsa
3. Install xclip and execute the command
$ xclip -sel clip < ~/.ssh/id_rsa.pub
If all three steps were executed without any errors as per the article instructions, the last step involves logging into your repository account (example GitHub, BitBucket, etc) and creating new SSH key which will contain the value from xclip. In GitHub you will see a text-area with name Key, where you will paste the value you obtained from xclip. That’s it, you are all set now.