How to Install Git on Debian

Git is a distributed version control system that tracks changes in your codebase, enabling you to collaborate with other developers, revert to previous states, and manage multiple project branches simultaneously. Whether you need to contribute to open-source projects on GitHub, deploy applications to production servers, or maintain version history for personal projects, Git provides the foundation for modern development workflows. By the end of this guide, you will have Git installed and configured on your Debian system, ready to initialize repositories and sync with remote services.

Choose Your Git Installation Method

Debian provides Git through its default repositories, while compiling from source gives you access to the latest release. The following table summarizes both approaches to help you decide which fits your needs.

MethodChannelVersionUpdatesBest For
APT (Default Repository)Debian packagesDistribution defaultAutomatic via apt upgradeMost users who want stability and easy maintenance
Source CompilationGitHub source mirrorLatest stableManual recompilation requiredUsers who need cutting-edge features or specific build options

For most users, the APT method is recommended because it provides automatic security updates and requires no manual maintenance. However, compile from source if you specifically need features unavailable in the repository version.

Update Debian Before Git Installation

Before installing Git, refresh your package index so APT sees the latest package metadata:

sudo apt update

This keeps your package list current without forcing a full system upgrade.

Method 1: Install Git with APT

The APT method installs Git from Debian’s official repositories. Debian does not include Git by default on minimal or container installs, so you will add it with APT and receive security updates through normal package upgrades.

Install Git from Default Repository

Install Git using the APT package manager:

sudo apt install git

APT resolves all dependencies automatically and configures Git for immediate use.

Verify Git Installation

Next, confirm the installation succeeded by checking the version:

git --version

Expected output (Debian 13 example):

git version 2.47.3

Debian 11 and 12 report older versions (2.30.x and 2.39.x). Any version output confirms Git is installed correctly.

To update Git later without upgrading every package, run:

sudo apt update
sudo apt install --only-upgrade git

Method 2: Compile Git from Source

Alternatively, compiling from source provides the latest Git release with all new features. This method requires manual updates when new versions are released, but in return gives you complete control over the build configuration.

Install Build Dependencies

Git requires several development libraries, build tools, and download utilities. Install them with:

sudo apt install build-essential libssl-dev zlib1g-dev libcurl4-gnutls-dev libexpat1-dev gettext curl wget ca-certificates

Specifically, this installs the C compiler, make utility (included in build-essential), and development headers for SSL encryption, compression, HTTP transfers, and XML parsing that Git needs during compilation.

Download Git Source Code

Next, download the latest stable release. You can use an automated command that always fetches the current version, or alternatively download manually if you prefer a simpler approach:

curl -s https://api.github.com/repos/git/git/tags | grep -oP '"name": "v\K[0-9]+\.[0-9]+\.[0-9]+' | head -1 | xargs -I{} wget https://github.com/git/git/archive/refs/tags/v{}.tar.gz

This automated approach saves you from manually checking version numbers each time Git updates. Specifically, here is how it works:

  • curl -s: Silently fetches Git’s release tags from the GitHub API
  • grep -oP: Extracts version numbers using regex (e.g., 2.52.0)
  • head -1: Takes the first match (latest stable version)
  • xargs -I{}: Passes the version number to wget, constructing the download URL

If the piped command syntax seems confusing or the GitHub API is rate limited, download manually instead: visit the Git tags page, find the latest stable version tag, and run wget https://github.com/git/git/archive/refs/tags/vX.XX.X.tar.gz with the actual version number. The automated command above simply does this lookup step for you.

Extract the Source Archive

Once downloaded, extract the tarball:

tar -xvf v*.tar.gz

Then navigate into the extracted directory:

cd git-*/

Compile Git

Now compile the source code using make. The prefix=/usr/local option installs Git to /usr/local/bin, keeping it separate from system-managed packages:

make prefix=/usr/local all

Compilation takes a few minutes depending on your system. During this process, you will see compiler output scrolling as each component builds.

Install Compiled Git

After compilation completes, install the compiled binaries to your system:

sudo make prefix=/usr/local install

This command copies Git executables, documentation, and support files to /usr/local/.

Verify Source Installation

Finally, confirm the newly compiled version is active:

git --version

Expected output (example from the current tag):

git version 2.52.0

Your version should match the tag you downloaded. If it shows an older number, your shell may be finding the APT-installed Git first. Run which git to check the path; it should show /usr/local/bin/git for the source-compiled version.

Update Source-Compiled Git

Source builds do not update automatically. The script below checks your installed version, fetches the newest tag, and rebuilds only when an update is available.

If you skipped the build dependency step, install those packages before running the script.

Save the script as ~/git-build/update-git.sh:

#!/bin/bash
set -e

INSTALL_PREFIX="/usr/local"
BUILD_DIR="$HOME/git-build"

for cmd in curl wget make gcc tar; do
  if ! command -v $cmd &> /dev/null; then
    echo "Error: $cmd is required but not installed."
    echo "Run: sudo apt install build-essential curl wget ca-certificates"
    exit 1
  fi
done

CURRENT_VERSION=$($INSTALL_PREFIX/bin/git --version 2>/dev/null | grep -oP '[0-9]+\.[0-9]+\.[0-9]+' | head -1 || echo "none")
LATEST_VERSION=$(curl -s https://api.github.com/repos/git/git/tags | grep -oP '"name": "v\K[0-9]+\.[0-9]+\.[0-9]+' | head -1)

if [ -z "$LATEST_VERSION" ]; then
  echo "Error: Could not fetch the latest Git version."
  exit 1
fi

if [ "$CURRENT_VERSION" = "$LATEST_VERSION" ]; then
  echo "Git is already up to date: $CURRENT_VERSION"
  exit 0
fi

echo "Updating Git from $CURRENT_VERSION to $LATEST_VERSION"

mkdir -p "$BUILD_DIR"
cd "$BUILD_DIR"
rm -rf git-*
wget -q "https://github.com/git/git/archive/refs/tags/v$LATEST_VERSION.tar.gz"
tar -xvf "v$LATEST_VERSION.tar.gz"
cd git-*/
make prefix="$INSTALL_PREFIX" all
sudo make prefix="$INSTALL_PREFIX" install

Make the script executable and run it:

chmod +x ~/git-build/update-git.sh
~/git-build/update-git.sh

Avoid cron automation: Compilation can fail due to missing dependencies or network issues. Run the script manually so you can review the output and fix problems before they affect your system.

Verify the updated version:

git --version
git version 2.52.0

Your output should match the latest tag you just built.

Configure Git After Installation

After installing Git, you need to configure your identity. Git attaches your name and email to every commit, which is required before you can commit changes.

Set Your Name and Email

Configure your Git identity with these commands, replacing the placeholders with your actual information:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

The --global flag applies these settings to all repositories on your system. Alternatively, omit it to configure settings for a single repository only.

Set Default Branch Name

Modern Git repositories typically use main as the default branch name instead of master. Therefore, configure this preference:

git config --global init.defaultBranch main

As a result, new repositories you create will use main as the initial branch, matching GitHub and GitLab defaults.

If you need to rename an existing local and remote branch, follow our guide to rename a local and remote Git branch.

Verify Configuration

To verify your settings, review your Git configuration:

git config --list

Expected output:

user.name=Your Name
user.email=your.email@example.com
init.defaultbranch=main

Your output will include additional settings, but these three lines confirm your identity and default branch are set. Alternatively, you can view the configuration file directly:

cat ~/.gitconfig

Essential Git Commands

With Git installed and configured, here are the fundamental commands for daily use.

Initialize a New Repository

Create a new Git repository in a project directory:

mkdir my-project
cd my-project
git init

As a result, this creates a hidden .git directory that stores all version history and configuration for the repository.

Check Repository Status

View the current state of your working directory and staging area:

git status

This output shows which files are modified, staged for commit, or untracked.

Stage and Commit Changes

Add files to the staging area and create a commit:

git add .
git commit -m "Initial commit"

Here, the git add . command stages all changes in the current directory, while the -m flag provides a commit message describing your changes.

If you need to undo a recent commit, see how to undo the last Git commit.

Connect to a Remote Repository

Link your local repository to a remote service like GitHub or GitLab:

git remote add origin https://github.com/username/repository.git

Replace the URL with your actual repository address. The name origin is the conventional name for the primary remote.

Push and Pull Changes

Upload your commits to the remote repository:

git push -u origin main

The -u flag sets up tracking, so consequently future pushes only require git push.

Similarly, download changes from the remote repository:

git pull origin main

This fetches remote changes and merges them into your current branch.

Cache Credentials

Avoid re-entering credentials for HTTPS remotes by enabling credential caching:

git config --global credential.helper "cache --timeout=18000"

This stores credentials in memory for 5 hours (18000 seconds). For long-term authentication, consider using SSH keys instead of HTTPS credentials.

Troubleshoot Common Git Issues

The following solutions address problems you may encounter when using Git on Debian.

SSL Certificate Errors

If Git fails to connect to HTTPS repositories, you may see an error like this:

fatal: unable to access 'https://github.com/user/repo.git/': SSL certificate problem: unable to get local issuer certificate

This typically indicates outdated or missing CA certificates. Reinstall them to fix the issue:

sudo apt install --reinstall ca-certificates

Additionally, for corporate networks with custom certificates, you may need to add your organization’s CA to the system trust store.

Permission Denied Errors

When pushing or pulling over SSH, you may encounter this error:

git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

This means your SSH key is not configured or not loaded. Test your SSH authentication:

ssh -T git@github.com

Expected output from GitHub:

Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.

GitLab and other hosts return different success messages, so any greeting from the host confirms your key is working.

If authentication fails, you need to generate an SSH key and add it to your GitHub/GitLab account. See the SSH installation guide for Debian for detailed instructions.

If you encounter permission errors on local repository files owned by root, fix the ownership:

sudo chown -R $(whoami):$(whoami) .git

Verify the ownership change:

ls -la .git

You should see your username as the owner instead of root.

Wrong Git Version After Source Install

If git --version shows the old APT version after compiling from source, your shell PATH may prioritize /usr/bin over /usr/local/bin. Check the active Git path:

which git

If it shows /usr/bin/git, either remove the APT package (sudo apt remove git) or add /usr/local/bin to the front of your PATH. To make this change permanent, append the export to your shell configuration file:

echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.bashrc

Subsequently, reload your shell configuration to apply the change immediately:

source ~/.bashrc

Alternatively, open a new terminal window for the change to take effect automatically.

Now verify that the correct Git version is active:

git --version

The output should show your source-compiled version instead of the APT version.

Remove Git from Debian

If you need to uninstall Git, the removal process depends on how you installed it.

Remove APT-Installed Git

Remove Git and its unused dependencies:

sudo apt remove git
sudo apt autoremove

Verify removal:

git --version

Expected output:

bash: git: command not found

Your shell may show a slightly different “command not found” message, which is still expected.

Remove Source-Compiled Git

Source builds install to /usr/local, and Git does not ship an uninstall target. Remove the source-compiled files manually:

Warning: The following commands permanently delete the source-compiled Git binaries and support files from /usr/local. If you want a backup, copy /usr/local/bin/git and /usr/local/libexec/git-core to a safe location first.

Remove the source-compiled files:

sudo rm -rf /usr/local/bin/git*
sudo rm -rf /usr/local/libexec/git-core
sudo rm -rf /usr/local/share/git-core

If you used the update script, you can also remove the build directory:

rm -rf ~/git-build

Remove Git Configuration Files

The following commands permanently delete your Git configuration, including saved credentials and aliases. Back up ~/.gitconfig first if you want to preserve these settings.

Remove user-level Git configuration:

rm -rf ~/.gitconfig ~/.git-credentials

Conclusion

You now have Git installed on Debian with your identity configured and the default branch set to main. The APT method provides automatic updates for stable usage, while source compilation gives you access to the latest features. From here, you can explore GitHub Desktop on Debian for a graphical interface, or alternatively set up GitLab on Debian to host your own repositories.

Leave a Comment

Let us know you are human: