How to install and use Git on Linux (Command Line)?

In the previous tutorial, you have learned about Git and GitHub. And the various types of features of GitHub like, pull requests, branches, repositories, merge pull requests. Now in this section, I will be only talking about the practical side of using Git on Linux and that is the important part of mastering GitHub. In this part, we will deal with command line interface in order to use GitHub. So Let’s get into it.

1. Installing Git for Linux

First, you gonna do is installing git on your Linux machine. If you are on a Ubuntu distro you have to use this command but I’m using Kali Linux and Git is already installed in it.

root@Louis:~# sudo apt-get install git

Actually “sudo” is worthless here by the way because I’m already on “root” but when you are on the Ubuntu distro you have to type “sudo”.

2. Configuring GitHub

Once, you have installed the Git on your Linux machine it’s time to configure the GitHub. You have to use your GitHub credentials in order to configure it.

root@Louis:~# git config --global "anandesh-sharma"
root@Louis:~# git config --global ""

just replace the strings in double quotes with your username and email address and you are done.

3. Create a Repository

Once we have configured our GitHub then we need to create a repository on GitHub. For that, you need to make a directory on your Linux machine that will serve as a repo. Let’s say the folder is “test”.

root@Louis:~# git init Test

“init” means we are initializing an empty repo named “Test”. And you will get this as a result: Initialized empty Git repository in /root/Test/.git/

Output in your case may be like: Initialized empty Git repository in /home/user/Test/.git/ (in Ubuntu or like distros).

4. Create a README File

Now it’s time to put something in our repo and usually, we initialize repo with a README file which is a text file that contains the information about the repo. First, you need to change your current directory with to the folder you have created “Test”.

root@Louis:~# cd Test

After that, you need to create a “.txt” file using “gedit or “touch” or “echo” commands to create your text file.

root@Louis:~/Test# echo "This is my test repository" > README

5. Adding Repository files

This is an important step. Here we add all the things that need to be pushed onto the website into an index. These things might be the text files or programs that you might add for the first time into the repository or it could be adding a file that already exists but with some changes (a newer version/updated version).

We have created a file README already, let’s create another file test.cpp (just a basic cpp program)

#include <iostream>
using namespace std;
int main(){
cout << "Hello World" << endl;
return 0;

Now we have two files README and test.cpp. git add” command can be used to add any number of files and folders to the index. Here, when I say index, what I am referring to is a buffer like space that stores the files/folders that have to be added to the Git repository.

root@Louis:~/Test# git add README
root@Louis:~/Test# git add test.cpp

6. Committing changes

Once all the files are added, we can commit it. This means that we have finalized what additions and/or changes have to be made and they are now ready to be uploaded to our repository. Use the command :

root@Louis:~/Test# git commit -m "This repo contains README and test.cpp file"

Now open your GitHub account and Create a repo in which you commit these file from your local machine to GitHub account.

Go Here: How to create a repository on GitHub

Once this is created, we can push the contents of the local repository onto the GitHub repository in your profile. Connect to the repository on GitHub using the command:

Important Note: Make sure you replace ‘user_name’ and ‘Test’ in the path with your Github username and folder before running the command!

root@Louis:~/Test# git remote add origin

7. Pushing Local Repository to GitHub Repository

The final step is to push the local repository contents into the remote host repository (GitHub), by using the command:

root@louis:~/test# git push -f origin master

Here I’ve used -f for forcing the updates to GitHub repositories because I initialized the GitHub repo with the file and when you try to simply push the local repo to remote one, it will throw an error. The reason of the error is: your local repo doesn’t know about another file yet that’s why you’ll get an error saying “error: failed to push some refs to ‘'".

You can simply pull the files from the repo to overcome this error

root@Louis:~/Test# git pull​ master

Remember the rhythm : Add > Commit > Push > Pull > Repeat — — — Is that simple ;-)

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.