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.
Matching infrastructure at centron
No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →
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 --versionExample output:
git version 2.43.5
Run the Git help command to confirm you can access all available options:
$ git helpInstall 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 updateThen, install Git:
$ sudo dnf install gitIf 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 -vInstall 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 updateInstall dependencies required for compiling Git:
$ sudo dnf install gettext-devel openssl-devel perl-CPAN perl-devel zlib-devel gcc autoconf libcurl-devel expat-develDownload 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.gzExtract the downloaded archive:
$ tar -xvf git-2.49.0.tar.gzEnter the extracted directory:
$ cd git-2.49.0Compile Git and install it into /usr/local:
$ sudo make prefix=/usr/local allInstall the compiled Git package:
$ sudo make prefix=/usr/local installOpen a new shell session to apply the changes:
$ bashCheck the installed version:
$ git --versionConfigure 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 --listOpen the global configuration file:
$ nano ~/.gitconfigEdit the local repository configuration file if applicable:
$ nano .git/configExample configuration format:
[user]
name = Your Full Name
email = email@example.comSave 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-projectNavigate into the project directory:
$ cd git-projectCreate a sample file, for example bash-script.sh:
$ touch bash-script.shInitialize the directory as a Git repository:
$ git initExample 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.gitPush changes from the local repository to the main branch of the remote repository:
$ git push origin mainRetrieve the latest changes from the remote repository:
$ git pull origin mainView the commit history of the repository:
$ git logList all remote repositories linked to your local repository:
$ git remoteList all branches, including local and remote branches:
$ git branch -aConclusion
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.
Testen Sie Ihr Setup auf ccloud³
Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.