How to Install Git on Linux

Git is a popular open source version control system like CVS or SVN. This article is for those, who are not familiar with Git. Here, we are providing you with basic steps of installing Git from source, Creating a new project, and Commit changes to the Git repository.
Most of the other version control systems, store the data as a list of files and changes are made to each file over time. Instead, Git thinks of its data more like a set of snapshots in a file system. Every time, it takes a snapshot of all your files (which look alike at that particular moment), it will be stored as a reference. If files are not changed, Git does not store the new snapshots. In this case, it just links to a previous snapshot of your file system.
Git is available with all the major Linux distributions. Thus, the easiest way to install Git is by using a Linux package manager. Use the following command to install git on Linux –
Use the following command to install git on Linux –
$ sudo apt-get install git
The output should be like this –
tp@linux:~$ sudo apt-get install git
[sudo] password for tp:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
git-man liberror-perl
Suggested packages:
git-daemon-run git-daemon-sysvinit git-doc git-el git-email git-gui gitk
gitweb git-arch git-bzr git-cvs git-mediawiki git-svn
The following NEW packages will be installed:
git git-man liberror-perl
0 upgraded, 3 newly installed, 0 to remove and 286 not upgraded.
Need to get 3,421 kB of archives.
After this operation, 21.9 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
......
An alternate way is to install Git from source which should be like this –
$ sudo apt-get install libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev asciidoc xmlto docbook2x
The output should be like this –
tp@linux:~$ sudo apt-get install libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev asciidoc xmlto docbook2x
Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting 'zlib1g-dev' instead of 'libz-dev'
gettext is already the newest version.
gettext set to manually installed.
The following extra packages will be installed:
comerr-dev dblatex docbook-dsssl docbook-utils docbook-xml docbook-xsl
fonts-lmodern fonts-texgyre jadetex krb5-multidev latex-beamer latex-xcolor
libcomerr2 libcurl3-gnutls libencode-locale-perl libexpat1
libfile-listing-perl libfont-afm-perl libgcrypt11-dev libgnutls-dev
libgnutls-openssl27 libgnutls26 libgnutlsxx27 libgpg-error-dev
libgssapi-krb5-2 libgssrpc4 libhtml-form-perl libhtml-format-perl
libhtml-parser-perl libhtml-tagset-perl libhtml-tree-perl
libhttp-cookies-perl libhttp-daemon-perl libhttp-date-perl
libhttp-message-perl libhttp-negotiate-perl libidn11-dev libintl-perl
libio-html-perl libk5crypto3 libkadm5clnt-mit9 libkadm5srv-mit9 libkdb5-7
libkrb5-3 libkrb5-dev libkrb5support0 libldap-2.4-2 libldap2-dev
.......
Git is by default installed under /usr/bin/git directory on recent Linux systems.
Once the installation is done, verify it by using the following command –
$ whereis git
The output should be like this –
git: /usr/bin/git /usr/bin/X11/git /usr/share/man/man1/git.1.gz
To get the version number of Git, you can use the following command –
$ git --version
The output will be like this –
git version 1.9.1
If you want to specify a User and Password information to Git repository, then use the following command –
$ git config --global user.email sairamkrishna@tutorialspoint.com
For verifying Git configuration, use the following command –
git config --list
The output should be like this –
user.email=sairamkrishna@tutorialspoint.com
The above information is stored in the .gitconfig file under the home directory. To verify, use the following command –
cat ~/.gitconfig
The output should be like this –
[user]
email = sairamkrishna@tutorialspoint.com
To create a Git repository project, we should attach any local directory. Suppose, if project directory is located under /home/tp/projects path, first go into that directory using CD command and execute git init command as shown below –
$ cd /home/tp/projects

~/projects$ git init
The output should be like this –
Initialized empty Git repository in /home/tp/projects/.git/
The above command creates a .git directory under projects folder. To verify, use the following command-
~/projects$ ls -altr .git
The output should be like this –
tp@linux:~/projects$ ls -altr .git
total 40
drwxrwxr-x 4 tp tp 4096 Feb 11 14:03 refs
drwxrwxr-x 2 tp tp 4096 Feb 11 14:03 info
drwxrwxr-x 2 tp tp 4096 Feb 11 14:03 hooks
-rw-rw-r-- 1 tp tp 23 Feb 11 14:03 HEAD
-rw-rw-r-- 1 tp tp 73 Feb 11 14:03 description
drwxrwxr-x 2 tp tp 4096 Feb 11 14:03 branches
drwxrwxr-x 3 tp tp 4096 Feb 11 14:03 ..
drwxrwxr-x 4 tp tp 4096 Feb 11 14:03 objects
-rw-rw-r-- 1 tp tp 92 Feb 11 14:03 config
drwxrwxr-x 7 tp tp 4096 Feb 11 14:03 .
Once a project is created, it will initialize the project using “git init”. Now, add your files to your project directory. For adding .txt files to Git repository, use the following command –
projects$ git add *.txt
Once adding process is done to the repository, you should commit these files as shown below command –
projects$ git commit -m 'Initial upload of the project'
The sample output should be like this –
[master (root-commit) 261b452] Initial upload of the project
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 tp.txt
Congratulations! Now, you know “How to setup git on Linux” . We’ll learn more about these types of commands in our next Linux post. Keep reading!