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
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:
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.
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
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:
These features, among others, contribute to Git’s popularity and widespread adoption in the software development community.
We are using a Rocky Linux 9 Operating System with following specifications.
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
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
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
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
A comprehensive Git commands cheat sheet from Atlassian.com is attached below for your ready reference.
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 GitHub
Learn how to change Apache document root in Linux by following this step-by-step guide. Adjust…
Discover how to change Apache port in Linux easily. Follow our simple guide to modify…
Learn how to create a virtual host in Apache Server with this comprehensive guide. Set…
Discover 10 practical tasks for the RHCSA exam with step-by-step solutions. Boost your Linux skills…
Discover the ultimate Fail2ban configuration guide. Learn how to set up, customize, and optimize Fail2ban…
Explore the ultimate guide to VPS servers and learn everything about Virtual Private Servers, including…
This website uses cookies.