How to Install Git on Ubuntu 24.04

Git is an open-source distributed version control system used to track changes in projects and during software development. Git allows multiple contributors to simultaneously work on a single project, maintain and track the history of all changes. It enables efficient collaboration and allows developers to synchronize changes using Git-compatible platforms such as GitHub, GitLab, and Bitbucket.
This article explains how to install and configure Git on Ubuntu 24.04. You will install the latest Git version and configure it to use local and remote repositories in your project environment.
Prerequisites
Before you begin, you need to:
- Have access to an Ubuntu 24.04 instance to install Git as a non-root sudo user.
Verify the Default Git Version
Git is available by default on Ubuntu 24.04, but the default version may be outdated and not the latest. Follow the steps below to test the default Git version that's pre-installed on your Ubuntu 24.04 server before installing another version.
View the default installed Git version.
console$ git --version
Your output should be similar to the one below.
git version 2.43.0
Run the Git command and verify that the default help page displays.
console$ git
Output:
usage: git [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>] [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path] [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare] [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>] [--config-env=<name>=<envvar>] <command> [<args>] These are common Git commands used in various situations: ........
If Git is unavailable or outdated on your Ubuntu 24.04 workstation. Follow the steps below to install the latest Git version on your instance.
Install Git Using APT
Git is available in the default APT package repositories on Ubuntu 24.04. Follow the steps below to add the Git PPA with the latest package information, and install the latest Git version.
Add the Git PPA to the APT package repository sources.
console$ sudo add-apt-repository ppa:git-core/ppa
Update the APT package index.
console$ sudo apt update
Install Git.
console$ sudo apt install git -y
The above command installs the latest Git version and replaces the default version installed on your system.
To install Git and all extra tools, install the
git-all
package instead.console$ sudo apt install git-all
Verify the installed Git version.
console$ git --version
Your output should be similar to the one below.
git version 2.48.1
Install a Specific Git Version From Source Code
Specific Git versions offer enhanced features and compatibility options you can use with applications or platforms like GitHub. Follow the steps below to install the latest Git version from source code or a specific version on Ubuntu 24.04.
Install all required dependency packages for compiling Git.
console$ sudo apt install libz-dev libssl-dev libcurl4-gnutls-dev libexpat1-dev gettext cmake gcc -y
Visit the Git releases page and use
wget
to download your target.tar.gz
Git source code archive depending on the version. For example,git-2.48.1.tar.gz
console$ wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.48.1.tar.gz
Extract files from the archive.
console$ tar -zxvf git-2.48.1.tar.gz
Navigate to the Git directory depending on the downloaded version.
console$ cd git-2.48.1/
Compile the source code to the
/usr/local
directory using Make.console$ sudo make prefix=/usr/local all
Install the compiled Git package.
console$ sudo make prefix=/usr/local install
Test the installed Git version.
console$ git --version
Your version output should be similar to the one below.
git --version
Configure Git
Git requires a valid username and email to include on commit messages in your project. Follow the steps below to configure Git using the git config
command with your default account information.
Add a global username to use with Git.
console$ git config --global user.name "Your Full Name"
Add a global email to use with Git.
console$ git config --global user.email "email@example.com"
Test the Git configuration and verify that your account information is available.
console$ git config --list
Output:
user.name=Your Full Name user.email=email@example.com
Open the
.gitconfig
file in your user's home directory to modify the Git configuration directly without using commands.console$ nano ~/.gitconfig
Access and Use Git on Ubuntu 24.04
Follow the steps below to access and use Git in your project environment.
Create a new project directory such as
demo-project
.console$ mkdir demo-project
Navigate to the
demo-project
directory.console$ cd demo-project
Create a new sample file such as
file.txt
.console$ echo "Greetings from Vultr, Git Works!!" > file.txt
Initialize a new repository in the directory using Git.
console$ git init
Add all files in the directory to staging using Git.
console$ git add .
Or specify a specific file to stage using Git.
console$ git add file.txt
Create a new commit using Git with all new or updated files.
console$ git commit -m "First Commit" -a
The
-a
option includes all new and modified files in the commit.Output:
1 file changed, 1 insertion(+) create mode 100644 file.txt
Use the following command to commit specific files using Git.
console$ git commit -m "Initial Commit" file.txt
View the Git commit history.
console$ git log
Check the repository status to test all changes, and untracked files in the current branch.
console$ git status
Add a new remote repository and save it as
origin
to use with Git.console$ git remote add origin <remote-repository-url>
Verify all available remotes.
console$ git remote -v
Push all local changes to the remote repository.
console$ git push origin main
The above command pushes all changes to the remote repository's main branch. Verify that you have write privileges to the remote repository before pushing changes using Git.
Pull the latest changes from the remote repository.
console$ git pull origin main
View all branches in the active project directory.
console$ git branch -a
Output:
* master
The
*
asterisk symbol confirms the active branch while the/remotes/origin/master
path indicates that a single branchmaster
is available on the remoteorigin
Create a new branch such as
demo-branch
using Git.console$ git checkout -b demo-branch
Output:
Switched to a new branch 'demo-branch'
Creating branches in Git allows you to separate environments in a project. For example, you can create a separate development and production branch to push different repository changes.
Switch to the branch.
console$ git checkout demo-branch
Check the active branch and verify that it's
demo-branch
.console$ git branch
Output:
* demo-branch master
Switch back to the
master
branch.console$ git checkout master
Output:
Switched to branch 'master'
Use the
git merge
command to merge branches. For example, merge thedemo-branch
to themaster
branch.console$ git merge master --no-ff
Push changes to the remote repository.
console$ git push origin
Use the
git clone
command to clone a remote repository to a directory using Git. For example, clone the Nginx webserver repository.console$ git clone https://github.com/nginx/nginx
Update Git
You can upgrade Git from the active version to a new or latest version on your Ubuntu 24.04. Follow the steps below to upgrade Git using the APT package manager.
Test the active Git version.
console$ git --version
Update the APT package index.
console$ sudo apt update
Install the Git package to automatically update the installed version.
console$ sudo apt install git -y
The above command installs the latest Git version available in the APT repository sources. The latest stable version is installed if you added the Git PPA to your repository sources.
Test the installed Git version and verify that it's updated.
console$ git --version
Conclusion
You have installed and configured Git on Ubuntu 24.04, staged files and added repositories to use with Git. You can install the latest Git version using the official PPA source and use Git to perform version control tasks on your Ubuntu 24.04 instance. For more information and configuration options, visit the Git documentation.