How to Install and Configure Git on Ubuntu 24.04
Git is a widely-used open-source distributed version control system designed to manage and record project modifications, particularly throughout software development workflows. It empowers multiple developers to simultaneously contribute to the same codebase while preserving a full history of changes. Through platforms such as GitHub, GitLab, and Bitbucket, Git facilitates seamless collaboration and synchronization.
This tutorial walks you through the process of installing the latest Git release and setting it up for both local and remote repository use within your development environment on Ubuntu 24.04.
Prerequisites
Ensure the following before proceeding:
- You must have access to an Ubuntu 24.04 system and the ability to run commands as a non-root user with sudo privileges.
Checking the Default Git Version on Ubuntu 24.04
Ubuntu 24.04 comes with Git pre-installed, but it may not be the most recent release. Use the steps below to check the existing version before deciding to upgrade.
Display the Pre-installed Git Version
To see which version of Git is already present:
$ git --version
The output should resemble:
git version 2.43.0
Check Git Help Page
Run the basic Git command to confirm the help interface displays correctly:
$ git
You should see output like:
usage: git [-v | --version] [-h | --help] [-C ] [-c =]
[--exec-path[=]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=] [--work-tree=] [--namespace=]
[--config-env==] []
These are common Git commands used in various situations:
........
If Git is missing or the installed version is outdated, continue with the instructions below to upgrade to the latest version.
Installing Git via APT on Ubuntu 24.04
Git is part of Ubuntu’s default APT repositories. Use these steps to add the Git PPA and install the most current version:
Add the Official Git PPA
$ sudo add-apt-repository ppa:git-core/ppa
Update APT Cache
$ sudo apt update
Install Git
$ sudo apt install git -y
This command installs the newest Git version, overwriting the default installation.
Install the Complete Git Toolkit
To include all auxiliary tools, use the following command:
$ sudo apt install git-all
Confirm the Git Version
$ git --version
The version output should look like:
git version 2.48.1
Installing a Specific Git Version from Source
In certain scenarios, it’s beneficial to compile a specific Git version for added compatibility. Follow the instructions below to install Git from source on Ubuntu 24.04.
Install Required Dependencies
$ sudo apt install libz-dev libssl-dev libcurl4-gnutls-dev libexpat1-dev gettext cmake gcc -y
Download Git Source Archive
Navigate to the Git releases page and download the version you want using wget, e.g., 2.48.1:
$ wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.48.1.tar.gz
Extract the Source Files
$ tar -zxvf git-2.48.1.tar.gz
Navigate to the Extracted Directory
$ cd git-2.48.1/
Compile the Source Code
$ sudo make prefix=/usr/local all
Install Compiled Git
$ sudo make prefix=/usr/local install
Verify the Installed Version
$ git --version
Configuring Git
Before making commits, Git must be configured with your name and email. This ensures all commits are correctly attributed.
Set Global Git Username
$ git config --global user.name "Your Full Name"
Set Global Git Email
$ git config --global user.email "email@example.com"
Check Current Git Settings
$ git config --list
You should see output like:
user.name=Your Full Name
user.email=email@example.com
Manually Edit Git Configuration
If desired, you can open the configuration file and make edits directly:
$ nano ~/.gitconfig
How to Use Git on Ubuntu 24.04
To begin using Git in your local development environment on Ubuntu 24.04, follow the steps below.
Create a Project Directory
Start by setting up a new folder for your project, such as demo-project
:
$ mkdir demo-project
Enter the Project Directory
$ cd demo-project
Create a Sample File
Create a simple text file using the echo
command:
$ echo "Greetings from centron, Git Works!!" > file.txt
Initialize a Git Repository
Set up a new Git repository in the current folder:
$ git init
Stage Files for Commit
Use this command to add all files in the directory to the staging area:
$ git add .
Or add a specific file manually:
$ git add file.txt
Create a Git Commit
To create a commit with all added or changed files:
$ git commit -m "First Commit" -a
The -a
flag commits all changes automatically. Output may look like:
1 file changed, 1 insertion(+)
create mode 100644 file.txt
Commit Specific Files
To commit individual files:
$ git commit -m "Initial Commit" file.txt
Review Commit History
$ git log
Check Repository Status
Use this to see changes, staged or untracked files:
$ git status
Add Remote Repository
Link your repository to a remote server (replace the URL accordingly):
$ git remote add origin <remote-repository-url>
Verify Remotes
$ git remote -v
Push Local Changes to Remote
Push commits to the main
branch:
$ git push origin main
Ensure you have permission to write to the remote before pushing.
Pull Updates from Remote
$ git pull origin main
Display All Branches
$ git branch -a
Example output:
* master
The asterisk marks the current active branch. /remotes/origin/master
shows a single master branch is available remotely.
Create a New Branch
Use the following to create a branch named demo-branch
:
$ git checkout -b demo-branch
Expected output:
Switched to a new branch 'demo-branch'
Branches help separate environments—useful for development vs. production workflows.
Switch Between Branches
$ git checkout demo-branch
Check the Current Branch
$ git branch
Expected output:
* demo-branch
master
Return to Master Branch
$ git checkout master
Output:
Switched to branch 'master'
Merge Branches
Merge demo-branch
into the current branch:
$ git merge master --no-ff
Push Your Changes
$ git push origin
Clone a Repository
Download a remote Git repo to your local machine:
$ git clone https://github.com/nginx/nginx
Upgrade Git on Ubuntu 24.04
You can upgrade your Git installation to the most recent version using APT.
Check the Current Git Version
$ git --version
Update System Packages
$ sudo apt update
Install the Updated Git Package
$ sudo apt install git -y
If the Git PPA is already added, this command installs the latest stable version available.
Verify the Upgrade
$ git --version
Conclusion
You’ve successfully installed Git on Ubuntu 24.04, configured your environment, and worked with repositories and commits. Using the official Git PPA ensures you have access to the latest version. Refer to the Git documentation for further setup and advanced options.