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.
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.
| Method | Channel | Version | Updates | Best For |
|---|---|---|---|---|
| DNF Package Manager | Rocky AppStream | Distribution default (2.43-2.47) | Automatic via dnf upgrade | Most users who want stable, tested packages |
| Source Compilation | GitHub Mirror | Latest stable (currently 2.52.0) | Manual recompilation required | Developers who need the latest features |
This guide applies to Rocky Linux 8, 9, and 10. DNF installs Git from AppStream on all three releases, but the version differs (2.43.x on Rocky Linux 8 and 2.47.x on Rocky Linux 9/10). The source build workflow stays the same across versions.
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. This method provides a stable, tested version that receives security updates when you upgrade your system. If git --version already prints a version, Git is installed and you can skip the install command.
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. If you only need the repository version, you can skip the source build section.
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, localization, and Perl-based tools such as git-svn. The wget package is included because minimal Rocky images usually omit it.
Install
tclandtkif you want the optional GUI toolsgitkandgit-gui. Without them, the build completes but shows atclshwarning.
sudo dnf install tcl tk
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 -oE '"name": "v[0-9]+\.[0-9]+\.[0-9]+"' | head -n 1 | cut -d '"' -f4 | xargs -I{} wget https://github.com/git/git/archive/refs/tags/{}.tar.gz
This command chains several operations together: curl fetches the tag list from GitHub’s API, grep extracts the newest stable tag (vX.Y.Z), cut trims the JSON, and xargs passes the tag to wget for download. The pattern matches stable tags only, so release candidates like v2.52.0-rc0 are ignored. If you want a refresher on wget output and flags, see wget command examples.
If you prefer a specific release or hit API rate limits, open the Git tags page and download a version directly:
wget https://github.com/git/git/archive/refs/tags/vX.Y.Z.tar.gzwhere X.Y.Z is the version you want.
Extract the downloaded archive using a wildcard pattern to handle any version:
tar -xzf v*.tar.gz
The archive extracts into a git-* directory. If you need a refresher on working with .tar.gz files, see how to open gz/tgz files in Linux.
Move 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
Verify that your shell is using the /usr/local binary:
command -v git
Expected output:
/usr/local/bin/git
If you still see /usr/bin/git or an older version, move /usr/local/bin ahead of /usr/bin in your PATH and reload your shell.
Clean Up Build Files
After successful installation, you can optionally remove the source directory and archive to free disk space:
This cleanup deletes the extracted
git-*source directory and the downloaded archive. If you made local changes or want to keep the sources, back them up first withcp -a git-* ~/git-source-backup.
cd ~
rm -rf git-*/ v*.tar.gz
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 a personal access token (many providers no longer accept account passwords). Credential caching stores these 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
If you need to rename an existing branch, see how to rename a local and remote Git branch.
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.
If you manage GitHub from the terminal, see how to install GitHub CLI on Rocky Linux for authentication and repository management tools.
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
Use the examples below to diagnose common Git problems on Rocky Linux and verify the fix before moving on.
Source Build Still Shows the Repository Version
If you compiled from source but git --version still shows the AppStream package, your shell is using /usr/bin/git instead of /usr/local/bin/git.
Example output from the system package:
git version 2.47.3
Rocky Linux 8 users may see git version 2.43.7 instead.
Check which Git binary is first in your PATH:
command -v git
Expected output when the system package is still taking precedence:
/usr/bin/git
Add this line to ~/.bashrc so /usr/local/bin takes precedence:
export PATH="/usr/local/bin:$PATH"
Apply the change immediately:
source ~/.bashrc
Alternatively, open a new terminal session so the change loads automatically.
Verify the path and version now point to the source build:
command -v git
git --version
Expected output:
/usr/local/bin/git git version 2.52.0
Compilation Stops With Missing Dependencies
If make fails with missing tools, the error output usually names the command that is missing.
Example error output:
/tmp/git-2.52.0/GIT-VERSION-GEN: line 100: cmp: command not found make: *** [Makefile:2788: GIT-VERSION-FILE] Error 1
Confirm whether cmp is available:
command -v cmp || echo "cmp missing"
Expected output when cmp is missing:
cmp missing
Install the Development Tools group (includes diffutils) and try the build again:
sudo dnf groupinstall "Development Tools"
sudo dnf install diffutils
From the Git source directory, rerun the build:
make prefix=/usr/local all
SSH Push Fails With Permission Denied (publickey)
If you see an SSH authentication error during git push, Git cannot find a valid key for your account.
Example error output:
git@github.com: Permission denied (publickey). fatal: Could not read from remote repository.
Test SSH authentication:
ssh -T git@github.com
Successful authentication returns:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
If the first connection prompts you to confirm the host key, type yes and press Enter to continue.
Install the SSH client and add a key to your agent if you do not have one yet:
sudo dnf install openssh-clients
ssh-keygen -t ed25519 -C "your.email@example.com"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
Upload the public key from ~/.ssh/id_ed25519.pub to your Git hosting provider, then run the authentication test again. For a full walkthrough, see install and configure SSH on Rocky Linux.
If you use HTTPS instead of SSH, replace your saved password with a personal access token and retry the push.
Manage Git Installation
Update DNF-Installed Git
When installed via DNF, Git updates arrive with your standard system packages. To update Git only, run:
sudo dnf upgrade --refresh git
Update Source-Compiled Git
For source-compiled installations, you can repeat the manual build steps or use an update script that detects the latest Git tag, rebuilds, and installs it.
This script performs a few safety checks before rebuilding:
- Privilege check: stops if you run it as root and uses
sudoonly for installation - Tool check: confirms
curl,tar,make, andgccare available - Version detection: compares your installed Git with the latest stable tag
Create the update script:
sudo nano /usr/local/bin/update-git
Paste the script below, then save the file:
#!/bin/bash
set -e
# Configuration
INSTALL_PREFIX="/usr/local"
BUILD_DIR="$HOME/git-build"
LOG_FILE="$BUILD_DIR/update-git.log"
# Refuse to run as root
if [ "$(id -u)" -eq 0 ]; then
echo "Run this script as a regular user; use sudo only for install steps."
exit 1
fi
# Check for required tools
if ! command -v curl >/dev/null 2>&1; then
echo "Error: curl is required but not installed."
echo "Run: sudo dnf install curl"
exit 1
fi
if ! command -v tar >/dev/null 2>&1; then
echo "Error: tar is required but not installed."
echo "Run: sudo dnf install tar"
exit 1
fi
for cmd in 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 installed version
CURRENT_VERSION=$($INSTALL_PREFIX/bin/git --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -n 1 || echo "none")
if [ "$CURRENT_VERSION" = "none" ]; then
CURRENT_TAG="none"
else
CURRENT_TAG="v$CURRENT_VERSION"
fi
# Fetch latest version from GitHub API
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."
echo "Check your network connection or try again later."
exit 1
fi
echo "Checking for Git updates..."
echo "Current version: $CURRENT_TAG"
echo "Latest version: $LATEST_TAG"
if [ "$CURRENT_TAG" = "$LATEST_TAG" ]; then
echo "Already up to date."
exit 0
fi
echo ""
echo "Updating from $CURRENT_TAG to $LATEST_TAG..."
echo "$(date): Updating to $LATEST_TAG" >> "$LOG_FILE"
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 can take several minutes)..."
make prefix="$INSTALL_PREFIX" all
echo "Installing..."
sudo make prefix="$INSTALL_PREFIX" install
# Verify
NEW_VERSION=$($INSTALL_PREFIX/bin/git --version | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -n 1)
echo "$(date): Updated to v$NEW_VERSION" >> "$LOG_FILE"
echo "Update complete."
echo "git version $NEW_VERSION"
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
The script stores build files and a log in $HOME/git-build. Remove that directory when you no longer need it.
Example output when already up to date:
Checking for Git updates... Current version: v2.52.0 Latest version: v2.52.0 Already up to date.
Example output when an update is available:
Checking for Git updates... Current version: v2.51.0 Latest version: v2.52.0 Updating from v2.51.0 to v2.52.0... Downloading v2.52.0... Extracting source... Compiling (this can take 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 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:
The following commands permanently delete the source-compiled Git files under
/usr/local, including gitweb assets and templates. If you customized anything there, back it up first withcp -a /usr/local/share/git-core ~/git-core-backup. Skip this section if you installed Git with DNF. These commands do not touch~/.gitconfigor your repositories.
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 /usr/local/share/perl5/FromCPAN
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
If you added /usr/local/bin to your PATH in ~/.bashrc specifically for the source build, remove that line after uninstalling.
Verify that Git is no longer accessible:
git --version
Expected output confirming removal:
bash: git: command not found
Next Steps With Git
You now have Git installed and configured on Rocky Linux, and you can verify which binary you are using, update it, and remove it cleanly. That foundation lets you initialize repositories, commit changes, and sync with remotes. For next steps, see how to undo the last Git commit or clear Git cache, and reference the official Git documentation for deeper workflows.