27
NovBlack Friday Deal : Up to 40% OFF! + 2 free self-paced courses + Free Ebook - SCHEDULE CALL
If you are one of those developers who is looking for version control commands to get your development work done, then this article is for you. In this blog, we’ll discuss top 19 commands that are useful for the developer which are a part of everyday development process and help developers to work efficiently.
To start with, let us first understand a little about SCM (Source Code Management). CTA’s
Source code management is a process where we can keep a track of the changes that are made to source code. SCM works by keeping a track of the changes made to a codebase. It helps in solving conflicts when multiple merges are made by many developers to the same code base repository. We often call it a Version Control System.
Git is one of the tools we use as a Source Code Management (SCM).
Below is a complete list of Git commands we will discuss today:
Syntax: git config –global user.name “[name]”
Syntax: git config –global user.email “[email address]”
This command is used to set the developer name and email address. This helps in making the commits back to the git repository.
Example:
DESKTOP-BMEDQ6H MINGW64 ~/Desktop/git (master)
$ git config --global user.name “[ishkapila]”
DESKTOP-BMEDQ6H MINGW64 ~/Desktop/git (master)
$ git config --global user.email "[ishcse@gmail.com]"
Syntax: git init [repository name]
This command helps in initializing a new repository.
DESKTOP-BMEDQ6H MINGW64 ~/Desktop/git (master)
$ git init “git-commands”
Initialized empty Git repository in C:/Users/IKapila/Desktop/git/git-commands/.git/
Syntax: git clone [url]
This command helps to clone an existing repository using the URL of the repository. DESKTOP-
BMEDQ6H MINGW64 ~/Desktop/git (master) $ git clone https://github.com/ishkapila/helloworld.git Cloning into 'Hello-World'... remote: Enumerating objects: 13, done. remote: Total 13 (delta 0), reused 0 (delta 0), pack-reused 13 Unpacking objects: 100% (13/13), done.
Syntax: git add [file]
This command helps to add a newly created on an existing file in the staging area.
DESKTOP-BMEDQ6H MINGW64 ~/Desktop/git/git-commands (master)
$ git add a.txt
Syntax: git add *
This command helps to add more than one file in the staging area.
DESKTOP-BMEDQ6H MINGW64 ~/Desktop/git/git-commands (master)
$ git add *
Syntax: git commit -m “[ Enter the commit message here]”
This command helps to permanently add the file in the git version history. -m is used to enter the message where we can mention the changes that went in this commit. This is helpful to find the correct commit in case we need to do a rollback.
DESKTOP-BMEDQ6H MINGW64 ~/Desktop/git/git-commands (master)
$ git commit -m "Added file a.txt"
[master (root-commit) e42e94a] Added file a.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a.txt
Syntax: git commit -a
This command helps to commit the files that we have added using the git add command.
Also, any files that have been modified to this step are committed.
This command opens a file in vi editor mode and asks you to add the commit message.
Once you enter the commit message save the file.
DESKTOP-BMEDQ6H MINGW64 ~/Desktop/git/git-commands (master)
$ git commit -a warning: LF will be replaced by CRLF in b.txt. The file will have its original line endings in your working directory [master 473959a] Adding b.txt 1 file changed, 1 insertion(+)
Syntax: git diff
This command helps to show the changes made in the files since the last commit was made. It also tells which file differences are not in staging yet.
DESKTOP-BMEDQ6H MINGW64 ~/Desktop/git/git-commands (master)
Read: Your Complete Guide to Jenkins Installation on Ubuntu
$ echo "Hello" >> a.txt
DESKTOP-BMEDQ6H MINGW64 ~/Desktop/git/git-commands (master)
$ echo "My name is Ish Kumar Kapila" >> a.txt
DESKTOP-BMEDQ6H MINGW64 ~/Desktop/git/git-commands (master)
$ git diff
warning: LF will be replaced by CRLF in a.txt.
The file will have its original line endings in your working directory
diff --git a/a.txt b/a.txt
index e69de29..66c5eaa 100644
--- a/a.txt
+++ b/a.txt
@@ -0,0 +1,2 @@
+Hello
+My name is Ish Kumar Kapila
Syntax: git diff –staged
This command helps to find the changes made in the files after the file was last committed.
Git add must be used and the file should be in a staging area before using this command.
DESKTOP-BMEDQ6H MINGW64 ~/Desktop/git/git-commands (master)
$ git add.
warning: LF will be replaced by CRLF in a.txt.
The file will have its original line endings in your working directory
DESKTOP-BMEDQ6H MINGW64 ~/Desktop/git/git-commands (master)
$ git diff --staged
diff --git a/a.txt b/a.txt
index e69de29..66c5eaa 100644
--- a/a.txt
+++ b/a.txt
@@ -0,0 +1,2 @@
+Hello
+My name is Ish Kumar Kapila
Syntax: git diff [first branch] [second branch]
This command helps in finding the difference between the 2 branches.
ikapila@ikapila-WX-1 MINGW64 /c/My docs (branch-2)
$ git diff branch-1 branch-2
diff --git a/branch1.txt b/branch1.txt
index 1193ff4..e69de29 100644
--- a/branch1.txt
+++ b/branch1.txt
@@ -1 +0,0 @@
-branch1
diff --git a/branch2.txt b/branch2.txt
new file mode 100644
index 0000000..c79b95b
--- /dev/null
+++ b/branch2.txt
@@ -0,0 +1,2 @@
+aaaa<
+branch2
Syntax: git reset [file]
This command helps to remove the file from the staging state and preserves its data as well.
ikapila@ikapila-WX-1 MINGW64 /c/My docs (branch-2)
$ git add a.txt
ikapila@ikapila-WX-1 MINGW64 /c/My docs (branch-2)
$ git status
On branch branch-2
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: a.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
ikapila@ikapila-WX-1 MINGW64 /c/My docs (branch-2)
$ git reset a.txt
ikapila@ikapila-WX-1 MINGW64 /c/My docs (branch-2)
$ git status
On branch branch-2
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
nothing added to commit but untracked files present (use "git add" to track)
Syntax: git reset –soft [commit]
This command helps to roll back all the commits after a specific commit id but preserves the local updated data and changes in the file.
ikapila@ikapila-WX-1 MINGW64 /c/My docs (branch-2)
$ git log
commit 4007a2c6b9837c0b32fc5e4a7ef7f3c547788be7 (HEAD -> branch-2) Author: Ish Kumar Kapila <ikapila@gmail.com> Date: Thu Nov 14 19:42:36 2019 +0530 new commit branch2 commit acf5c89178422256d38fd99cba01da08c069847a Author: Ish Kumar Kapila <ikapila@gmail.com> Date: Thu Nov 14 07:16:25 2019 +0530 branch1.txt added ikapila@ikapila-WX-1 MINGW64 /c/My docs (branch-2) $ git reset --soft acf5c89178422256d38fd99cba01da08c069847a ikapila@ikapila-WX-1 MINGW64 /c/My docs (branch-2) $ git log commit acf5c89178422256d38fd99cba01da08c069847a (HEAD -> branch-2) Author: Ish Kumar Kapila <ikapila@gmail.com> Date: Thu Nov 14 07:16:25 2019 +0530 branch1.txt added ikapila@ikapila-WX-1 MINGW64 /c/My docs (branch-2) $ ll total 2089 -rw-r--r-- 1 ikapila 1049089 0 Nov 14 19:17 branch1.txt -rw-r--r-- 1 ikapila 1049089 11 Nov 14 19:41 branch2.txt
Syntax: git reset –hard [commit]
This command helps to clean the history and all changes made after this commit id and the head moves to this specific commit id.
ikapila@ikapila-WX-1 MINGW64 /c/My docs (branch-2)
$ git log
commit 0a43a9bf7bd21b096cd05a4223ff9a16ffa41855 (HEAD -> branch-2)
Author: Ish Kumar Kapila <ikapila@gmail.com>
Date: Thu Nov 14 19:43:42 2019 +0530
nnnnnnnnn
commit acf5c89178422256d38fd99cba01da08c069847a
Author: Ish Kumar Kapila <ikapila@gmail.com>
Date: Thu Nov 14 07:16:25 2019 +0530
branch1.txt added
ikapila@ikapila-WX-1 MINGW64 /c/My docs (branch-2)
$ git reset --hard acf5c89178422256d38fd99cba01da08c069847a
HEAD is now at acf5c89 branch1.txt added
ikapila@ikapila-WX-1 MINGW64 /c/My docs (branch-2)
$ ll
total 2088
-rw-r--r-- 1 ikapila 1049089 0 Nov 14 19:17 branch1.txt
Syntax: git status
This command helps to find all the changes that need to be committed.
ikapila@ikapila-WX-1 MINGW64 /c/My docs (branch-2)
$ git status
On branch branch-2
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: branch2.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
Syntax: git rm --cached [file]
ikapila@ikapila-WX-1 MINGW64 /c/My docs (branch-2)
$ git status
On branch branch-2
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: 2.txt
ikapila@ikapila-WX-1 MINGW64 /c/My docs (branch-2)
$ git rm --cached 2.txt
rm '2.txt'
Syntax: git rm -f [file]
This command is used to delete a file from staging state and from the local file system.
ikapila@ikapila-WX-1 MINGW64 /c/My docs (branch-2)
$ git status
On branch branch-2
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: 2.txt
ikapila@ikapila-WX-1 MINGW64 /c/My docs (branch-2)
$ git rm -f 2.txt
rm '2.txt'
file 2.txt is removed from local file system.
Read: Ansible vs Puppet - What Should You Know?
Syntax: git log
This command is helpful to check the history of all the commits in the branch.
ikapila@ikapila-WX-1 MINGW64 /c/My docs (branch-2)
$ git log
commit f887f0bb2bd63f18fe07d371eb6f1682e9ac7c6d (HEAD -> branch-2)
Author: Ish Kumar Kapila <ikapila@gmail.com>
Date: Thu Nov 14 20:39:15 2019 +0530
a.txt
commit 4398abafcad73c13669b980a41b3c6a4bc338945 Author: Ish Kumar Kapila <ikapila@gmail.com> Date: Thu Nov 14 20:38:19 2019 +0530 branch1.txt 222 commit 802e9f01260fc0b0ca6c1f798ce5d60fd8648ab8 Author: Ish Kumar Kapila <ikapila@gmail.com> Date: Thu Nov 14 20:37:47 2019 +0530 branch1.txt 111 Syntax: git log –follow[file]
This command is helpful in checking all the versions of a file.
ikapila@ikapila-WX-2 MINGW64 /c/MyDocs/Janbask/git commands (master)
$ git log --follow a.txt
commit 4802f4685d7f64c60f6521be41e9bebe56103645 (HEAD -> master)
Author: Ish Kumar Kapila <ikapila@gmail.com>
Date: Sat Nov 16 05:54:11 2019 +0530
111
commit 2941520b8a8a487c2814d215cd17909a3b96d5b9
Author: Ish Kumar Kapila <ikapila@gmail.com>
Date: Sat Nov 16 05:54:00 2019 +0530
11
commit 932562c6de3894ae6ae2ce737287fba7eedf908c
Author: Ish Kumar Kapila <ikapila@gmail.com>
Date: Sat Nov 16 05:53:45 2019 +0530
1
Syntax: git show [commit]
This command helps in checking the details and the changes that were made in a specified commit.
ikapila@ikapila-WX-2 MINGW64 /c/MyDocs/Janbask/git commands (master)
$ git show 4802f4685d7f64c60f6521be41e9bebe56103645
commit 4802f4685d7f64c60f6521be41e9bebe56103645 (HEAD -> master)
Author: Ish Kumar Kapila <ikapila@gmail.com>
Date: Sat Nov 16 05:54:11 2019 +0530
111
diff --git a/a.txt b/a.txt
index c6dd273..df7d416 100644
--- a/a.txt
+++ b/a.txt
@@ -1,2 +1,3 @@
hi a
hi aa
+hi aaa
Syntax: git tag [commitID]
This command helps to create the tag of a commit id.
ikapila@ikapila-WX-2 MINGW64 /c/MyDocs/Janbask/git commands (master)
$ git tag -a v1.0 4802f4685d7f64c60f6521be41e9bebe56103645 -m "first tag"
Syntax: git tag
This command is helpful to see all the tags in a branch.
ikapila@ikapila-WX-2 MINGW64 /c/MyDocs/Janbask/git commands (master)
$ git tag v1.0
Syntax: git show [tag]
This command is helpful to show all the details of a tag.
ikapila@ikapila-WX-2 MINGW64 /c/MyDocs/Janbask/git commands (master)
$ git show v1.0 tag v1.0 Tagger: Ish Kumar Kapila <ikapila@gmail.com> Date: Sat Nov 16 06:03:36 2019 +0530 first tag commit 4802f4685d7f64c60f6521be41e9bebe56103645 (HEAD -> master, tag: v1.0) Author: Ish Kumar Kapila <ikapila@gmail.com> Date: Sat Nov 16 05:54:11 2019 +0530 111 diff --git a/a.txt b/a.txt index c6dd273..df7d416 100644 --- a/a.txt +++ b/a.txt @@ -1,2 +1,3 @@ hi a hi aa +hi aaa
Syntax: git branch
This command lists all the local branches in the current repository.
ikapila@ikapila-WX-2 MINGW64 /c/MyDocs/Janbask/git commands (master)
$ git branch
branch1
* master
Syntax: git branch [branch name]
This command helps create a new branch.
ikapila@ikapila-WX-2 MINGW64 /c/MyDocs/Janbask/git commands (master)
$ git branch branch2
Syntax: git branch -d [branch name]
This command helps in deleting a branch locally.
ikapila@ikapila-WX-2 MINGW64 /c/MyDocs/Janbask/git commands (master)
$ git branch -d branch2
Deleted branch branch2 (was 4802f46)
14. git checkout
Syntax: git checkout [branch name]
This command helps switch from one branch to another.
ikapila@ikapila-WX-2 MINGW64 /c/MyDocs/Janbask/git commands (master)
$ git checkout branch1
Switched to branch 'branch1'
ikapila@ikapila-WX-2 MINGW64 /c/MyDocs/Janbask/git commands (branch1)
$
Syntax: git checkout -b [branch name]
This command does 2 jobs, it helps to create the branch and checkout in it at the same time.
ikapila@ikapila-WX-2 MINGW64 /c/MyDocs/Janbask/git commands (branch1)
$ git checkout -b branch3
Switched to a new branch 'branch3'
ikapila@ikapila-WX-2 MINGW64 /c/MyDocs/Janbask/git commands (branch3)
$
Syntax: git merge [branch name]
This command is helpful to merge the changes made in a Branch B (branch1) to Branch A ( current branch/master ).
ikapila@ikapila-WX-2 MINGW64 /c/MyDocs/Janbask/git commands (master)
$ git merge branch1
Updating 4802f46..cf9da0e
Fast-forward
a.txt | 1 +
1 file changed, 1 insertion(+)
Syntax: git remote add [variable name] [Remote Server Link]
This command is helpful to link the working repository to the remote one.
ikapila@ikapila-WX-2 MINGW64 /c/MyDocs/Janbask/git commands (master)
$ git remote add origin https://github.com/Ish1984/gitcommands.git
Syntax: git push [variable name] master
Read: Unpack Docker: What It Is, Why It's Popular, and Essential Commands
This command is helpful to upload or update the changes in the current repository to the remote master branch.
ikapila@ikapila-WX-2 MINGW64 /c/MyDocs/Janbask/2 (master)
$ git push origin master Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 4 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 282 bytes | 282.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://github.com/Ish1984/gitcommands.git 4acee50..1f2b8f7 master -> master
Syntax: git push [variable name] [branch]
This command is helpful to upload or update the changes in the current repository to the remote specific branch. (branch1)
ikapila@ikapila-WX-2 MINGW64 /c/MyDocs/Janbask/2 (branch1)
$ git push origin branch1 Enumerating objects: 3, done. Counting objects: 100% (3/3), done. Delta compression using up to 4 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (2/2), 269 bytes | 269.00 KiB/s, done. Total 2 (delta 0), reused 0 (delta 0) remote: remote: Create a pull request for 'branch1' on GitHub by visiting: remote: https://github.com/Ish1984/gitcommands/pull/new/branch1 remote: To https://github.com/Ish1984/gitcommands.git * [new branch] branch1 -> branch1
Syntax: git pull [Repository Link]
This command helps pull and combine the changes from the remote to the local repository.
ikapila@ikapila-WX-2 MINGW64 /c/MyDocs/Janbask/2 (master)
$ git pull https://github.com/Ish1984/gitcommands.git From https://github.com/Ish1984/gitcommands * branch HEAD -> FETCH_HEAD
Already up to date.
Syntax: git stash save
This command helps store the modified files temporarily.
ikapila@ikapila-WX-2 MINGW64 /c/MyDocs/Janbask/2 (branch1)
$ touch d.txt
ikapila@ikapila-WX-2 MINGW64 /c/MyDocs/Janbask/2 (branch1)
$ touch e.txt
ikapila@ikapila-WX-2 MINGW64 /c/MyDocs/Janbask/2 (branch1)
$ git add d.txt
warning: LF will be replaced by CRLF in d.txt.
The file will have its original line endings in your working directory
ikapila@ikapila-WX-2 MINGW64 /c/MyDocs/Janbask/2 (branch1)
$ git add e.txt
warning: LF will be replaced by CRLF in e.txt.
The file will have its original line endings in your working directory
ikapila@ikapila-WX-2 MINGW64 /c/MyDocs/Janbask/2 (branch1)
$ git stash save
Saved working directory and index state WIP on branch1: 1231e99 b.txt
Here: 1231e99 b.txt are the last commit id and last file committed.
Syntax: git stash pop
This command helps pop up the last stashed content.
ikapila@ikapila-WX-2 MINGW64 /c/MyDocs/Janbask/2 (branch1)
$ git stash pop
On branch branch1
Changes to be committed:
(use "git restore --staged <file>..." to unstage) new file: d.txt new file: e.txt
Syntax: git stash list
This command helps list all the stashed changes.
ikapila@ikapila-WX-2 MINGW64 /c/MyDocs/Janbask/2 (branch1)
$ git stash list
stash@{0}: WIP on branch1: 1231e99 b.txt
Syntax: git stash drop
This command is helpful to drop all the stashed changes.
ikapila@ikapila-WX-2 MINGW64 /c/MyDocs/Janbask/2 (branch1)
$ git stash drop Dropped refs/stash@{0} (6937564de08aa19bbd5538c758a20ce1f9f7c946) ikapila@ikapila-WX-2 MINGW64 /c/MyDocs/Janbask/2 (branch1) $ git stash list
With the above write-up, I have revealed usage of top 19 useful Git commands. If you want to learn more about it regarding how to implement these commands in your DevOps project or you want to learn more commands, then get yourself registered with JanBask Training DevOps training and give your development career a lead with DevOps certification. Happy coding!
A DevOps Engineer by profession and a Technical Blogger by passion. I love to learn anything about DevOps and Cloud computing and at the same time share the knowledge among those who are willing to learn and share the same passion with me.
Cyber Security
QA
Salesforce
Business Analyst
MS SQL Server
Data Science
DevOps
Hadoop
Python
Artificial Intelligence
Machine Learning
Tableau
Search Posts
Related Posts
What is DevOps Lifecycle? Devops Lifecycle Phases Plan & Measure 420.9k
Docker Explained – An Introductory tutorial for Docker 3.8k
Boost Your Career With 150 DevOps Interview Questions and Answers 156.9k
Unpack Docker: What It Is, Why It's Popular, and Essential Commands 784k
Periodic Table Of DevOps Tools - A Revolutionary Breakthrough 525.9k
Receive Latest Materials and Offers on DevOps Course
Interviews