CentOS Git Installation Guide

Install Git on CentOS 7.5

Git is a very popular Version Control System (VCS). It is written by the creator of Linux Kernel, Linus Torvalds. It is used to manage versions and snapshots of source codes of different software. It is used by software developers and software companies all around the world.

Git is a collaborative tool that helps many people work on the same project simultaneously. Git has a remote or central repository. This is where everyone pushes their changes. A developer clones a snapshot of the remote Git repository. It keeps a local copy of the remote repository in the developer’s computer. The developer makes changes to the source code and then he can push the changes back to a remote Git repository. The codes then can be checked and merged by the maintainer of that repository. This is how Git works.

In this article, I will show you how to install Git on CentOS 7.5 and the basics of Git. Let’s get started.

Installing Git

Git is available in the official package repository of CentOS 7.5.

First update the yum package repository cache with the following command:

$ sudo yum makecache

The yum package repository cache should be updated.

Now install Git with the following command:

$ sudo yum install git

Press y and then press <Enter> to continue.

Git should be installed.

You can check whether Git is working with the following command:

$ git --version

As you can see, Git is installed and working correctly.

Now let’s see how to use Git.

Initial Configuration of Git

Before you can use Git, you have to set some global Git variables, such as your name, email etc. You do not need to repeat these commands every time. This is a onetime configuration.

First set your full name with the following command:

$ git config --global user.name 'YOUR FULL NAME'

$ git config --global user.email 'YOUR EMAIL'

Enabling Colors in Git

By default, on CentOS 7.5, colors are disabled in Git. But colors make Git easier to use. Don’t worry, you can enable colors easily.

Run the following commands to enable colors in Git:

$ git config --global color.branch auto
$ git config --global color.diff auto
$ git config --global color.status auto
$ git config --global color.ui auto

Colors should be enabled in Git.

Initializing a Git Repository

To get a project or directory for Git ready, first you have to initialize it.

First navigate into your project directory with the following command:

$ cd YOUR/PROJECT/DIRECTORY

Now run the following command to initialize the directory for Git:

$ git init

Install Git CentOS7

The directory should be initialized as you can see from the screenshot below.

Tracking Files in a Git Repository

In a Git repository, you first tell Git what files or directories to track for changes. This is also called adding files or directories to the Git repository.

You can check the status of your Git repository with the following command:

$ git status

As you can see, I have one untracked file index.php

You can add index.php file to the Git repository as follows:

$ git add index.php

Now git status says index.php is ready to commit.

You can add all the files and directories in your newly created Git repository as follows:

$ git add -A

Committing Changes to the Repository

Whenever you make any changes to a file in your Git repository, you must add it to your Git repository with git add command as I showed you earlier. Then you have to commit the changes to the repository as follows:

$ git commit -m 'A MESSAGE DESCRIBING WHAT YOU’VE CHANGED'

Checking All the Commits

You can check all the commits you’ve made with the following command:

$ git log
Or
$ git log --oneline

As you can see, my previous commit is listed.

Cloning a Git Repository

You can also clone an existing Git repository from GitHub or BitBucket. Just grab the Git repository URL and run the following command:

$ git clone YOUR_REPOSITORY_URL

The Git repository should be cloned.

A new directory should be created in the directory where you ran the command from as you can see:

If you navigate to the directory and check you should see all the commits of that Git repository:

That’s how you install and use Git on CentOS 7.5. Thanks for reading this article.

About the author

Shahriar Shovon

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.