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:

The output should resemble:

Check Git Help Page

Run the basic Git command to confirm the help interface displays correctly:

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

Install Git

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

The version output should look like:

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

Compile the Source Code

$ sudo make prefix=/usr/local all

Install Compiled Git

$ sudo make prefix=/usr/local install

Verify the Installed 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

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:

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:

Enter the Project Directory

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:

Stage Files for Commit

Use this command to add all files in the directory to the staging area:

Or add a specific file manually:

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

Check Repository Status

Use this to see changes, staged or untracked files:

Add Remote Repository

Link your repository to a remote server (replace the URL accordingly):

$ git remote add origin <remote-repository-url>

Verify Remotes

Push Local Changes to Remote

Push commits to the main branch:

Ensure you have permission to write to the remote before pushing.

Pull Updates from Remote

Display All Branches

Example output:

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

Expected output:

Return to Master Branch

Output:

Switched to branch 'master'

Merge Branches

Merge demo-branch into the current branch:

$ git merge master --no-ff

Push Your Changes

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

Update System Packages

Install the Updated Git Package

If the Git PPA is already added, this command installs the latest stable version available.

Verify the Upgrade

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.

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: