How To Install and Set Up Git on Ubuntu

July 10, 2024

Introduction

Git is a version control system used in modern software development. It allows multiple developers to work on the same project and track changes and different revisions. The system seamlessly integrates with Ubuntu and enhances collaboration and efficient code management.

This step-by-step guide walks you through installing and configuring Git on Ubuntu.

How to install and set up Git on Ubuntu - a tutorial.

Prerequisites

  • A user account with sudo or root privileges.
  • A machine running Ubuntu (20.04, 22.04, or 24.04).
  • Access to a command line/terminal window (Ctrl+Alt+T).

Install Git on Ubuntu

There are two ways to install Git on Ubuntu:

  • With apt, from the official Ubuntu repository. The official Ubuntu package manager offers a straightforward installation process, but the version in the repository may not be the latest.
  • From the source code. A more complicated installation process, but it offers the latest version with the newest features and bug fixes.

The sections below show both methods for installing Git on Ubuntu. Choose the method most suitable for your needs and follow the steps.

Method 1: Install Git with apt on Ubuntu

Using the apt package management tool is the easiest way to install Git. However, the version in the default repository is often not the latest release. This is because Ubuntu prioritizes stability and thorough testing over cutting-edge updates to ensure that software in the official repositories is reliable and secure for users.

If you want to install the latest release, scroll down to read how to install it from the source. To install Git with apt, follow these steps:

1. Update the system package index with the following command:

sudo apt update

Updating the package index ensures you are working with the latest software versions.

2. Install Git from the default Ubuntu repository by running:

sudo apt install git -y
Installing Git on Ubuntu using the apt package manager.

Allow the process to complete.

3. Verify the installation and version by running:

git --version
Checking the Git program version after installation.

The output states the program version if it has been installed correctly.

Method 2: Install Git on Ubuntu From Source Code

To set up the latest version of Git on Ubuntu, download and install it from the source code. Follow the steps below:

1. Start by installing the required dependencies for Git on Ubuntu. Run:

sudo apt install make libssl-dev libghc-zlib-dev libcurl4-gnutls-dev libexpat1-dev gettext unzip -y

Wait for the installation to complete.

2. Create a new tmp directory and move to that directory:

mkdir tmp
cd /tmp

3. Open a browser window and navigate to the following address:

https://mirrors.edge.kernel.org/pub/software/scm/git/

From the list of Git releases, find the version number for the latest release (or another specific version you want to install) and enter the version number in the following command:

curl -o git.tar.gz https://mirrors.edge.kernel.org/pub/software/scm/git/git-X.X.X.tar.gz

For example, the latest version at the time of writing this article is 2.9.5. We will use the curl command to download Git version 2.9.5 and rename the downloaded file to git.tar.gz:

curl -o git.tar.gz https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.9.5.tar.gz
Downloading the latest Git version with curl.

The command downloads the Git tar file.

4. Uncompress the tarball file by running:

tar -zxf git.tar.gz

5. Move to the new directory:

cd git-*

6. Compile the package by running the following command:

make prefix=/usr/local/
Compiling the Git source code.

7. Once the process completes, install Git by running:

sudo make prefix=/usr/local install
Installing Git from the source code on Ubuntu.

8. Verify the Git installation by running:

git --version
Checking Git version after installation.

Set up Git on Ubuntu

Git contains a basic configuration file that holds crucial information. Setting your username and email address is essential.

1. In a terminal window, enter this command and replace your_name with your name and [email protected] with your email address:

git config --global user.name "your_name"
git config --global user.email "[email protected]"

2. Verify configuration changes with the command:

git config --list

The system should display the name and email address you just entered.

Configuring the global Git settings.

Note: Not configuring the settings results in a warning when making a commit, which makes you go back and revise your commits.

Starting with Basic Git Commands on Ubuntu

The following is a list of useful Git commands to help you get started:

  • Find the changed files in the working directory: git status
  • Compare codebase states: git diff
  • Add changes to your next commit: git add
  • Add selected changes into your next commit: git add -p
  • Change the last commit: git commit -amend
  • Commit all local changes in tracked files: git commit -a
  • Commit previously staged changes: git commit
  • Rename a Local branch: git branch -m new-name
  • List all currently configured remotes: git remote -v
  • View information about a remote: git remote show
  • Add a new remote repository: git remote add
  • Delete a remote repository: git remote remove [remote name]
  • Download all changes from the remote repository: git fetch
  • Download all changes from the remote repository and merge into HEAD: git pull branch
  • Create a new branch with the command: git branch [branch_name]

To see more Git commands use git --help, or download and refer to our Git commands cheat sheet for free to have the most commonly used commands always at hand.

Conclusion

Now you know how to install and configure Git on your Ubuntu system. Use Git for efficient version control, collaboration, and to track changes in software development projects.

For CentOS, Windows, or macOS systems, see our guides on installing Git on Windows and installing Git on MacOS.

Was this article helpful?
YesNo
Sofija Simic
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
Next you should read
How to Install and Use Git on Windows
April 18, 2024

Git tracks source code changes during the software development process. It can help coordinate work among...
Read more
How To Rename a Local and Remote Git Branch
April 11, 2024

Git is a version control system that helps you control the stages of software development. It uses named...
Read more
How to Create a New Branch in Git
January 9, 2024

This article outlines the basic commands needed to create a Git branch. A Git branch allows you to work on...
Read more
How to Install Git on CentOS 7 With Yum or Latest Repository
March 15, 2019

In this tutorial, learn how to install Git on CentOS 7. Git is a distributed version control system to track...
Read more