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).
Matching infrastructure at centron
Ubuntu servers without your own hardware: ccloud³ VMs with full root access from €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →
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 --versionThe output should look like this:
git version 2.43.0Verify Git Functionality
$ gitIf 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/ppaUpdate the APT Package Index
$ sudo apt updateInstall Git
$ sudo apt install git -yTo install Git along with all additional tools, use the following command:
$ sudo apt install git-allInstalling 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 -yDownload 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.gzExtract the Downloaded Archive
After downloading, extract the contents of the Git archive.
$ tar -zxvf git-2.48.1.tar.gzMove 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 allInstall Git
Once compiled, install Git by executing the command below:
$ sudo make prefix=/usr/local installConfirm Git Installation
Verify that Git was installed correctly by checking its version.
$ git --versionUpon successful installation, the output will resemble the following:
git --versionConfiguring 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 --listTo make direct modifications to the Git configuration without using commands, open the .gitconfig file located in your home directory.
$ nano ~/.gitconfigUsing 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-projectMove into the Project Folder
Switch to the newly created directory.
$ cd demo-projectGenerate a Sample File
Create a text file containing an initial message.
$ echo "Greetings from centron, Git Works!!" > file.txtInitialize a Git Repository
Set up Git tracking in the project directory.
$ git initStage 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.txtCommit Changes
Record your modifications by creating a commit.
$ git commit -m "First Commit" -aCommit 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.txtView Commit History
Display the history of commits in your repository.
$ git logCheck Repository Status
Review the status of tracked and untracked files.
$ git statusConnecting 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 -vPush Local Changes to a Remote Repository
Upload all committed changes from your local repository to the remote repository's main branch.
$ git push origin mainEnsure 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 mainList All Available Branches
Check the existing branches in the current repository.
$ git branch -aUnderstanding 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-branchOutput:
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-branchCheck the Active Branch
View the currently active branch.
$ git branchOutput:
* demo-branch
master
Switch Back to the Master Branch
Return to the main branch (master) using:
$ git checkout masterOutput:
Switched to branch 'master'
Merge a Branch into Master
To integrate changes from demo-branch into the master branch, use:
$ git merge master --no-ffPush Merged Changes to Remote
Once merged, push the updated master branch to the remote repository.
$ git push originClone 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/nginxUpdating 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 --versionUpdate the APT Package Index
Refresh the package list to ensure you get the latest available software versions.
$ sudo apt updateInstall or Upgrade Git
Use the following command to install or update Git to the latest stable version.
$ sudo apt install git -yIf 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 --versionConclusion
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.
Testen Sie Ihr Setup auf ccloud³
Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.