How to Install Git on CentOS Stream (10, 9)

Git is the distributed version control system that tracks changes across files, enabling collaboration and code history management for projects of any size. Whether you are a developer maintaining production codebases, a sysadmin versioning configuration files, or a contributor to open-source projects, Git provides the foundation for modern software workflows. This guide walks you through installing Git on CentOS Stream, configuring your identity, and performing essential operations from initializing repositories to synchronizing with remote servers.

Choose Your Git Installation Method

CentOS Stream provides Git through the default AppStream repository, offering a stable, tested version integrated with the system package manager. Source compilation provides an alternative when you need the absolute latest release or specific build options.

MethodChannelVersionUpdatesBest For
DNF Package ManagerCentOS AppStreamDistribution defaultAutomatic via dnf upgradeSystem administrators, production servers, general users
Source CompilationGitHub ReleasesLatest stableManual rebuild requiredDevelopers needing cutting-edge features or custom builds

This guide covers CentOS Stream 9 and CentOS Stream 10. Commands work identically on both versions. CentOS Stream 8 reached end-of-life in May 2024 and is no longer supported.

For most users, the DNF method is recommended because it provides automatic security updates, integrates with the system’s package management, and requires no maintenance. Source compilation is suitable when you specifically need features unavailable in the repository version or must pin a particular Git release.

Refresh System Packages

Before installing new software, refresh the package metadata and apply any pending updates. This ensures your system has current security patches and prevents potential dependency conflicts:

sudo dnf upgrade --refresh

The --refresh flag forces DNF to download fresh repository metadata even if the cache appears current. On a recently updated system, expect to see “Nothing to do” which confirms packages are current.

Install Git with DNF Package Manager

The simplest installation path uses the CentOS Stream AppStream repository. This method delivers a stable, tested version that receives security updates alongside your regular system maintenance. If git --version already returns a version number, Git is installed and you can proceed to configuration.

sudo dnf install git

DNF resolves dependencies automatically, pulling in Perl modules and supporting libraries. Press y when prompted to confirm the transaction.

Verify DNF Installation

Confirm Git is accessible by checking its version:

git --version

Expected output on CentOS Stream 9 or 10:

git version 2.47.3

The version number confirms Git is installed and ready for configuration. If you only need the repository version, skip the source compilation section and proceed to Configure Git Identity.

Install Git from Source

Compiling Git from source provides access to the latest features and security fixes before they reach the CentOS Stream repositories. This method requires additional setup but ensures you run the newest version with full control over build options.

Install Build Dependencies

Install the Development Tools group and required libraries. The Development Tools group provides GCC, make, autoconf, and other build essentials:

sudo dnf groupinstall "Development Tools"
sudo dnf install libcurl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-ExtUtils-MakeMaker wget

These dependencies enable Git’s HTTPS transport (libcurl-devel, openssl-devel), XML parsing for remote helpers (expat-devel), localization (gettext-devel), compression (zlib-devel), and Perl-based tools like git-svn (perl-ExtUtils-MakeMaker). The wget package handles the source download on minimal CentOS Stream images that omit it.

For the optional GUI tools gitk and git-gui, install tcl and tk. Without them, the build completes but displays a tclsh warning: sudo dnf install tcl tk

Download Latest Git Source

Download the latest stable Git release using automatic version detection. This command queries the GitHub API, extracts the newest stable tag, and downloads the corresponding archive:

cd ~
curl -s https://api.github.com/repos/git/git/tags | grep -oE '"name": "v[0-9]+\.[0-9]+\.[0-9]+"' | head -n 1 | cut -d '"' -f4 | xargs -I{} wget -q --show-progress https://github.com/git/git/archive/refs/tags/{}.tar.gz

This command chains several operations: curl fetches the tag list from GitHub’s API, grep extracts stable version tags (avoiding release candidates like v2.53.0-rc0), cut parses the JSON, and xargs passes the tag to wget for download. The pattern ensures you always get the latest stable release.

If you hit API rate limits or prefer a specific version, browse the GitHub releases page and download directly: wget https://github.com/git/git/archive/refs/tags/vX.Y.Z.tar.gz

Extract the downloaded archive using a wildcard pattern to handle any version:

tar -xzf v*.tar.gz

Enter the extracted source directory:

cd git-*/

Compile and Install

Compile Git with the /usr/local prefix, which installs binaries to /usr/local/bin where they take precedence over the system package. The -j$(nproc) flag uses all available CPU cores to accelerate compilation:

make prefix=/usr/local all -j$(nproc)

Compilation takes 2-5 minutes depending on your system. When complete, expect output similar to:

GIT_VERSION = 2.52.0
    GEN command-list.h
    CC hex.o
    ...
    LINK git
    GEN gitweb/gitweb.cgi

Install the compiled binaries. This step requires root privileges:

sudo make prefix=/usr/local install

Verify Source Installation

Confirm the newly compiled Git is accessible and reports the expected version:

git --version
git version 2.52.0

Verify your shell is using the source-compiled binary:

command -v git
/usr/local/bin/git

If command -v git shows /usr/bin/git instead, the DNF-installed version is taking precedence. See the troubleshooting section for PATH adjustment.

Clean Up Build Files

After successful installation, remove the source directory and archive to reclaim disk space:

cd ~
rm -rf git-*/ v*.tar.gz

Configure Git Identity

Git embeds your name and email in every commit you create. Configure these settings before making any commits. The --global flag applies to all repositories for your user account:

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

Git stores global configuration in ~/.gitconfig. For work on public repositories where you prefer privacy, use a pseudonym and a no-reply email provided by GitHub or GitLab.

Review your configuration:

git config --list
user.name=Your Name
user.email=your.email@example.com

Configure Default Branch Name

Modern projects commonly use main instead of master as the default branch. Configure Git to use main for new repositories:

git config --global init.defaultBranch main

Configure Credential Caching

When working with remote repositories over HTTPS, Git prompts for your username and personal access token (most providers no longer accept account passwords). Credential caching stores these credentials temporarily so you avoid re-entering them for every operation.

Enable credential caching with a 4-hour timeout:

git config --global credential.helper 'cache --timeout=14400'

After this duration, Git clears the cached credentials and prompts again on the next authenticated operation. For enhanced security on shared systems, consider SSH keys instead of HTTPS credentials.

Essential Git Operations

With Git installed and configured, you can perform the fundamental operations used in daily development work.

Initialize a Repository

Create a new directory and initialize it as a Git repository:

mkdir ~/my-project
cd ~/my-project
git init
Initialized empty Git repository in /home/user/my-project/.git/

Git creates a hidden .git directory containing the repository’s history and configuration. To rename an existing branch, see how to rename a local and remote Git branch.

Stage and Commit Changes

Stage files for commit using git add. To stage a specific file:

git add filename.txt

To stage all changed files in the current directory:

git add .

Create a commit with a descriptive message:

git commit -m "Add initial project files"

The -m flag provides the commit message inline. For longer messages, omit -m to open your default text editor. If you need to undo a commit, see how to undo the last Git commit.

Connect to a Remote Repository

Link your local repository to a remote server (GitHub, GitLab, Bitbucket, or self-hosted):

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

Replace the URL with your actual repository address. The name origin is a conventional alias for the primary remote. Verify the remote was added:

git remote -v
origin  https://github.com/username/repository.git (fetch)
origin  https://github.com/username/repository.git (push)

Push and Pull Changes

Push local commits to the remote repository:

git push origin main

Pull remote changes into your local repository:

git pull origin main

Replace main with your branch name if different. The first push may require -u to set the upstream tracking reference.

Update Source-Compiled Git

DNF-installed Git updates automatically with sudo dnf upgrade git. For source-compiled installations, you can repeat the manual build steps or use an update script that detects the latest version, rebuilds, and installs it.

This script includes safety checks:

  • Privilege check: refuses to run as root, using sudo only for the install step
  • Tool check: verifies curl, tar, make, and gcc are available
  • Version comparison: compares installed version with the latest stable tag

Create the update script:

sudo nano /usr/local/bin/update-git

Paste the following script content, then press Ctrl+O, Enter to save, and Ctrl+X to exit:

#!/bin/bash
set -e

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

# Refuse to run as root
if [ "$(id -u)" -eq 0 ]; then
    echo "Run this script as a regular user. Sudo is used only for installation."
    exit 1
fi

# Check required tools
for cmd in curl tar make gcc; do
    if ! command -v "$cmd" >/dev/null 2>&1; then
        echo "Error: $cmd is required but not installed."
        echo "Run: sudo dnf groupinstall \"Development Tools\""
        exit 1
    fi
done

mkdir -p "$BUILD_DIR"

# Get current and latest versions
CURRENT_VERSION=$($INSTALL_PREFIX/bin/git --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' || echo "none")
LATEST_TAG=$(curl -s https://api.github.com/repos/git/git/tags | grep -oE '"name": "v[0-9]+\.[0-9]+\.[0-9]+"' | head -n 1 | cut -d '"' -f4)

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

echo "Checking for Git updates..."
echo "Current version: ${CURRENT_VERSION:-not installed}"
echo "Latest version:  ${LATEST_TAG#v}"

if [ "v$CURRENT_VERSION" = "$LATEST_TAG" ]; then
    echo "Already up to date."
    exit 0
fi

echo ""
echo "Updating to $LATEST_TAG..."

cd "$BUILD_DIR"
rm -rf git-*/

echo "Downloading $LATEST_TAG..."
curl -fLO --progress-bar "https://github.com/git/git/archive/refs/tags/${LATEST_TAG}.tar.gz"

echo "Extracting source..."
tar -xzf "${LATEST_TAG}.tar.gz"
rm -f "${LATEST_TAG}.tar.gz"

cd "git-${LATEST_TAG#v}"

echo "Compiling (this takes several minutes)..."
make prefix="$INSTALL_PREFIX" all -j$(nproc)

echo "Installing..."
sudo make prefix="$INSTALL_PREFIX" install

NEW_VERSION=$($INSTALL_PREFIX/bin/git --version | grep -oE '[0-9]+\.[0-9]+\.[0-9]+')
echo ""
echo "Update complete."
echo "git version $NEW_VERSION"

Make the script executable:

sudo chmod +x /usr/local/bin/update-git

Run the update script from any directory:

update-git

Example output when already up to date:

Checking for Git updates...
Current version: 2.52.0
Latest version:  2.52.0
Already up to date.

Example output when an update is available:

Checking for Git updates...
Current version: 2.51.0
Latest version:  2.52.0

Updating to v2.52.0...
Downloading v2.52.0...
Extracting source...
Compiling (this takes several minutes)...
Installing...
[sudo] password for user: 

Update complete.
git version 2.52.0

Avoid automating this script with cron. Compilation can fail due to network issues, API changes, or missing dependencies. Always run the script manually to monitor output and address any problems.

Troubleshooting Common Git Issues

Source Build Shows Repository Version

If git --version shows the AppStream version after compiling from source, your shell is using /usr/bin/git instead of /usr/local/bin/git.

Check which binary is first in your PATH:

command -v git

If the output shows /usr/bin/git, add /usr/local/bin to the beginning of your PATH. Add this line to ~/.bashrc:

export PATH="/usr/local/bin:$PATH"

Apply the change immediately:

source ~/.bashrc

Alternatively, open a new terminal session. Verify the path and version:

command -v git
git --version
/usr/local/bin/git
git version 2.52.0

Compilation Fails With Missing Dependencies

If make fails with errors about missing headers or commands, install the corresponding package:

Error MessageMissing PackageFix Command
curl/curl.h: No such filelibcurl-develsudo dnf install libcurl-devel
expat.h: No such fileexpat-develsudo dnf install expat-devel
openssl/ssl.h: No such fileopenssl-develsudo dnf install openssl-devel
zlib.h: No such filezlib-develsudo dnf install zlib-devel
cmp: command not founddiffutilssudo dnf install diffutils

After installing missing packages, rerun the make command from the source directory.

HTTPS Clone Fails with Certificate Errors

If Git reports certificate verification failures when cloning over HTTPS, ensure the CA certificates package is installed:

sudo dnf install ca-certificates

For corporate environments with custom certificates, configure Git to use your organization’s CA bundle.

Remove Git from CentOS Stream

If you need to uninstall Git, the removal process differs based on installation method.

Remove DNF-Installed Git

Remove the Git package and clean up orphaned dependencies:

sudo dnf remove git
sudo dnf autoremove

Verify removal:

git --version
bash: git: command not found

Remove Source-Compiled Git

For source-compiled installations, manually remove the installed files from /usr/local:

The following commands permanently delete the source-compiled Git files. This does not affect your repositories or ~/.gitconfig. Skip this section if you installed Git via DNF.

sudo rm -f /usr/local/bin/git /usr/local/bin/git-* /usr/local/bin/gitk /usr/local/bin/scalar
sudo rm -rf /usr/local/libexec/git-core
sudo rm -rf /usr/local/share/git-core
sudo rm -rf /usr/local/share/git-gui
sudo rm -rf /usr/local/share/gitk
sudo rm -rf /usr/local/share/gitweb
sudo rm -rf /usr/local/share/bash-completion/completions/git
sudo rm -rf /usr/local/share/perl5/Git /usr/local/share/perl5/Git.pm

If you created the update script, remove it:

sudo rm -f /usr/local/bin/update-git

Verify removal:

git --version
bash: git: command not found

Next Steps With Git

You now have Git installed and configured on CentOS Stream, ready for version control workflows. With your identity set and the basics of repository management covered, explore renaming Git branches, undoing commits, and the official Git documentation to build on this foundation.

Leave a Comment

Let us know you are human: