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.
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 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 --versionThe output should resemble:
git version 2.43.0Check Git Help Page
Run the basic Git command to confirm the help interface displays correctly:
$ gitYou 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/ppaUpdate APT Cache
$ sudo apt updateInstall Git
$ sudo apt install git -yThis 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-allConfirm the Git Version
$ git --versionThe version output should look like:
git version 2.48.1Installing 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 -yDownload 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.gzExtract the Source Files
$ tar -zxvf git-2.48.1.tar.gzNavigate to the Extracted Directory
$ cd git-2.48.1/Compile the Source Code
$ sudo make prefix=/usr/local allInstall Compiled Git
$ sudo make prefix=/usr/local installVerify the Installed Version
$ git --versionConfiguring 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 --listYou should see output like:
user.name=Your Full Name
user.email=email@example.comManually Edit Git Configuration
If desired, you can open the configuration file and make edits directly:
$ nano ~/.gitconfigHow 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-projectEnter the Project Directory
$ cd demo-projectCreate a Sample File
Create a simple text file using the echo command:
$ echo "Greetings from centron, Git Works!!" > file.txtInitialize a Git Repository
Set up a new Git repository in the current folder:
$ git initStage 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.txtCreate a Git Commit
To create a commit with all added or changed files:
$ git commit -m "First Commit" -aThe -a flag commits all changes automatically. Output may look like:
1 file changed, 1 insertion(+)
create mode 100644 file.txtCommit Specific Files
To commit individual files:
$ git commit -m "Initial Commit" file.txtReview Commit History
$ git logCheck Repository Status
Use this to see changes, staged or untracked files:
$ git statusAdd Remote Repository
Link your repository to a remote server (replace the URL accordingly):
$ git remote add origin <remote-repository-url>Verify Remotes
$ git remote -vPush Local Changes to Remote
Push commits to the main branch:
$ git push origin mainEnsure you have permission to write to the remote before pushing.
Pull Updates from Remote
$ git pull origin mainDisplay All Branches
$ git branch -aExample output:
* masterThe 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-branchExpected output:
Switched to a new branch 'demo-branch'Branches help separate environments—useful for development vs. production workflows.
Switch Between Branches
$ git checkout demo-branchCheck the Current Branch
$ git branchExpected output:
* demo-branch
masterReturn to Master Branch
$ git checkout masterOutput:
Switched to branch 'master'Merge Branches
Merge demo-branch into the current branch:
$ git merge master --no-ffPush Your Changes
$ git push originClone a Repository
Download a remote Git repo to your local machine:
$ git clone https://github.com/nginx/nginxUpgrade Git on Ubuntu 24.04
You can upgrade your Git installation to the most recent version using APT.
Check the Current Git Version
$ git --versionUpdate System Packages
$ sudo apt updateInstall the Updated Git Package
$ sudo apt install git -yIf the Git PPA is already added, this command installs the latest stable version available.
Verify the Upgrade
$ git --versionConclusion
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.
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.