How to install Git on Linux 9

Share on Social Media

In this Linux tutorial, you will learn how to install Git on Linux 9 that includes all Red Hat based Linux OS. #centlinux #linux #git

What is Git?:

Git is a distributed version control system used in software development. It tracks changes to source code and allows multiple developers to collaborate efficiently. Git stores a project’s history as a series of snapshots, enabling easy branching, merging, and rollback. It promotes collaboration, code integrity, and the ability to work offline. Developers can commit changes to their local repositories and synchronize with a central one. This decentralized nature ensures data redundancy and flexibility. Git’s popularity stems from its speed, scalability, and robustness, making it an essential tool for modern software development teams.

Read Also: How to install Git on CentOS 7

Video to install Git on Linux:

YouTube player

Environment Specification:

We are using a Rocky Linux 9 Operating System with following specifications.

  • CPU – 3.4 Ghz (2 cores)
  • Memory – 2 GB
  • Storage – 20 GB
  • Operating System – Rocky Linux release 9.2 (Blue Onyx)
  • Hostname – git-01.centlinux-com.preview-domain.com
  • IP Address – 192.168.116.125/24

Install Git on Linux:

Login to your Rocky Linux server as root user by using ssh command.

Check the Linux OS and Kernel versions.

# cat /etc/rocky-release
Rocky Linux release 9.2 (Blue Onyx)

# uname -r
5.14.0-284.18.1.el9_2.x86_64

Git software is available in standard yum repositories. So, you can install it by executing dnf command.

# dnf install -y git

After Git installation, check the version of git command.

# git --version
git version 2.39.3

Git Server Setup:

The Git software has been installed. Now, to make it work as a version control server, you need to perform some necessary configurations.

The first thing you need to do is set up your user details. These details, including your name and email, that are primarily used in Git commit messages.

# git config --global user.name "ahmer"
# git config --global user.email "ahmer@centlinux-com.preview-domain.com"

Create a directory for keeping local Git repositories.

# mkdir ~/git
# cd ~/git

Set the default Branch name for Git initialization purpose.

# git config --global init.defaultBranch master

You can enable the credential helper cache, to store your user details.

# git config --global credential.helper "cache --timeout=18000"

Initialize current directory as the local git repository by executing following command at Linux Bash prompt.

# git init
Initialized empty Git repository in /root/git/.git/

The above command creates a directory structure for your local git repository. You can verify this by using ls command.

# ls -a .git
.  ..  branches  config  description  HEAD  hooks  info  objects  refs

Git Command Usage:

Go to your git local repository and list down the files therein.

# cd ~/git
# ls

Currently, there isn’t any file.

Let’s create a new file.

# echo 123 > file1

Check the status of your local repository.

# git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        file1

nothing added to commit but untracked files present (use "git add" to track)

There is one untrack file. You need to add it to your git repository.

# git add file1

Check the status again.

# git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   file1

Now permantly save file to your git repository as follows.

# git commit -m "My First Commit"
[master 929553b] My First Commit
 1 file changed, 1 insertion(+)
 create mode 100644 file1

Check the status again.

# git status
On branch master
nothing to commit, working tree clean

Now modify your file1 and create a new file2.

# echo 456 >> file1
# echo 456 > file2

Check the status of your local repository.

# git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   file1

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        file2
        
no changes added to commit (use "git add" and/or "git commit -a")

Add file1 and file2 to your git repository.

# git add file1 file2

Check the status again.

# git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   file1
        new file:   file2

Commit changes to local repository.

# git commit -m "My Second Commit"
[master ca60397] My Second Commit
 2 files changed, 2 insertions(+)
 create mode 100644 file2

Check the status of your repository.

# git status
On branch master
nothing to commit, working tree clean

You can use git rm command to remove a file from repository.

# git rm file2
rm 'file2'

Check list of files in git repo.

# ls
file1

Check the status of git repo again.

# git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    file2

Commit changes to git repository.

# git commit -m "My Third Commit"
[master e08b9d1] My Third Commit
 1 file changed, 1 deletion(-)
 delete mode 100644 file2

View the log of commits to your local git repository.

# git log
commit e08b9d12be5ed67522ed807c9813668f32c50267 (HEAD -> master)
Author: ahmer <ahmer@centlinux-com.preview-domain.com>
Date:   Tue Aug 1 21:16:31 2023 +0500

    My Third Commit

commit ca603978fa4be24299433244b086dd965436862e
Author: ahmer <ahmer@centlinux-com.preview-domain.com>
Date:   Tue Aug 1 21:15:53 2023 +0500

    My Second Commit

commit 929553b61f22e09fd65e3cf09bd8ad281bce9290
Author: ahmer <ahmer@centlinux-com.preview-domain.com>
Date:   Tue Aug 1 21:14:53 2023 +0500

    My First Commit

Useful Git Commands:

You can check your git configurations anytime by executing following command at Linux bash prompt.

# git config --list
user.name=ahmer
user.email=ahmer@centlinux-com.preview-domain.com
init.defaultbranch=master
credential.helper=cache --timeout=18000
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true

To work with a remote Git repository, you must link your local repository to it.

You can use following git command format to link your local repository with a remote repository.

# git remote add origin <remote-repository-link>

Whenever you made changes in your files, you must use commit option to save them in your local repository.

Optionally, you can include a changelog message with each commit, serving as a quick note about the changes that have been made.

# git commit -m "changelog message"

To push your committed changes to a remote repository, you can use the following command.

# git push origin master

You can use the following command to retrieve the latest version from your remote repository to the local repository.

# git pull origin master

Use following command to check the status of your local git repository.

# git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

To get further help about git command switches and their usage you can use following command.

# git help -a

Conclusion – Install Git on Linux:

In this Linux tutorial, you have learned how to install git on Linux 9. You can improve your understanding of Git software by attending the online course The Complete Git Guide: Understand and master Git and GitHub