Git is a mature, actively maintained open source project initially developed in 2005 by Linus Torvalds, the famous Linux operating system kernel creator. Git is designed for developers that need a pretty straightforward version control system. Most software is collaborative efforts and sometimes can have hundreds of people with commits working on software development projects. It is essential to track these commits customarily done in branches in most projects before being merged into the master for release. It is easy to review and track down any incorrect commits and revert, leading to a much easier development if anything goes wrong.
In the following tutorial, you will learn how to install Git on CentOS 8 Stream in various methods.
- Recommended OS: Any current CentOS 8 Stream.
- User account: root access
Updating Operating System
Update your CentOS Stream operating system to make sure all existing packages are up to date:
sudo dnf upgrade --refresh -y
The tutorial will be using the sudo command and assuming you have sudo status.
To verify sudo status on your account:
Example output showing sudo status:
[joshua@centos-stream ~]$ sudo whoami root
To set up an existing or new sudo account, visit our tutorial on adding a User to Sudoers on CentOS Stream.
To use the root account, use the following command with the root password to log in.
The tutorial will utilize the terminal, which can be found in your show applications menu.
Install Required Dependecies
The following dependencies are needed to enable the PowerTools repository successfully. This should already be installed by default, but run the command to check.
sudo dnf install dnf-plugins-core
By default, this should be installed already but should be checked.
Method 1. Install Git from Appstream
By default, Git is available in the App stream repository and installed using the DNF package manager. To do this, use the following command:
sudo dnf install git
Example of dependencies that will be installed:
Type “Y,” then press “ENTER KEY” to proceed with the installation.
Once installed, verify the installation:
git version 2.31.1
You can download the source and compile it in the following method for users wanting the latest version, or perhaps a specific version.
Method 2. Compile & Install Git from Source
The tutorial has covered installing from the App stream. However, for those wanting the absolute latest Git version, it’s recommended to always install from the source. However, it is a bit more time-consuming but will always leave you with the newest version available.
As with unstable, you will need to make sure any security issues are monitored; with the source, you can quickly re-compile any urgent updates making this the better option for anyone needing to use the latest Git.
Before starting the installation from the source, use the su command to log into the root account for this installation.
To begin with, install the Git dependencies as follows:
sudo dnf install gettext-devel curl-devel expat-devel openssl-devel perl-CPAN perl-devel zlib-devel unzip cmake gcc make -y
Next, visit the release page to find the master zip archive or the latest stable release from Git.
When writing this tutorial, 2.34.1 is the latest stable release. Use the wget command as below:
Use the wget command to get the latest development version (master):
wget https://github.com/git/git/archive/refs/heads/master.zip -O git-nightly.zip
Note, do not use this version unless it will be unstable and possibly contain bugs.
Next, unzip the archive you downloaded. The tutorial will extract git to the /opt/ directory as follows.
sudo unzip v2.34.1.zip -d /opt
If you cannot unzip, somehow you do not have the unzip package installed, run the following command.
sudo dnf install unzip -y
Now you will need to navigate to the directory using the CD command:
You now need to run the following make commands to install git:
sudo make prefix=/usr/local all
sudo make prefix=/usr/local install
Now that you have installed Git from the source, verify the installation and build:
git version 2.34.1
Example in a real environment:
Congratulations, you have installed Git on CentOS 8 Stream using the compile method.
How to Configure Git
After installation, you will need to set up standard settings such as names and e-mails, mainly around git commit messages. This is pretty straight forward as the tutorial will explain below.
The first step is to provide your name that will be set Globally:
git config --global user.name "YOUR NAME"
Next, select your e-mail; this can fake if you prefer:
git config --global user.email "YOUR EMAIL"
To confirm these have been added, use the config –list command:
git config --list
Unless specified, Git stores details in the ~/.gitconfig file. You can review what is currently stored by using the cat command:
Note, using the sudo command with the git config command will set two separate user names and e-mails.
You can store this information for quicker access in the future; note this is for dedicated servers being run by 1 or 2 people that are trustworthy as the information isn’t stored securely or encrypted and is just in text form, so any users that have access to the server can easily read this.
git config --global credential.helper cache
If you must use credential helper, it is advised to cache only for a limited time for increased security. For example, you will be working today using git for 1 to 4 hours but won’t be touching it for maybe a few weeks, then set expiry for 5 hours:
git config --global credential.helper "cache --timeout=18000"
After 5 hours, the credentials will be deleted, and this secures your GIT.
Comments and Conclusion
Git is a fantastic piece of software for software developers and sysadmins. Web server owners can track changes on specific directories when developing your servers or website, and the ability to quickly revert should not be looked over. Git is not the most accessible software to work, and however, it works well for what it is designed for.