How to Install Git on Rocky Linux

Git is the distributed version control system that powers modern software development. Whether you need to track changes in your projects, collaborate with a team, or contribute to open-source software, Git provides the tools to manage code history efficiently. By the end of this guide, you will have Git installed on Rocky Linux, configured with your identity, and ready for version control workflows including repository initialization, commits, and remote synchronization.

These instructions apply to Rocky Linux 8, 9, and 10. The DNF installation method works identically across all versions, though the repository Git version differs: 2.43.x on Rocky Linux 8 and 2.47.x on Rocky Linux 9 and 10. Source compilation steps are consistent across all releases.

Choose Your Git Installation Method

Rocky Linux offers two approaches for installing Git: the DNF package manager for simplicity or source compilation for the absolute latest version. The table below summarizes each method to help you decide which fits your workflow.

MethodChannelVersionUpdatesBest For
DNF Package ManagerRocky AppStreamDistribution default (2.43-2.47)Automatic via dnf upgradeMost users who want stable, tested packages
Source CompilationGitHub MirrorLatest stable (currently 2.52.0)Manual recompilation requiredDevelopers who need the latest features

For most users, the DNF method is recommended because it provides automatic security updates and integrates with the system package manager. Only compile from source if you specifically need features unavailable in the repository version or require a particular Git release for compatibility reasons.

Update Your System Before Installation

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

sudo dnf upgrade --refresh

If updates are available, DNF will display a summary and prompt for confirmation. On a recently updated system, you may see “Nothing to do” which confirms your packages are current. For systems with slow mirror speeds, see how to increase DNF speed on Rocky Linux.

Install Git with DNF Package Manager

The simplest way to install Git on Rocky Linux is through the default AppStream repository. Git is not pre-installed on minimal Rocky Linux installations but may already be present on desktop environments. This method provides a stable, tested version that receives security updates automatically when you run system upgrades.

sudo dnf install git

DNF will resolve dependencies and display the packages to be installed. Press y when prompted to confirm the installation.

Verify the Installation

After installation completes, confirm Git is accessible by checking its version:

git --version

Expected output on Rocky Linux 10 or 9:

git version 2.47.3

On Rocky Linux 8, the version will be slightly older:

git version 2.43.7

If the command returns a version number, Git is installed and ready for configuration. You can skip to the Configure Git section below.

Install Git from Source Code

Compiling Git from source gives you access to the latest features and security fixes before they reach the Rocky Linux repositories. This method requires more effort but is useful when you need a specific version or want capabilities not yet available in packaged releases.

Install Build Dependencies

First, install the development tools group and additional libraries required for compilation. The Development Tools group provides essential build utilities like gcc, make, and autoconf:

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, XML parsing for remote helpers, internationalization support, and documentation generation. The wget package is included because minimal Rocky Linux installations may not have it by default.

Download the Source Code

With dependencies in place, download the latest Git source code. The following command uses the GitHub API to automatically detect the newest stable version and download the corresponding archive:

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 command chains several operations together: curl fetches the tag list from GitHub’s API, grep extracts the latest version number, and xargs passes it to wget for downloading. You will see wget output showing the download progress.

If you prefer to download a specific version manually, visit the Git tags page and run: wget https://github.com/git/git/archive/refs/tags/vX.Y.Z.tar.gz where X.Y.Z is your desired version number.

Next, extract the downloaded archive using a wildcard pattern to handle any version:

tar -xzf v*.tar.gz

Then navigate into the extracted source directory:

cd git-*/

Compile and Install Git

Run make to compile Git with the /usr/local prefix. This prefix installs Git to /usr/local/bin, which takes precedence over the system version in your PATH:

make prefix=/usr/local all

Compilation takes several minutes depending on your hardware. When complete, the final lines will show the generation of gitweb components:

    GEN gitweb/gitweb.cgi
    GEN gitweb/static/gitweb.js

After compilation succeeds, install Git to the system. This step requires root privileges:

sudo make prefix=/usr/local install

Verify the Source Installation

Confirm that the newly compiled Git is in your PATH and reports the expected version:

git --version

Expected output:

git version 2.52.0

If the version still shows the older system package, the /usr/local/bin directory may not be first in your PATH. Verify the installation location:

which git

The output should show /usr/local/bin/git for the source-compiled version. When it shows /usr/bin/git instead, you may need to log out and back in, or manually prepend /usr/local/bin to your PATH.

Clean Up Build Files

After successful installation, you can optionally remove the source directory and archive to free disk space:

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

Keep the source directory if you plan to recompile with different options or update to a newer version later.

Configure Git Identity and Preferences

After installation, configure Git with your identity. Git embeds your name and email in every commit you create, which helps collaborators identify who made each change. These settings are required before you can commit any changes.

Set Your Name and Email

Configure your global identity with the following 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 for your user account. Git stores this configuration in ~/.gitconfig. For work on public repositories where you prefer privacy, you can use a pseudonym and a no-reply email address provided by platforms like GitHub or GitLab.

Verify Your Configuration

Review your Git configuration to confirm the settings were applied:

git config --list

Expected output showing your configured values:

user.name=Your Name
user.email=your.email@example.com

You can also view the configuration file directly:

cat ~/.gitconfig

Configure Credential Caching

When working with remote repositories over HTTPS, Git prompts for your username and password on each push or pull. Credential caching stores your credentials temporarily so you do not need to re-enter them for every operation.

To enable credential caching with a 4-hour timeout (14400 seconds):

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

After this duration, Git clears the cached credentials and prompts again on the next authenticated operation. Adjust the timeout value based on your security requirements and work session length.

For enhanced security on shared systems, consider using SSH keys instead of HTTPS credentials. SSH authentication avoids password storage entirely and provides stronger cryptographic verification. See your Git hosting provider’s documentation for SSH key setup instructions.

Create and Manage Git Repositories

With Git installed and configured, you can create repositories to track your projects. This section covers the fundamental operations you will use daily.

Initialize a New Repository

Create a new directory for your project and initialize it as a Git repository:

mkdir ~/my-project
cd ~/my-project
git init

Git creates a hidden .git directory that stores the repository’s configuration, history, and metadata:

hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint:   git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint:   git branch -m <name>
Initialized empty Git repository in /home/user/my-project/.git/

Many projects and platforms now use main as the default branch name. To configure Git to use main for all new repositories:

git config --global init.defaultBranch main

Check Repository Status

The git status command shows the current state of your working directory and staging area:

git status

In a newly initialized repository with no commits:

On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

This output tells you which branch you are on, whether there are uncommitted changes, and what actions you can take next.

Stage and Commit Changes

When you modify files, Git tracks the changes but does not automatically include them in commits. First, stage the files you want to commit using git add:

git add filename.txt

To stage all changed files in the current directory:

git add .

After staging, create a commit with a descriptive message explaining the change:

git commit -m "Add initial project files"

The -m flag lets you provide the commit message inline. For longer messages, omit -m and Git will open your default text editor.

Connect to a Remote Repository

To collaborate with others or back up your code to a hosting service like GitHub, GitLab, or Bitbucket, link your local repository to a remote:

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

Replace the URL with your actual remote repository address. The name origin is a conventional alias for the primary remote, though you can use any name.

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

After committing changes locally, push them to the remote repository:

git push origin main

Replace main with your branch name if different (such as master for older repositories). The first push to a new branch may require the -u flag to set the upstream tracking reference.

To incorporate changes that collaborators have pushed to the remote:

git pull origin main

This fetches the remote changes and merges them into your current branch. If conflicts exist between your local changes and the remote, Git will prompt you to resolve them before completing the merge.

Troubleshooting Common Git Issues

This section addresses common problems you may encounter when installing or using Git on Rocky Linux.

Source-Compiled Git Not Found in PATH

After compiling from source, if git --version still shows the old system version, the /usr/local/bin directory may not be first in your PATH. Check the current Git path:

which git
echo $PATH

When /usr/local/bin appears after /usr/bin in the PATH output, the system version takes precedence. To fix this, add the following line to your ~/.bashrc file:

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

Apply the change immediately:

source ~/.bashrc

Compilation Fails with Missing Dependencies

If make fails with errors about missing commands or libraries, the error output typically identifies the missing component. A common example when the Development Tools group is not installed:

/tmp/git-2.52.0/GIT-VERSION-GEN: line 100: cmp: command not found
make: *** [Makefile:2788: GIT-VERSION-FILE] Error 1

This error indicates that essential build utilities are missing. Ensure the Development Tools group is installed, then add any additional missing packages:

sudo dnf groupinstall "Development Tools"
sudo dnf install zlib-devel perl-ExtUtils-MakeMaker

If you also want to build Git’s documentation, add asciidoc and xmlto to the install command. For most users, the packages above are sufficient.

Permission Denied When Pushing to Remote

If you receive “Permission denied (publickey)” or authentication errors when pushing:

  • Verify your credentials are correct and you have write access to the repository
  • For HTTPS, ensure credential caching is configured or enter credentials when prompted
  • For SSH, verify your SSH key is added to the ssh-agent and registered with your Git hosting provider. If you have not configured SSH keys yet, see how to install and configure SSH on Rocky Linux

Test SSH connectivity to GitHub (or your provider):

ssh -T git@github.com

Manage Git Installation

Update DNF-Installed Git

When installed via DNF, Git updates arrive with your standard system packages. Run the following command to update all packages including Git:

sudo dnf upgrade --refresh

Update Source-Compiled Git

For source-compiled installations, you can either repeat the manual compilation process or use the following update script. This script automatically detects the latest Git version, compares it with your installed version, and handles the download, compilation, and installation.

First, create the update script:

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

Paste the following script content:

#!/bin/bash
set -e

# Check for required tools
for cmd in curl wget make gcc nproc; do
    if ! command -v $cmd &>/dev/null; then
        echo "Error: $cmd is required but not installed."
        exit 1
    fi
done

# Get current installed version
CURRENT_VERSION=$(git --version 2>/dev/null | grep -oP '\d+\.\d+\.\d+' || echo "none")

# Fetch latest version from GitHub API
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 latest version from GitHub API."
    echo "Check your internet connection or try again later."
    exit 1
fi

echo "Checking for Git updates..."
echo "Current version: $CURRENT_VERSION"
echo "Latest version:  $LATEST_VERSION"

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

echo ""
echo "Updating from $CURRENT_VERSION to $LATEST_VERSION..."

# Create temporary build directory
BUILD_DIR=$(mktemp -d)
cd "$BUILD_DIR"

# Download and extract
echo "Downloading Git $LATEST_VERSION..."
wget -q "https://github.com/git/git/archive/refs/tags/v${LATEST_VERSION}.tar.gz"

echo "Extracting source code..."
tar -xzf "v${LATEST_VERSION}.tar.gz"
cd "git-${LATEST_VERSION}"

# Compile and install
echo "Compiling Git (this may take a few minutes)..."
make prefix=/usr/local all -j$(nproc) >/dev/null 2>&1

echo "Installing Git (requires sudo password)..."
sudo make prefix=/usr/local install >/dev/null 2>&1

# Cleanup
rm -rf "$BUILD_DIR"

# Verify
NEW_VERSION=$(git --version | grep -oP '\d+\.\d+\.\d+')
echo ""
echo "Update complete!"
echo "git version $NEW_VERSION"

Save the file by pressing Ctrl+O, then Enter, then exit with Ctrl+X. Next, make the script executable:

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

Since the script installs to /usr/local/bin, you can run it 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 from 2.51.0 to 2.52.0...
Downloading Git 2.52.0...
Extracting source code...
Compiling Git (this may take a few minutes)...
Installing Git (requires sudo password)...
[sudo] password for user: 

Update complete!
git version 2.52.0

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

Remove Git from Rocky Linux

If you need to remove Git from your system, the process differs depending on how you installed it.

Remove DNF-Installed Git

To remove Git installed via the package manager:

sudo dnf remove git

After removal, clean up any orphaned dependencies that were installed automatically with Git:

sudo dnf autoremove

Verify removal by checking that the git command is no longer available:

git --version
bash: git: command not found

Remove Source-Compiled Git

If you compiled Git from source, manually remove the installed files from /usr/local:

sudo rm -f /usr/local/bin/git /usr/local/bin/git-*
sudo rm -rf /usr/local/libexec/git-core
sudo rm -rf /usr/local/share/git-core
sudo rm -rf /usr/local/share/man/man1/git*
sudo rm -rf /usr/local/share/man/man5/git*
sudo rm -rf /usr/local/share/man/man7/git*

If you created the update script, remove it as well:

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

These commands permanently delete the source-compiled Git installation. Your Git configuration file (~/.gitconfig) and any repository data in .git directories are not affected by uninstallation.

Verify that Git is no longer accessible:

git --version

Expected output confirming removal:

bash: git: command not found

Final Thoughts

You now have Git installed and configured on Rocky Linux, ready for version control workflows. The DNF method provides stable releases with automatic updates, while source compilation offers access to the latest features. With your identity configured, repositories initialized, and remote connections established, you can track changes, collaborate with teams, and maintain a complete history of your projects. For more advanced Git usage, explore the official Git documentation, learn how to clear Git cache when troubleshooting, or consider integrating Git with an IDE like VS Code on Rocky Linux.

Leave a Comment