Christmas Special : Upto 40% OFF! + 2 free courses  - SCHEDULE CALL

- DevOps Blogs -

19 Useful Git Commands For Developers to be Revealed

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:

  1. git config
  2. git init
  3. git clone
  4. git add
  5. git status
  6. git commit
  7. git diff
  8. git reset
  9. git rm
  10. git log
  11. git show
  12. git tag
  13. git branch
  14. git checkout
  15. git merge
  16. git remote
  17. git push
  18. git pull
  19. git stash

SQL Server Curriculum

Git Commands

1. git config

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]"

2. git init

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/

3. git clone

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.

4. git add

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 *

5. git commit

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(+)

6. git diff

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: DevOps Engineer Salary in India – What You Really Need to Know
 
$ 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

7. git reset

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

8. git status

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)

9. git rm

 

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: Top 7 DevOps Tools: Manage & Optimize The Organizational Task

10. git log

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

11. git show

 

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

12. git tag

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

13. git branch

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)
$

15. git merge

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(+)

16. git remote

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

17. git push

Syntax: git push [variable name] master

Read: DevOps Skills That Organization are Looking for DevOps Engineer

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

18. git pull

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.

19. git stash

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

SQL Server Quiz

Conclusion

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!



fbicons FaceBook twitterTwitter lingedinLinkedIn pinterest Pinterest emailEmail

     Logo

    Ish Kumar

    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.


Comments

Trending Courses

Cyber Security Course

Cyber Security

  • Introduction to cybersecurity
  • Cryptography and Secure Communication 
  • Cloud Computing Architectural Framework
  • Security Architectures and Models
Cyber Security Course

Upcoming Class

1 day 27 Dec 2024

QA Course

QA

  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing
QA Course

Upcoming Class

2 days 28 Dec 2024

Salesforce Course

Salesforce

  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL
Salesforce Course

Upcoming Class

4 days 30 Dec 2024

Business Analyst Course

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum
Business Analyst Course

Upcoming Class

1 day 27 Dec 2024

MS SQL Server Course

MS SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design
MS SQL Server Course

Upcoming Class

1 day 27 Dec 2024

Data Science Course

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning
Data Science Course

Upcoming Class

8 days 03 Jan 2025

DevOps Course

DevOps

  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing
DevOps Course

Upcoming Class

0 day 26 Dec 2024

Hadoop Course

Hadoop

  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation
Hadoop Course

Upcoming Class

2 days 28 Dec 2024

Python Course

Python

  • Features of Python
  • Python Editors and IDEs
  • Data types and Variables
  • Python File Operation
Python Course

Upcoming Class

1 day 27 Dec 2024

Artificial Intelligence Course

Artificial Intelligence

  • Components of AI
  • Categories of Machine Learning
  • Recurrent Neural Networks
  • Recurrent Neural Networks
Artificial Intelligence Course

Upcoming Class

9 days 04 Jan 2025

Machine Learning Course

Machine Learning

  • Introduction to Machine Learning & Python
  • Machine Learning: Supervised Learning
  • Machine Learning: Unsupervised Learning
Machine Learning Course

Upcoming Class

1 day 27 Dec 2024

 Tableau Course

Tableau

  • Introduction to Tableau Desktop
  • Data Transformation Methods
  • Configuring tableau server
  • Integration with R & Hadoop
 Tableau Course

Upcoming Class

2 days 28 Dec 2024

Search Posts

Reset

Receive Latest Materials and Offers on DevOps Course

Interviews