How To Install and Use Git on Ubuntu 20.04

In this guide, we want to teach you How To Install and Use Git on Ubuntu 20.04.

Git is a distributed, open-source version control system (VCS) that enables you to store code, track revision history, merge code changes, and revert to earlier code versions when needed.

Benefits of Git:

  • Historical Change Tracking
  • Work as a Team
  • Improve Team Speed & Productivity
  • Availability and Redundancy
  • Git Is the Industry Standard

How To Install and Use Git on Ubuntu 20.04

To set up Git, you must log in o your server as a non-root user with sudo privileges. To do this, you can follow our guide on Initial Server Setup with Ubuntu 20.04.

Installing Git on Ubuntu 20.04

By default, git is installed on your Ubuntu 20.04. You can verify it by checking its version:

git --version
Output
git version 2.25.1

Note: If you didn’t get a version number in your output, you can easily install git from the APT repository:

# sudo apt update
# sudo apt install git

Now you can move on to configuring the Git section.

Installing Git from the Source

There is another way to install Git that is more flexible and has the most release. It is to install Git from the source. To do this, follow the steps below:

First, install the required packages on your server with the following command:

sudo apt install libz-dev libssl-dev libcurl4-gnutls-dev libexpat1-dev gettext cmake gcc

Then, create a temp directory and navigate to it with the following commands:

# mkdir tmp
# cd /tmp

At this point, visit this link and download the version you want to use with the curl command:

curl -o git.tar.gz https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.9.3.tar.gz

To know more about the curl command, you can visit the How To Use curl command in Linux.

Then, extract your downloaded file:

tar -zxf git.tar.gz

Switch to your Git directory:

cd git-*

Now make the package and install it with the following commands:

# make prefix=/usr/local all
# sudo make prefix=/usr/local install

It will take some time to complete.

Next, replace the shell process:

exec bash

Then, verify your Git installation on Ubuntu 20.04 by checking its version:

git --version
Output
git version 2.9.3

Configuring Git

At this point, you must configure Git so that the generated commit messages you make will contain the correct information and support you as you build your software project.

You must provide your name and email address because Git embeds this information into each commit you do. To do this, you can use the following commands:

# git config --global user.name "Your Name"
# git config --global user.email "[email protected]"

After setting up the user name and email ID, you can verify the details using git config --list command:

git config --list
Output
user.name=Your Name
user.email=[email protected]

All the configuration done through the 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:

cat ~/.gitconfig
Output
[user]
   name=Your Name
   email=[email protected]

Using Git on Ubuntu 20.04

Now that you have set up Git, you can try using it by performing 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 the repository:

git clone https://github.com/jekyc/wig.git
Output
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 | 7.89 MiB/s, done.
Resolving deltas: 100% (2832/2832), done.
Checking connectivity... done.

Also, you can check all the subcommands available with the git command using git help -a command as shown below:

git help -a
Output
usage: git [--version] [--help] [-C <path>] [-c name=value]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]

available git commands in '/usr/local/libexec/git-core'

add merge-index
add--interactive merge-octopus
am merge-one-file
annotate merge-ours
apply merge-recursive
archimport merge-resolve
archive merge-subtree
bisect merge-tree
bisect--helper mergetool
blame mktag
branch mktree
.....

That’s it.

Conclusion

At this point, you learn to Install Git from the APT repository and Source on Ubuntu 20.04. Also, you learn basic usage of it.

Hope you enjoy it.

You may be interested in these articles:

How To Set up Ruby on Rails on Ubuntu 20.04

Install and Configure Lighttpd on Ubuntu 20.04

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!