How to Install and Configure Git on Rocky Linux 9
Git is an open-source, distributed version control system that tracks file changes and enables efficient project management. It allows multiple collaborators to work on the same file, while preserving and monitoring the complete history of all changes in a repository. Git integrates seamlessly with repository hosting platforms like GitHub, GitLab, and BitBucket, making it easier to collaborate and maintain version control across projects.
This guide explains how to install Git on Rocky Linux 9. You will learn how to check the pre-installed Git version and install the latest stable version on your workstation.
Prerequisites
- Access to a Rocky Linux 9 instance as a non-root user with sudo privileges.
Check the Default Git Version
By default, Rocky Linux 9 comes with Git pre-installed. Although the included version might not be the latest, it is sufficient for performing version control tasks and managing repositories locally. Follow the steps below to verify your current Git version before upgrading or installing a specific version.
Check the pre-installed Git version:
$ git --version
Example output:
git version 2.43.5
Run the Git help command to confirm you can access all available options:
$ git help
Install Git Using DNF
Dandified YUM (DNF) is the default package manager on Rocky Linux 9, allowing you to install packages from its repositories. While the version available in DNF may not be the latest, it provides a quick way to install a stable release of Git.
First, update the package index:
$ sudo dnf update
Then, install Git:
$ sudo dnf install git
If the following message appears, it means the latest stable version from the Rocky Linux 9 repository is already installed:
Package git-2.43.5-2.el8_10.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
Verify the installed Git version:
$ git -v
Install a Specific Git Version from Source
Building Git from source gives you the flexibility to install the latest release or a specific version required by your project. This method also allows installing beta or alpha versions not available in DNF repositories.
First, update the package index:
$ sudo dnf update
Install dependencies required for compiling Git:
$ sudo dnf install gettext-devel openssl-devel perl-CPAN perl-devel zlib-devel gcc autoconf libcurl-devel expat-devel
Download the desired Git version from the official archives (e.g., version 2.49.0):
$ wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.49.0.tar.gz
Extract the downloaded archive:
$ tar -xvf git-2.49.0.tar.gz
Enter the extracted directory:
$ cd git-2.49.0
Compile Git and install it into /usr/local
:
$ sudo make prefix=/usr/local all
Install the compiled Git package:
$ sudo make prefix=/usr/local install
Open a new shell session to apply the changes:
$ bash
Check the installed version:
$ git --version
Configure Git
Before using Git, configure your username and email, as these details are included in commit messages.
Set your global username:
$ git config --global user.name "Your Full Name"
Set your global email:
$ git config --global user.email "email@example.com"
To set username and email for a single repository, omit --global
:
$ git config user.name "Your Name"
$ git config user.email "email@example.com"
View your Git configuration:
$ git config --list
Open the global configuration file:
$ nano ~/.gitconfig
Edit the local repository configuration file if applicable:
$ nano .git/config
Example configuration format:
[user]
name = Your Full Name
email = email@example.com
Save changes and exit the editor to apply your updates.
Access and Use Git
With Git installed and activated on your Rocky Linux 9 system, you can now integrate it into your project workflow. The steps below demonstrate how to set up a local project, manage files, and authenticate with a remote repository service such as GitHub for committing changes.
Create a new project directory:
$ mkdir git-project
Navigate into the project directory:
$ cd git-project
Create a sample file, for example bash-script.sh
:
$ touch bash-script.sh
Initialize the directory as a Git repository:
$ git init
Example output:
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>
Stage all files in the directory:
$ git add .
Stage a specific file instead of all files:
$ git add <filename>
Commit the staged changes with a message:
$ git commit -m "new commit"
Add a remote repository to connect with your local repository:
$ git remote add origin <remote repository>
Set the remote URL with authentication details for GitHub (or another provider):
$ git remote set-url origin https://<username>:<token>@github.com/username/repo.git
Push changes from the local repository to the main branch of the remote repository:
$ git push origin main
Retrieve the latest changes from the remote repository:
$ git pull origin main
View the commit history of the repository:
$ git log
List all remote repositories linked to your local repository:
$ git remote
List all branches, including local and remote branches:
$ git branch -a
Conclusion
You have successfully installed Git on Rocky Linux 9, set up and configured local repositories, and learned how to synchronize changes with a remote repository. Git enables robust version control, making it possible to track, manage, and roll back file changes as needed. By integrating Git with a service like GitHub, you can securely store your repository online and collaborate effectively. For further details, refer to the official Git documentation.