How to Install and Configure Git on Ubuntu 24.04
Git is an open-source, distributed version control system that helps developers track changes in their projects efficiently. It enables multiple contributors to collaborate seamlessly on a single codebase, maintaining a detailed history of all modifications. Git is widely used with platforms like GitHub, GitLab, and Bitbucket, which provide remote repository management and synchronization features.
This guide will walk you through the installation and configuration of Git on Ubuntu 24.04, ensuring you have the latest version and know how to set up both local and remote repositories.
Prerequisites
Before proceeding with the installation, ensure you have the following:
- Access to an Ubuntu 24.04 system.
- A user account with sudo privileges (non-root).
Checking the Pre-installed Git Version
Ubuntu 24.04 includes Git by default, but it may not be the most recent version. Before installing a new version, check the one already available on your system:
Check the Installed Git Version
$ git --version
The output should look like this:
git version 2.43.0
Verify Git Functionality
$ git
If Git is unavailable or outdated, follow the steps below to install the latest version.
Installing Git Using APT
Git can be installed using the default APT package manager on Ubuntu 24.04. Follow these steps to install the latest stable version:
Add the Git PPA Repository
$ sudo add-apt-repository ppa:git-core/ppa
Update the APT Package Index
$ sudo apt update
Install Git
$ sudo apt install git -y
To install Git along with all additional tools, use the following command:
$ sudo apt install git-all
Installing a Specific Git Version from Source
Using a specific version of Git ensures better compatibility and additional features for various applications and platforms like GitHub. Below are the step-by-step instructions for compiling and installing a particular Git version from the source code on Ubuntu 24.04.
Set Up Required Dependencies
Before compiling Git, install the necessary dependency packages.
$ sudo apt install libz-dev libssl-dev libcurl4-gnutls-dev libexpat1-dev gettext cmake gcc -y
Download the Git Source Code
Navigate to the official Git releases page and download the required .tar.gz
archive using wget
. For example, to download version 2.48.1, use the following command:
$ wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.48.1.tar.gz
Extract the Downloaded Archive
After downloading, extract the contents of the Git archive.
$ tar -zxvf git-2.48.1.tar.gz
Move into the Git Source Directory
Change into the extracted Git directory, ensuring the version matches the one you downloaded.
$ cd git-2.48.1/
Build Git from Source
Use the make
command to compile Git and install it in the /usr/local
directory.
$ sudo make prefix=/usr/local all
Install Git
Once compiled, install Git by executing the command below:
$ sudo make prefix=/usr/local install
Confirm Git Installation
Verify that Git was installed correctly by checking its version.
$ git --version
Upon successful installation, the output will resemble the following:
git --version
Configuring Git
After installation, configure Git with your personal details to ensure commits are properly attributed:
Set Your Git Username and Email
$ git config --global user.name "Your Name"
$ git config --global user.email "email@example.com"
Verify Git Configuration
$ git config --list
To make direct modifications to the Git configuration without using commands, open the .gitconfig
file located in your home directory.
$ nano ~/.gitconfig
Using Git on Ubuntu 24.04
Follow these step-by-step instructions to set up and use Git in your project environment.
Creating a Project Directory
Begin by setting up a new folder to organize your Git project, such as demo-project
.
$ mkdir demo-project
Move into the Project Folder
Switch to the newly created directory.
$ cd demo-project
Generate a Sample File
Create a text file containing an initial message.
$ echo "Greetings from centron, Git Works!!" > file.txt
Initialize a Git Repository
Set up Git tracking in the project directory.
$ git init
Stage Files for Commit
Add all project files to the staging area before committing.
$ git add .
Stage a Single File
Instead of staging all files, you can select a specific file.
$ git add file.txt
Commit Changes
Record your modifications by creating a commit.
$ git commit -m "First Commit" -a
Commit a Specific File Using Git
To commit only a particular file instead of all changes, use the following command:
$ git commit -m "Initial Commit" file.txt
View Commit History
Display the history of commits in your repository.
$ git log
Check Repository Status
Review the status of tracked and untracked files.
$ git status
Connecting a Local Repository to a Remote Git Repository
To link your local repository with a remote repository, add the remote repository and assign it the alias origin
.
$ git remote add origin
Check Configured Remote Repositories
List all configured remote repositories to confirm the connection.
$ git remote -v
Push Local Changes to a Remote Repository
Upload all committed changes from your local repository to the remote repository’s main branch.
$ git push origin main
Ensure you have write permissions to the remote repository before pushing changes.
Pull the Latest Updates from a Remote Repository
Retrieve and merge any new changes from the remote repository.
$ git pull origin main
List All Available Branches
Check the existing branches in the current repository.
$ git branch -a
Understanding Branch Listings
An asterisk (*
) next to a branch name indicates the currently active branch. The presence of /remotes/origin/master
shows that a branch named master
exists on the remote repository.
Create a New Git Branch
To create and switch to a new branch (e.g., demo-branch
), use the following command:
$ git checkout -b demo-branch
Output:
Switched to a new branch 'demo-branch'
Branches allow for parallel development environments, such as separate branches for development and production.
Switch to an Existing Git Branch
To move to an existing branch (e.g., demo-branch
), use:
$ git checkout demo-branch
Check the Active Branch
View the currently active branch.
$ git branch
Output:
* demo-branch master
Switch Back to the Master Branch
Return to the main branch (master
) using:
$ git checkout master
Output:
Switched to branch 'master'
Merge a Branch into Master
To integrate changes from demo-branch
into the master
branch, use:
$ git merge master --no-ff
Push Merged Changes to Remote
Once merged, push the updated master branch to the remote repository.
$ git push origin
Clone a Remote Repository
To create a local copy of a remote Git repository, use the git clone
command. For example, to clone the Nginx repository:
$ git clone https://github.com/nginx/nginx
Updating Git on Ubuntu 24.04
To upgrade Git to the latest available version, follow these steps using the APT package manager.
Check the Installed Git Version
Before updating, verify the currently installed version of Git.
$ git --version
Update the APT Package Index
Refresh the package list to ensure you get the latest available software versions.
$ sudo apt update
Install or Upgrade Git
Use the following command to install or update Git to the latest stable version.
$ sudo apt install git -y
If you have added the official Git PPA, the latest stable release will be installed.
Verify the Updated Git Version
After installation, confirm that Git has been successfully updated.
$ git --version
Conclusion
Congratulations! You have successfully installed and updated Git on Ubuntu 24.04. You can now manage repositories, track changes, and perform version control efficiently. For further configuration options and advanced usage, refer to the official Git documentation.