How to install Git on Linux 9

Share on Social Media

Learn how to install Git on Linux 9 with our step-by-step guide. Master the process of setting up this essential version control system on your Linux distribution and streamline your development workflow effortlessly. #centlinux #linux #git

YouTube player

What is Version Control System?

A version control system (VCS) is a software tool that manages changes to files and directories over time. It allows multiple users to collaborate on projects by tracking modifications, recording who made the changes, and facilitating the merging of different versions of files.

In essence, a version control system enables developers to:

  1. Track Changes: VCS keeps a history of changes made to files, including what changes were made, who made them, and when they were made.
  2. Collaborate: Multiple developers can work on the same project simultaneously without interfering with each other’s work. VCS allows for seamless collaboration by managing conflicts and integrating changes made by different users.
  3. Revert to Previous Versions: Developers can revert to earlier versions of files if needed, allowing them to undo changes or restore previous states of the project.
  4. Branching and Merging: VCS allows developers to create branches, which are separate lines of development, to work on new features or experiments without affecting the main project. Branches can later be merged back into the main codebase.
  5. Backup and Disaster Recovery: By storing project history and files in a central repository, VCS provides a form of backup and disaster recovery. Even if files are lost or corrupted, developers can retrieve previous versions from the VCS.

Popular version control systems include Git, Subversion (SVN), Mercurial, and Perforce, among others. Git, in particular, is widely used in the software development industry due to its speed, flexibility, and distributed nature.

What is Git in Linux?

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

Features in Git

Git offers a variety of features that make it a powerful and versatile version control system for managing software development projects. Some of the key features of Git include:

  1. Distributed Version Control: Git is a distributed version control system, meaning every developer has a complete copy of the repository, including its full history. This allows for offline work and easy collaboration without relying on a central server.
  2. Branching and Merging: Git provides robust support for branching and merging, allowing developers to create isolated environments (branches) to work on new features or experiments. Branches can later be merged back into the main codebase seamlessly.
  3. Speed and Performance: Git is designed for speed and performance, enabling quick commits, branching, merging, and other operations even in large repositories.
  4. Data Integrity: Git uses cryptographic hashing to ensure the integrity of data stored in the repository. Each file and commit is checksummed, making it difficult for data corruption to go undetected.
  5. Staging Area (Index): Git includes a staging area (also known as the index) where changes to files can be reviewed and selectively added to the next commit. This allows for greater control over the commit process.
  6. Flexible Workflows: Git supports various workflows, including centralized, feature branching, and Gitflow, among others. This flexibility enables teams to adopt workflows that best suit their development processes.
  7. Support for Large Projects: Git handles large repositories and files efficiently, making it suitable for projects of all sizes, from small personal projects to large enterprise-scale applications.
  8. Integration with Other Tools: Git integrates seamlessly with a wide range of development tools and services, including IDEs, continuous integration (CI) systems, code hosting platforms (e.g., GitHub, GitLab, Bitbucket), and more.
  9. Open Source and Community Support: Git is an open-source project with a vibrant community of users and contributors. This means ongoing development, continuous improvement, and extensive support resources available online.

These features, among others, contribute to Git’s popularity and widespread adoption in the software development community.

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
  • 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 version control software has been installed. Now, to make it work as a git 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"

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>
Date: Tue Aug 1 21:16:31 2023 +0500

My Third Commit

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

My Second Commit

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

My First Commit

Useful Git Commands

Some useful variations of git commands example are:

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
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 commands 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 git server (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 git server 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 commands switches and their usage you can use following command.

# git help -a

Git Commands Cheat Sheet:

A comprehensive Git commands cheat sheet from Atlassian.com is attached below for your ready reference.

Final Thoughts

Learning how to install Git on Linux 9 opens doors to efficient version control and streamlined collaboration in your development projects. With Git’s robust features and seamless integration with Linux environments, you’re poised to elevate your workflow and productivity. Embrace the power of version control and empower your team to achieve greater success in software development endeavors on Linux. You can improve your understanding of the version control system and Git commands by attending the online training The Complete Git Guide: Understand and master Git and GitHubshow?id=oLRJ54lcVEg&bids=1060093

Leave a Comment