In this article, we will review the top 10 most commonly used Git commands. You can’t call yourself a competent DevOps Engineer or IT professional
In this article, we will review installing and using Git on Linux machines. Besides minor differences in syntax, the install commands and procedures are similar across any of the major Linux distributions.
The “Git” in Git-SCM stands for “pig-headed”. It is a British slang term referring to someone who is argumentative and always thinks he or she is correct about any and everything! Linus Torvalds, the inventor of the Linux operating system, also created the Git version control system (VCS) as a way to better manage, track, and organize source code. “SCM” stands for source code management—another way of referring to VCS.
Depending on the flavor or type of Linux distribution (distro) you are running, Red Hat Enterprise Linux (RHEL), Community Enterprise Linux (CentOS), or a Debian-based distribution (e.g. Ubuntu), Git may already be installed on your system. You can double-check by running the following command(s):
$ rpm -qa | grep git
libglvnd-glx-1.0.1-0.8.git5baa1e5.el7.x86_64
crontabs-1.11-6.20121102git.el7.noarch
linux-firmware-20200421-80.git78c0348.el7_9.noarch
net-tools-2.0-0.25.20131004git.el7.x86_64
libglvnd-1.0.1-0.8.git5baa1e5.el7.x86_64
libglvnd-egl-1.0.1-0.8.git5baa1e5.el7.x86_64
Pay attention to the output here (above). As you can see, the rpm command to check for git does provide misleading results. This is because the grep function in the rpm command serves to highlight any rpm (out of the hundreds or thousands installed on your machine) with the “git” keyword anywhere in its name. The “git” in the output (above) is referring to the package releases of other rpms installed on the machine–and not the actually git package.
The yum info command is another way to verify a package. In this case, the output of this command (below) provides more information about the package or rpm.
$ yum info git
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.umd.edu
* extras: mirrors.advancedhosters.com
* updates: mirror.ette.biz
Available Packages
Name : git
Arch : x86_64
Version : 1.8.3.1
Release : 23.el7_8
Size : 4.4 M
Repo : base/7/x86_64
Summary : Fast Version Control System
URL : http://git-scm.com/
License : GPLv2
Description : Git is a fast, scalable, distributed revision control system with an
: unusually rich command set that provides both high-level operations
: and full access to internals.
:
: The git rpm installs the core tools with minimal dependencies. To
: install all git packages, including tools for integrating with other
: SCMs, install the git-all meta-package.
$ dpkg-query -W git
Another way to verify that a package is installed on your machine is by running the which command.
$ which git
/usr/bin/git
In this instance, we’re referring to Git. The output of the which command (above), indicates that git is installed. The output of the which command will show the /path/to/the/executable for that package. If git was not installed, you should see something like the output (below).
$ which git
/usr/bin/which: no git in (/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/admin/.local/bin:/home/admin/bin)
Installing Git on a Linux machine is simple and can be achieved using the yum command on a RHEL7 or CentOS7 distribution. Enter the following command (below) to install Git along with its dependencies.
$ sudo yum -y install git
On a Debian-based distribution like Ubuntu for example, you can install Git using the apt command.
$ sudo apt install git -y
On a RHEL8 or CentOS8 machine, you can install Git using the dnf command.
$ sudo dnf install git -y
Now that you have Git installed on your Linux machine, you’ll need to initialize it to start using it. Create and/or change directory (cd) to the location where you want to download or clone your first Git repository (repo).
$ mkdir -p my-git-repos
$ cd my-git-repos/
$ git init
Initialized empty Git repository in /home/admin/my-git-repos/.git/
We can find plenty of public repos to clone at Github.com–home to over 200 million repositories from 4+ millions organizations and 73+ million developers as of this posting. It the example below, we are cloning a GoLang repo called example.
Photo by admingeek from Infotechys
It contains a collection of programs that demonstrate the standard libraries, and tools of the Go programming language. The following commands below illustrate the process of cloning the Golang repository and checking out its contents:
$ git clone https://go.googlesource.com/example
Cloning into 'example'...
remote: Total 165 (delta 81), reused 165 (delta 81)
Receiving objects: 100% (165/165), 93.49 KiB | 0 bytes/s, done.
Resolving deltas: 100% (81/81), done.
$ ls -l
total 0
drwxrwxr-x. 9 admin admin 177 Jan 21 23:15 example
$ cd example
$ ls -l
total 24
drwxrwxr-x. 3 admin admin 67 Jan 21 23:15 appengine-hello
-rw-rw-r--. 1 admin admin 100 Jan 21 23:15 go.mod
-rw-rw-r--. 1 admin admin 2452 Jan 21 23:15 go.sum
drwxrwxr-x. 12 admin admin 229 Jan 21 23:15 gotypes
drwxrwxr-x. 2 admin admin 22 Jan 21 23:15 hello
-rw-rw-r--. 1 admin admin 11358 Jan 21 23:15 LICENSE
drwxrwxr-x. 2 admin admin 82 Jan 21 23:15 outyet
-rw-rw-r--. 1 admin admin 2600 Jan 21 23:15 README.md
drwxrwxr-x. 2 admin admin 47 Jan 21 23:15 stringutil
drwxrwxr-x. 2 admin admin 57 Jan 21 23:15 template
In the output above, we successfully cloned the example repo from the Golang Repository and can now begin to test it as well as contribute to it–if this project is one that we’re interested in, knowledgeable, and passionate about. We’ve only scratched the surface of what’s possible with Git. Here you can find commonly used Git commands that allow developers to leverage its capabilities as a collaborative source code management (SCM) tool.
Was this article helpful to you? If so, leave us a comment below and share!
Related Posts
In this article, we will review the top 10 most commonly used Git commands. You can’t call yourself a competent DevOps Engineer or IT professional
In this article, we review how to install VSCode on CentOS 7, providing step-by-step instructions to ensure a seamless setup of the Visual Studio Code
In this article, we will examine installing and using Gitlab on Ubuntu server version 20.04. Gitlab community edition or Gitlab CE can be installed by