Cyberithub

Top 17 Popular GIT Commands Examples on Linux

In this tutorial, I will take you through top 17 GIT Commands Examples on Linux. Git is a popular and widely used source management system that greatly simplifies the development cycle. It enables users to create, use, and switch between branches for content development as easily as people create and switch between files in their daily workflow.

It is implemented using a fast, efficient architecture that allows for ease of experimentation and refinement of local changes in an isolated environment before sharing them with others. In short, it allows everyday users to focus on getting the content right instead of worrying about source management, while providing more advanced users with the ability to record, edit, and share changes at any level of detail.

Top 17 Popular GIT Commands Examples on Linux 1

GIT Commands

Also Read: GIT Tutorial: Install GIT and Clone Repository in 7 Easy Steps

1. Configure User using git config

The git config command is a command that is used to configure your Git settings. The minimum setting for Git is to set a username and email address. You can either configure each Git repository differently or configure the settings globally. If you set the configuration globally, you don’t have to configure the email address and username every time you initialize a Git repository. You can always override these in each repository if necessary.

[root@localhost ~]# git config user.email "test@example.com"

If you want to set the configuration globally, you need to add the --global keyword in below git commands:-

[root@localhost ~]# git config --global user.email "test@example.com"

You can also configure through User Name as shown below.

[root@localhost ~]# git config --global user.name "test"

2. Add file using git add

The git add command is a git commands that will add Modified files/folders to the Git tracking system. This means that the files and folders will be staged.

[root@localhost ~]# git add file
[root@localhost ~]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: file
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# .bash_history
# .bash_logout
# .bash_profile
# .bashrc
# .config/
# .cshrc
# .kube/
# .ssh/
# .tcshrc
# 1908
# 23 Aug
# anaconda-ks.cfg
# example.js
# example.pl
# example/
# file.txt
# file1.txt
# hellogitworld/
# helm-rbac.yaml
# my.active.firewall.rules
# perl/
# test.sh
# test.yaml

3. Commit Using git commit

The git commit command is used when you want to commit your code to the Git history. This means taking a snapshot of your code base and storing it in the Git database for future reference.

[root@localhost ~]# git commit

If you execute the preceding code, the default editor that was set for Git will open up and ask you to enter a message for the commit. There is also a shorter way of doing this. If you want to enter a message directly with the commit, you can run the following git commands:

[root@localhost ~]# git commit -m "Initial Commit"
[master (root-commit) d724a4f] Initial Commit
1 file changed, 1000 insertions(+)
create mode 100644 file

4. Check Log using git log

To check which commits have been made in the repository, you can use the following git commands:

[root@localhost ~]# git log
commit d724a4f4c61cecde8fce650ee2d66c8d65870eff
Author: Test User <ykumar@a10networks.com>
Date: Sat Feb 1 12:16:59 2020 -0500
Initial Commit

You can also check commit log of specific file using below command.

[root@localhost ~]# git log -- file
commit d724a4f4c61cecde8fce650ee2d66c8d65870eff
Author: Test User <ykumar@a10networks.com>
Date: Sat Feb 1 12:16:59 2020 -0500
Initial Commit

You can also check the committed code and commit message that corresponds to a particular Commit ID using below command.

[root@localhost ~]# git log "d724a4f4c61cecde8fce650ee2d66c8d65870eff"
commit d724a4f4c61cecde8fce650ee2d66c8d65870eff
Author: Test User <ykumar@a10networks.com>
Date: Sat Feb 1 12:16:59 2020 -0500

Initial Commit

5. Reset Repository state by git reset

The git reset command changes your repository and working directory to a known state. Specifically, git reset adjusts the HEAD ref to a given commit, and by default, updates the index to match that commit. If desired, git reset can also modify your working directory to mirror the revision of your project represented by the given commit.

[root@localhost ~]# git reset

You can also do soft reset by using --soft option

[root@localhost ~]# git reset --soft

You can also perform hard reset by using --hard option.

[root@localhost ~]# git reset --hard
HEAD is now at d724a4f Initial Commit

6. Check the branch using git branch

If you want to check the current branch you have, you can simply do that by running git branch command as shown below. Here * mark represents the current branch you are currently in.

[root@localhost ~]# git branch
* master

7. Add Branch

If you want to add a branch, you can do that by running git branch feature1 git commands.

[root@localhost ~]# git branch feature1

Check if it is added or not. Star(*) tells you which branch you are currently in.

[root@localhost ~]# git branch
feature1
* master

NOTE:

Please note that if you try to add some branch which is already there, then you will get below error.
[root@localhost ~]# git branch master
fatal: A branch named 'master' already exists.

8. Switch Branch Using git checkout

If you want to switch to different branch, then you need to use git checkout command. Here to switch to feature1 branch, you need to use git checkout feature1 command.

[root@localhost ~]# git checkout feature1
Switched to branch 'feature1'

9. Delete Branch Using -D Option

Let’s say you want to delete some branch after you are done with that branch, then you need to delete that branch using -D option. Here to delete feature1 branch, you need to first come out of this branch by switching to branch master using git checkout master command.

[root@localhost ~]# git checkout master
Switched to branch 'master'

Now delete the branch by using -D option as shown below.

[root@localhost ~]# git branch -D feature1
Deleted branch feature1 (was d724a4f)

NOTE:

Please note that if you try to delete some branch in which you are currently in. It will throw you below error.
[root@localhost ~]# git branch -D feature1
error: Cannot delete the branch 'feature1' which you are currently on.

10. Check difference in file Using git diff

To track the difference between two files, you need to use git diff command.

[root@localhost ~]# git diff
diff --git a/file b/file
index 1179824..38fc2b8 100644
--- a/file
+++ b/file
@@ -1,4 +1,4 @@
-1
+This is the change
2
3
4

11. Merging Branch Using git merge

If you want to integrate changes from another branch say feature1 in this case, then you to need to use git merge feature1 command.

[root@localhost ~]# git merge feature1
Updating 0496ed7..9867d1e
Fast-forward
file | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

12. Check Stash List Using git stash

If you want to list all the stashes created by git stash command, then you need to use git stash list command.

[root@localhost ~]# git stash list
[root@localhost ~]# git stash
Saved working directory and index state WIP on master: d724a4f Initial Commit
HEAD is now at d724a4f Initial Commit

13. Revert using git revert

If you want to create a new commit that undo the changes introduced by commit id f5ad9ef37d88ad412f90aa6a23f71e775982f4b8, then you need to use below git revert command.

[root@localhost ~]# git revert f5ad9ef37d88ad412f90aa6a23f71e775982f4b8
[feature1 7feaff2] Revert "bad commit"
1 file changed, 2 deletions(-)
delete mode 100644 file2

14. Performing pop 

Git has a command that allows you to save the current state of the local working repository and go back to the last committed revision with git stash.

This is really helpful when you have to develop an urgent fix. After this, you can restore the stashed changes and continue with your development.

[root@localhost ~]# git stash
[root@localhost ~]# git stash pop
Auto-merging file2
# On branch feature1
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: file2
#
# Changes not staged for commit:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: file
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# .bash_history
# .bash_logout
# .bash_profile
# .bashrc
# .config/
# .cshrc
# .gitconfig
# .kube/
# .ssh/
# .tcshrc
# 1908
# 23 Aug
# anaconda-ks.cfg
# example.js
# example.pl
# example/
# file.txt
# file1.txt
# hellogitworld/
# helm-rbac.yaml
# my.active.firewall.rules
# perl/
# test.sh
# test.yaml
Dropped refs/stash@{0} (500cbc019841f891a882bcd4b73ad7ae8096c289

15. Add All file for Commit

If you want to add all the files for next commit, then you need to use –all option with git add command as you can see below.

[root@localhost ~]# git add --all

Check status using git status.

[root@localhost ~]# git status
# On branch feature1
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: .bash_history
# new file: .bash_logout
# new file: .bash_profile
# new file: .bashrc
# new file: .config/configstore/update-notifier-npm.json
# new file: .cshrc
# new file: .gitconfig
# new file: .kube/cache/discovery/192.168.0.105_6443/admissionregistration.k8s.io/v1/serverresources.json
# new file: .kube/cache/discovery/192.168.0.105_6443/admissionregistration.k8s.io/v1beta1/serverresources.json
# new file: .kube/cache/discovery/192.168.0.105_6443/apiextensions.k8s.io/v1/serverresources.json
# new file: .kube/cache/discovery/192.168.0.105_6443/apiextensions.k8s.io/v1beta1/serverresources.json
# new file: .kube/cache/discovery/192.168.0.105_6443/apiregistration.k8s.io/v1/serverresources.json
# new file: .kube/cache/discovery/192.168.0.105_6443/apiregistration.k8s.io/v1beta1/serverresources.json
# new file: .kube/cache/discovery/192.168.0.105_6443/apps/v1/serverresources.json

16. Checking Status Using git status

To list out all the changes that take place during next commit, you need to run git status command as shown below.

[root@localhost ~]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: file
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# .bash_history
# .bash_logout
# .bash_profile
# .bashrc
# .config/
# .cshrc
# .kube/
# .ssh/
# .tcshrc
# 1908
# 23 Aug
# anaconda-ks.cfg
# example.js
# example.pl
# example/
# file.txt
# file1.txt
# hellogitworld/
# helm-rbac.yaml
# my.active.firewall.rules
# perl/
# test.sh
# test.yaml

17. Check Other Options Using git –help

If you do not know about all the options comes with git commands, you can check it by running git –help command as you can see below.

[root@localhost ~]# git --help
usage: git [--version] [--help] [-c name=value]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]

The most commonly used git commands are:
add Add file contents to the index
bisect Find by binary search the change that introduced a bug
branch List, create, or delete branches
checkout Checkout a branch or paths to the working tree
clone Clone a repository into a new directory

Reference: Professional GIT

Also Read: Hands on Object Oriented Programming with C#

Leave a Comment