How do I install the latest version of Git with apt?

I read about a security flaw in Git, which was fixed in version 2.2.1. I currently have Git 2.1.0 on my system (Ubuntu 14.10), and tried to reinstall it with apt. However, apt told me that I currently have the latest version.

The Git website does not have prebuilt versions for Linux. They say that you can install it with package managers. Without building from source, how would I install the latest version of Git?


Use the PPA from the maintainers of git on Ubuntu:

sudo apt-add-repository ppa:git-core/ppa
sudo apt-get update
sudo apt-get install git

If you don't know what PPAs are, first read What are PPAs and how do I use them?

If you receive an error about add-apt-repository command not found you need to install software-properties-common, and then redo the above steps.

sudo apt-get install software-properties-common

This what i did to upgrade git 1.7.9.5 to 2.xxx on Ubuntu 12.04:

sudo apt-get install python-software-properties
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:git-core/ppa -y
sudo apt-get update
sudo apt-get install git -y
git --version

The most common situation is when you want to install the latest version of git, but your Operating System's repositories are not updated. For example, in my case I have a laptop running Ubuntu 20.04, and when I executed the command sudo apt install git the installed version was 2.25.1; instead of 2.32.0 which is the current version at git-scm.com.

How can I get the latest version?

Well, we can install it by following one of these methods: Using APT Reprositories, Building and Installing or Using binary files.

A. Building and Installing (recommended for developers)

A-1. Uninstall the default version provided by Ubuntu's package manager and configuration by using:

sudo apt remove --purge --auto-remove -y git

or:

sudo apt purge --auto-remove -y git

A-2. Go to the official CMake webpage, then download and extract the latest version. Update the version and build variables in the following command to get the desired version:

version=2.32
build=0
mkdir ~/temp
cd ~/temp
wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-$version.$build.tar.gz
tar -xzvf git-$version.$build.tar.gz
cd git-$version.$build/

A-3. Install the extracted source by running:

make -j$(nproc) prefix=/usr/local all
sudo make prefix=/usr/local install

A-4. Test your new git version.

git --version

Results of git --version:

git version 2.25.X

B. Using PPA Repositories (recommended for normal users)

There is a PPA available from Ubuntu Git Maintainers team that we can use to easily install the latest stable Git version. So we can install it easily following these steps:

B-1. Uninstall the default version provided by Ubuntu's package manager as in A-1.

B-2. Add PPA repository to your sources list.

sudo add-apt-repository ppa:git-core/ppa

B-3. Finally we can update and install the git package.

sudo apt update
sudo apt install git

B-4. Test your new git version as in A-4.

Note

In 2.32.X the X represents the last part of the version that we defined as build. The build may change if git is updated. According to the official web page the Latest Release is 2.32.0. If you want the Previous Release 2.31.1 just replace the version and build parameters like this:

version=2.31
build=1
mkdir ~/temp
cd ~/temp
wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-git-$version.$build.tar.gz
tar -xzvf git-$version.$build.tar.gz
cd git-$version.$build/