Git is one of the most popular open-source, distributed version control systems available, designed to handle everything from small to huge projects with speed and efficiency. It allows you to keep track of your code changes, create different code branches, collaborate with other developers, to name a few. Linux Kernel, one of the popular open-source projects, is also hosted on a Git repository.
In this comprehensive guide, we‘ll cover installing Git on CentOS, Ubuntu, and Windows operating systems. We‘ll also dive deeper into verifying the installation, managing different versions, and some best practices around Git configuration.
Installing Git on CentOS
The yum package manager can be used to install Git on CentOS 6.x/7.x systems, while DNF is the newer package manager available on CentOS 8.x.
Git Installation on CentOS 6.x/7.x
To install Git on CentOS 6.x/7.x, run the following yum command:
$ sudo yum install git
When prompted, confirm the installation by pressing ‘y‘.
Here is a sample installation log on a minimal CentOS 7 system. Note that Git dependencies like Perl are installed automatically:
$ sudo yum install git
# Installation log...
Installed:
git.x86_64 0:1.8.3.1-23.el7_8
Dependency Installed:
# List of Git dependencies...
Complete!
Git Installation on CentOS 8.x
For CentOS 8.x, use the dnf command instead:
$ sudo dnf install git
Confirm the installation if prompted.
Below is a sample log when installing Git using dnf on CentOS 8:
$ sudo dnf install git
# Installation log...
Installed:
git.x86_64 0:2.27.0-1.el8
Complete!
DNF handles dependencies automatically, so additional packages required by Git will be installed.
Installing Git on Ubuntu
On Ubuntu, Git can be installed using the apt package manager:
$ sudo apt update
$ sudo apt install git
When prompted, confirm the installation by pressing ‘Y‘.
Here is an example log when installing Git on Ubuntu 20.04 LTS:
$ sudo apt update
$ sudo apt install git
Reading package lists... Done
# Installation log...
Unpacking git (1:2.25.1-1ubuntu3) ...
Setting up git (1:2.25.1-1ubuntu3) ...
Processing triggers for man-db (2.9.1-1) ...
The main Git packages and required dependencies like git-man and liberror-perl are installed automatically.
Installing Git on Windows
Windows installers for Git are available from the official Git website. Download the 64-bit installer for most modern systems.
The steps to install Git on Windows are:
- Run the downloaded Git installer .exe file and accept any User Access Control prompts.
- Accept the license agreement and click Next.
- Leave the default installation path or change it if required. Click Next.
- Select the components to install. The default options are fine for most uses.
- Click Next on the remaining screens to accept default options.
- On the experimental options screen, leave them unchecked unless you specifically need them.
- Click Install to begin the installation.
- When the installation completes, you can choose to launch Git Bash.
Git can then be accessed from the Windows Command Prompt or Git Bash.
Verifying the Git Installation
You can check the installed Git version using the git –version command:
$ git --version
git version 2.25.1
This will confirm that Git is installed and accessible from the command line.
Managing Multiple Git Versions
At times you may need to install and maintain different Git versions on the same system.
On Linux, tools like scl (Software Collections Library) can be used to install multiple Git versions simultaneously.
To install Git 1.9.x along with the distribution Git version (2.25.x):
$ sudo yum install rh-git19
$ scl enable rh-git19 bash
The second command will start a new shell with Git 1.9.x. Check the version:
$ git --version
git version 1.9.5
To go back to the system Git version, exit this shell.
On Windows, separate Git versions can be installed in different directories. Ensure the PATH environment variable points to the Git executable you want to use.
Configuring Git
It‘s recommended to set your name and email in Git, especially if you plan to contribute to public repositories:
$ git config --global user.name "John Doe"
$ git config --global user.email [email protected]
The –global flag sets this configuration for all Git repositories on your system. You can override these values later at the repository level.
Configure your preferred text editor for commit messages:
$ git config --global core.editor vim
Set push behavior to simple – a safer default compared to the matching option:
$ git config --global push.default simple
Consider setting some GUI tools like gitk for commit history visualization:
$ git config --global gui.commitmsg.commit.tools gitk
There are tons of customization options available in Git. Refer to the Git config documentation for more details.
Cloning a Git Repository
Now that Git is installed, let‘s clone a sample repository to test it out.
We‘ll use the Geekflare-learning repository on GitHub:
$ git clone https://github.com/Top10SM/Top10SM-learning.git
Cloning into ‘Top10SM-learning‘...
remote: Enumerating objects: 12, done.
remote: Counting objects: 100% (12/12), done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 12 (delta 1), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (12/12), done.
This creates a local copy of the remote repository. Navigate into this directory and check the log:
$ cd Top10SM-learning
$ git log
commit 07f3a09c619f36e9e9c67a786ca631f79d766710 (HEAD -> master, origin/master, origin/HEAD)
Author: Geek Flare <[email protected]>
Date: Sun Oct 4 02:37:39 2020 +0530
Update README.md
commit 196819c357b78b24a1e74e0c4f86dd5400e6cdf4
...
You can make local changes to files, create commits, branches and then push changes back to the remote repository.
This validates our Git installation and basic repository operations are working correctly.
Summary
In this guide, we went through installing the latest Git versions on CentOS, Ubuntu and Windows systems. We also looked at verifying the installation, managing multiple versions, and some initial configuration.
Git is a powerful version control system that integrates closely with developer workflows. Mastering Git helps build confidence with managing code changes and collaborating efficiently on projects.
Some next steps after installing Git:
- Learn Git branching and commit best practices.
- Understand concepts like Git rebase vs merge.
- Get started with GitHub by managing repositories and collaborating on projects.
- Look at GitOps principles for deployment and DevOps.
Git and version control are essential skills for any developer today. Hopefully, this guide provided a solid foundation for getting started with Git and using it effectively for your projects.