
Git is an open-source distributed version control system that tracks changes in files and enables efficient tracking using in projects. Git enables multiple users to collaborate on a single file, maintain and track the history of all changes in a repository. Git works with repository hosting platforms like GitHub, GitLab, and BitBucket to support efficient collaboration and version control on different projects.
This article explains how to install Git on Rocky Linux 9. You will verify the pre-installed Git version on Rocky Linux 9 and install the latest stable version to use on your workstation.
Prerequisites
Before you begin, you need to:
- Have access to a Rocky Linux 9 instance as a non-root sudo user.
Verify the Default Git Version
Git is pre-installed and available on Rocky Linux 9 by default. The default version may not be the latest, but it allows you to perform versioning tasks and manage repositories on your workstation. Follow the steps below to verify the default Git version before installing the latest or a specific version on Rocky Linux 9.
Check the pre-installed Git version on Rocky Linux 9.
console$ git --version
Your output should be similar to the one below.
git version 2.43.5
Run the Git help command to verify that you can access all Git options.
console$ git help
Output:
........ collaborate (see also: git help workflows) fetch Download objects and refs from another repository pull Fetch from and integrate with another repository or a local branch push Update remote refs along with associated objects 'git help -a' and 'git help -g' list available subcommands and some concept guides. See 'git help <command>' or 'git help <concept>' to read about a specific subcommand or concept. See 'git help git' for an overview of the system.
Install Git Using DNF
Dandified YUM (DNF) is the default package manager for Rocky Linux 9 that lets you install packages on your server. Git is available in the default DNF sources on Rocky Linux 9. The available version may not be the latest, but DNF provides a fast way to install a stable Git version. Follow the steps below to update the DNF package index and install Git.
Update the DNF package information index.
console$ sudo dnf update
Install Git using the DNF package manager.
console$ sudo dnf install git
If you receive the following package output, the latest stable Git version in the Rocky Linux 9 repositories is already installed. Compile Git from source code to install the latest stable version.
Package git-2.43.5-2.el8_10.x86_64 is already installed. Dependencies resolved. Nothing to do. Complete!
Check the installed Git version.
console$ git -v
Run the Git help command and verify access to all Git options.
console$ git help
Install a Specific Git Version from Source Code
Installing Git from source code allows you to install the latest version or a specific version required in your project. You can install the latest stable version, beta, or alpha releases that are not available in the default DNF package manager sources, providing you with more customization options. Follow the steps below to install the required packages and install a specific Git version from source code.
Update the DNF package information index.
console$ sudo dnf update
Install all required packages for compiling Git on Rocky Linux 9.
console$ sudo dnf install gettext-devel openssl-devel perl-CPAN perl-devel zlib-devel gcc autoconf libcurl-devel expat-devel
Visit the Git archives page and download the latest stable Git version or specific version archive using
wget
. For example,https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.49.0.tar.gz
console$ wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.49.0.tar.gz
Extract all files from the downloaded archive, depending on the version.
console$ tar -xvf git-2.49.0.tar.gz
Navigate to the extracted Git directory depending on the downloaded version.
console$ cd git-2.49.0
Run
make
to compile Git from source code and install it in the/usr/local
directory.console$ sudo make prefix=/usr/local all
Output:
................ CC t/unit-tests/u-reftable-tree.o CC t/unit-tests/u-strbuf.o CC t/unit-tests/u-strcmp-offset.o CC t/unit-tests/u-strvec.o CC t/unit-tests/clar/clar.o CC t/unit-tests/unit-test.o CC t/unit-tests/lib-oid.o LINK t/unit-tests/bin/unit-tests GEN gitweb/gitweb.cgi GEN gitweb/static/gitweb.js
Install Git from the compiled package to the
/usr/local/
directory.console$ sudo make prefix=/usr/local install
The above command replaces the default Git version in the
/usr/local
directory, allowing you to use the newly installed version.Enter a new shell to apply the Git package information changes.
console$ bash
Test the installed Git version.
console$ git --version
Your updated Git version should appear like the one below.
git version 2.49.0
Run the Git help command and confirm access to all help options.
console$ git help
Configure Git
Git requires a correct username and email to tag on commit messages for staged files or changes. The git config
command allows you to configure Git in your current repository or globally on your server. Follow the steps below to configure Git with your username and email information.
Configure the username to use with Git in all repositories.
console$ git config --global user.name "Your Full Name"
Configure the email to use with Git in all repositories.
console$ git config --global user.email "email@example.com"
To specify the username and email information to use in an initialized repository, run the
git config
command without the--global
option.console$ git config user.name "Your me" $ git config user.email "Your me"
Display the Git configuration information.
console$ git config --list
Output:
user.name=Your Full Name user.email=email@example.com
Use the
--local
option to display the local Git information if configured in the current repository.console$ git config --list --local
Open the
.gitconfig
file in your user's home directory to view the stored information.console$ nano ~/.gitconfig
Use the hidden
.git/config
directory in your current repository if you configured local Git information in your current repository.console$ nano .git/config
Modify the Git configuration information if needed.
ini[user] name = Your Full Name email = email@example.com
Save the file and exit the text editor after applying the new changes.
Access and Use Git
Git is installed and active on your Rocky Linux 9 server as performed in the instructions above. Follow the steps below to access and use Git in your project environment and authenticate with a remote repository provider such as GitHub to commit file changes.
Create a project like
git-project
.console$ mkdir git-project
Navigate to the project directory.
console$ cd git-project
Create a basic configuration file like
bash-script.sh
.console$ touch bash-script.sh
Initialize the project directory using Git.
console$ git init
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>
Use
git add
to stage all files in the directory.console$ git add .
Use
git add
with a file name to stage a specific file in the repository.console$ git add <filename>
Create a new commit for the staged changes using Git.
console$ git commit -m "new commit"
Add a remote repository to sync with the local Git repository.
console$ git remote add origin <remote repository>
Log in to your Git repository provider, such as GitHub to enable push access.
console$ git remote set-url origin https://<username>:<token>@github.com/username/repo.git
Enter your GitHub username and password if prompted.
Push all local repository changes to the remote repository's main branch.
console$ git push origin main
Pull the latest changes from the remote repository.
console$ git pull origin main
Track the Git commit history.
console$ git log
List all available remotes in your local repository.
console$ git remote
List all branches in the current repository.
console$ git branch -a
Conclusion
You have installed Git on Rocky Linux 9 and configured local repositories to synchronize changes in your project environment. Git enables versioning, allowing you to track and undo file changes on a server. Integrating Git with a remote repository provider like GitHub allows you to store local repository changes safely and roll back any files whenever needed. For more information, visit the Git documentation.
No comments yet.