How to Install GIT on Ubuntu 22.04 | 20.04

Git is a free and open-source distributed version control system designed to handle everything from small to extensive projects quickly and efficiently. Git is easy to learn and has a tiny footprint with lightning-fast performance. It outclasses SCM tools like Subversion or CVS with features like cheap local branching, convenient staging areas, and multiple workflows.

The following tutorial will teach you how to install GIT on Ubuntu 22.04 or 20.04 Linux distributions with instructions on using the standard repository or importing the latest version from the Ubuntu Git maintainers teams. The tutorial will cover some basic Git commands required for everyday use.

Recommended Steps Before Installation

First, update your system to ensure all existing packages are up to date.

Before you begin, run an update on your system to ensure all packages are up-to-date to avoid any conflicts during the installation.

sudo apt update

Optionally, you can list the updates for users who require review or are curious.

sudo apt --list upgradable

Proceed to upgrade any outdated packages using the following command.

sudo apt upgrade

#1st Method: Install Git – Ubuntu Repository

The first method is to install Git directly from Ubuntu’s repository. This is the easiest solution and is recommended. First, ensure you have not installed Git by running the following command.

git --version

If nothing is returned, run the installation command.

sudo apt install git

And that is it; you can additionally check the Git version installed by running the following command.

git --version

Example output:

git version 2.37.2

#2nd Method: Install Git – Ubuntu Git Maintainers PPA

The second method is to install the latest from the Ubuntu Git Maintainers team for the latest stable version. This can be more desirable to get bug fixes and newer features, you have to way up the option and the environment you are working in. For most users, using this version is often more desirable.

Import Ubuntu Git Maintainers PPA

First, install the following packages that are required. These are most likely installed but run the command to be safe.

sudo apt install dirmngr ca-certificates software-properties-common apt-transport-https -y

For users who have not previously imported a GPG key from the Ubuntu keyserver, the command line terminal will often have issues importing GPG keys from LaunchPAD PPAs because the directories are not created. This is an easy fix. Use the following command that will, in turn, generate the directories.

sudo gpg --list-keys

Example output:

gpg: directory '/root/.gnupg' created
gpg: keybox '/root/.gnupg/pubring.kbx' created
gpg: /root/.gnupg/trustdb.gpg: trustdb created

The next task is to import the GPG key needed.

sudo gpg --no-default-keyring --keyring /usr/share/keyrings/git-core-ppa.gpg --keyserver keyserver.ubuntu.com --recv-keys E1DD270288B4E6030699E45FA1715D88E1DF1F24 >> /dev/null

Example output:

gpg: keybox '/usr/share/keyrings/git-core-ppa.gpg' created
gpg: key A1715D88E1DF1F24: public key "Launchpad PPA for Ubuntu Git Maintainers" imported
gpg: Total number processed: 1
gpg:               imported: 1

With the GPG key now imported, you can import the LaunchPAD PPA. Remember, match the command to the version of Ubuntu you are utilizing, or the installation will likely fail with errors.

Import Git PPA for Ubuntu 22.10 Kinetic Kudu

echo 'deb [signed-by=/usr/share/keyrings/git-core-ppa.gpg] https://ppa.launchpadcontent.net/git-core/ppa/ubuntu kinetic main' | sudo tee -a /etc/apt/sources.list.d/git-core-ppa.list

Import Git PPA for Ubuntu 22.04 LTS Jammy Jellyfish LTS

echo 'deb [signed-by=/usr/share/keyrings/git-core-ppa.gpg] https://ppa.launchpadcontent.net/git-core/ppa/ubuntu jammy main' | sudo tee -a /etc/apt/sources.list.d/git-core-ppa.list

Import Git PPA for Ubuntu 20.04 LTS Focal Fossa LTS

echo 'deb [signed-by=/usr/share/keyrings/git-core-ppa.gpg] https://ppa.launchpadcontent.net/git-core/ppa/ubuntu focal main' | sudo tee -a /etc/apt/sources.list.d/git-core-ppa.list

Alternative Branch Git Release Candidate (RC release)

Alternatively, you can instead import the release candidate branch. Currently, they contain the same build, but generally, you will find a newer version when ready in this PPA first. For most users, I recommend installing stable, but you can import both PPAs. Generally, the one with the latest version should always be installed first.

Import Git RC PPA for Ubuntu 22.10 Kinetic Kudu

echo 'deb [signed-by=/usr/share/keyrings/git-core-ppa.gpg] https://ppa.launchpadcontent.net/git-core/candidate/ubuntu/ kinetic main' | sudo tee -a /etc/apt/sources.list.d/git-core-ppa.list

Import Git RC PPA for Ubuntu 22.04 LTS Jammy Jellyfish LTS

echo 'deb [signed-by=/usr/share/keyrings/git-core-ppa.gpg] https://ppa.launchpadcontent.net/git-core/candidate/ubuntu/ jammy main' | sudo tee -a /etc/apt/sources.list.d/git-core-ppa.list

Import Git RC PPA for Ubuntu 20.04 LTS Focal Fossa LTS

echo 'deb [signed-by=/usr/share/keyrings/git-core-ppa.gpg] https://ppa.launchpadcontent.net/git-core/candidate/ubuntu/ focal main' | sudo tee -a /etc/apt/sources.list.d/git-core-ppa.list

With the repository imported, run a quick update command.

sudo apt update

Further, verify the repositories by using the policy command.

apt-cache policy git

As mentioned above, the tutorial imported both the stable and the candidate branches, with both having a priority set at 500. So if a version drops in any of the repositories, you should get prompted to update it immediately over the standard Ubuntu repository.

Git Installation command

With the PPA now set, install Git using the following command.

sudo apt install git -y

Once done, check the version to see your new Git version.

git --version

Example output:

git version 2.38.1

With the tutorial, the original version was v2.34.1, and the new version was 2.35.1 at the time of the creation of the tutorial. Furthermore, any further updates will be automatically downloaded and installed with the new PPA repository added.

GIT Configuration & Setup

The following parts will cover some typical setups and commands used daily by users of GIT.

GIT Add user

After installation, you must set up standard settings such as names and e-mails, mainly around git commit messages. This is pretty straightforward, as the tutorial will explain below.

The first step is to provide your name that will be set Globally.

git config --global user.name "YOUR NAME"

Next, select your e-mail; this can be fake if you prefer.

git config --global user.email "YOUR EMAIL"

GIT Create Directory

First, create a directory for users who want to make a new directory strictly for GIT.

mkdir example-directory -p

Next, navigate to the directory.

cd example-directory -p

The next task is to use the initialization command, and this will create a hidden .git directory to store the configuration, history, and so on.

git init

You will see a terminal output stating the status of the directory being initialized, and you can additionally see the content using the following command.

ls -a .git

Print GIT CONFIG Details

To confirm GIT config users and details, use the config –list command

git config --list

Unless specified, Git stores details in the ~/.gitconfig file. You can review what is currently stored by using the cat command.

cat ~/.gitconfig

The sudo command with the git config command will set two separate user names and e-mails.

Store GIT Credentials

For users who want to keep authorization details stored, you can enable the credential helper cache using the following.

git config --global credential.helper cache

If you must use credential helper, it is advised to cache only for a limited time for increased security. For example, you will be working today using GIT for 1 to 4 hours but won’t be touching it for maybe a few weeks, then set the expiry for 5 hours as follows.

git config --global credential.helper "cache --timeout=18000"

After 5 hours, the credentials will be deleted, which secures your GIT.

Check Directory GIT Status

To view the status of a GIT repository, you can use the following git status command.

git status

While the above command helps with giving a status of the GIT, you can additionally list all git commands and sub.

Connect Remote GIT Repository

For users that need to work with GIT remotes to sync and download/upload changes, you will need to link the GIT. This can be done using the git remote command as follows.

git remote add origin remote-repository-link

Commit GIT Changes

When you have completed changes in your GIT directory and want to SYNC it to push to the remote repository, use the following Git commit command.

git commit -m "git message changelog"

Note the -m “git message change” is the message that appears in the changelog.

Push GIT Changes

To push or send changes to the remote repository to SYNC in both versions, use the following git push command.

git push origin master

Pull GIT Changes

Alternatively, to pull or get changes from the remote repository to SYNC in both versions, use the following git push command.

git pull origin master

Update GIT on Ubuntu Linux

To grab updates for your Git version, just run the following command that will cover all installation methods as you installed Git with the APT package manager.

sudo apt update && sudo apt upgrade

This will ensure you are kept up-to-date with Git and other packages not marked on hold in your system.

Remove GIT on Ubuntu Linux

For users that no longer want GIT installed or would instead roll back to the default packages present in Ubuntu’s repository, first use the following command to remove Git.

sudo apt autoremove git -y

Next, remove the PPA using the same command as adding the PPA.

sudo rm /etc/apt/sources.list.d/git-core-ppa.list

Share to...