In this article, I will take you through the steps to install and use Git on Ubuntu 20.04 LTS. Git is a free and open source distributed version control system available in most of the hardware platforms. Of all the version control system we have available today, git is the most commonly and most widely used version control system. It can be used to manage very small to very large projects very efficiently. It started with a tiny footprint in 2005 but it has now spreaded globally with the world wide use. Git allows and encourages you to have multiple local branches that can be entirely independent of each other. More on Git Official website.
How to Install and Use Git on Ubuntu 20.04 LTS
Also Read: How to Install iwatch command on Ubuntu 20.04 LTS
Step 1: Prerequisites
a) You should have a running
Ubuntu 20.04 LTS Server.
b) You should have
root access to run privileged commands.
c) You should have
apt-get utility available in your System.
Step 2: Update Your Server
It is always a good idea to check for any latest available updates using
apt-get update command to keep your System packages up to date.
root@localhost:~# apt-get update Hit:1 http://in.archive.ubuntu.com/ubuntu focal InRelease Get:2 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB] Get:3 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB] Get:4 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB] Get:5 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1,344 kB] Ign:6 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 InRelease Hit:7 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 Release Get:8 http://in.archive.ubuntu.com/ubuntu focal-updates/main i386 Packages [562 kB] Get:9 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 DEP-11 Metadata [279 kB] Get:10 http://in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [875 kB] Get:12 http://in.archive.ubuntu.com/ubuntu focal-updates/universe i386 Packages [647 kB]
Step 3: Install Git
There are multiple ways to install Git but we will look into the most easiest way that is by using
apt-get package manager. As git package is available in the default Ubuntu Repository so it can be installed by running
apt-get install git command as shown below.
root@localhost:~# apt-get install git Reading package lists... Done Building dependency tree Reading state information... Done The following additional 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-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 93 not upgraded. Need to get 0 B/5,465 kB of archives. After this operation, 38.4 MB of additional disk space will be used. Do you want to continue? [Y/n] y Selecting previously unselected package liberror-perl. (Reading database ... 198740 files and directories currently installed.) Preparing to unpack .../liberror-perl_0.17029-1_all.deb ... Unpacking liberror-perl (0.17029-1) ... Selecting previously unselected package git-man. Preparing to unpack .../git-man_1%3a2.25.1-1ubuntu3.2_all.deb ... Unpacking git-man (1:2.25.1-1ubuntu3.2) ... Selecting previously unselected package git. Preparing to unpack .../git_1%3a2.25.1-1ubuntu3.2_amd64.deb ... Unpacking git (1:2.25.1-1ubuntu3.2) ... Setting up liberror-perl (0.17029-1) ... Setting up git-man (1:2.25.1-1ubuntu3.2) ... Setting up git (1:2.25.1-1ubuntu3.2) ... Processing triggers for man-db (2.9.1-1) ...
Step 4: Check Git Version
You can verify the git installation by running
git --version command. If it shows output like below, then it confirms that git is successfully installed.
root@localhost:~# git --version git version 2.25.1
Step 5: Setting Up Git
The next immediate step is to setup and configure the user name and email ID before performing any commit. This makes it easy to manage and track the number of commits done by multiple users on a branch. You can setup user name by using
git config --global user.name "User name" syntax and email ID by using
git config --global user.email "Email ID" syntax.
root@localhost:~# git config --global user.name "CyberITHub" root@localhost:~# git config --global user.email "firstname.lastname@example.org"
After setting up the user name and email ID, you can verify the details using
git config --list command.
root@localhost:~# git config --list user.name=CyberITHub email@example.com
All the configuration done through git command will save the information in a file called
.gitconfig in the user's home directory. So you can also verify the configuration by checking this file using
cat ~/.gitconfig command as shown below.
root@localhost:~# cat ~/.gitconfig [user] name = CyberITHub email = firstname.lastname@example.org
Step 6: Using Git
Now that we have setup Git, we can try using it by performing a git clone operations from GitHub. We can just clone a repository called wig by using
git clone https://github.com/jekyc/wig.git command as shown below. This will create a directory
wig in your local system and copy all the contents from repository.
root@localhost:~# git clone https://github.com/jekyc/wig.git Cloning into 'wig'... remote: Enumerating objects: 4240, done. remote: Total 4240 (delta 0), reused 0 (delta 0), pack-reused 4240 Receiving objects: 100% (4240/4240), 4.47 MiB | 3.24 MiB/s, done. Resolving deltas: 100% (2832/2832), done.
You can also check all the subcommands available with git command using
git help -a command as shown below.
root@localhost:~# git help -a See 'git help <command>' to read about a specific subcommand Main Porcelain Commands add Add file contents to the index am Apply a series of patches from a mailbox archive Create an archive of files from a named tree bisect Use binary search to find the commit that introduced a bug branch List, create, or delete branches bundle Move objects and refs by archive checkout Switch branches or restore working tree files cherry-pick Apply the changes introduced by some existing commits citool Graphical alternative to git-commit clean Remove untracked files from the working tree clone Clone a repository into a new directory commit Record changes to the repository ..................................
Step 7: Uninstall Git
Once you are done using Git, you can think of removing this tool completely by using
apt-get remove git command as shown below.
root@localhost:~# apt-get remove git Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: git-man liberror-perl Use 'apt autoremove' to remove them. The following packages will be REMOVED: git 0 upgraded, 0 newly installed, 1 to remove and 93 not upgraded. After this operation, 36.5 MB disk space will be freed. Do you want to continue? [Y/n] y (Reading database ... 199674 files and directories currently installed.) Removing git (1:2.25.1-1ubuntu3.2) ...