Guide (Best in 2020)

GIT Commands  

The Ultimate Guide

Git commands are important. They play a major role in managing your source code effectively.

In fact, to know Git commands usage is an important skill. As a developer, you have to master this skill at some stage.

Bottom line... 

Without git commands and git best practices, you can not use git to its full potential.

Git commands are the backbone. And for a software developer, a must-have skill.

In this ultimate guide, I am going to show you all important git commands. With a bonus for you.

Keep reading.


Chapter 1:

Git Fundamentals

Let's start with Git Fundamentals.

In this chapter I will cover git basics.

Specifically, in this chapter, I'm going to cover what is GIT. And why GIT commands are SUPER important in 2020.

Let’s dive in

Git is popular and many companies are using it one of the example - software giant Microsoft use Git

So, there is no doubt Git is popular and powerful.

Git is a version control system (VCS). Its free and open source.

As Git is open source so you can modify and share Git source code

Git is different from many version control systems like CVS, SVN, Mercurial, Fossil etc. 


For many good reasons, let me explain. 

First important thing : Git is a distributed version control system

And that's why Git only needs local resources & files to operate most of the time.

(you can commit your changes without active internet)

distributed version control system - Git

Git stores snapshots of your committed files. And not the delta (difference from the previous commit). 

If there exist any file with the same content, then it's only stored once.

When you clone a repository, it creates a .git folder at the root of the repository.  

In Git, .git folder contains all the information which is necessary for your project.

Also, it contains all the information about commits, remote repository address etc.,

If you use ls (ls stands for list) command you can see list of all files inside .git folder.

Inside view of .Git Folder

(I will explain more git commands in details in the next chapters)  

Git is case-sensitive, as it was originally built by Linus Torvalds for Linux kernel's version control system Linus Torvalds also decided how Git got its name.

And that is one of the reason why Git is free and open source

As Windows has case-insensitive file system, and I found Git for windows and Git scm are case-insensitive on windows.

git scm download

For Git commands tutorial, I have used Git scm, version - 2.27.0

Also I prefer to use small case for all git commands.


To make sure same set of commands works on Linux.

Lets jump to the next chapter -  Basic Git Commands.  

Chapter 2:


Git Commands

This chapter is all about making everything SUPER easy for you to start with Git commands.

In this chapter I’ll show you how to start with Git commands.

.. And ... the good thing ..   

Once you Master the core concepts in Git, you can manage your project more proactively and effectively.

So let's start ...

After Installing Git 

1. After installing git the first thing you need to set up is your identity. Your name and email. 

This is important to setup.


Git commit uses this information. 

Set up a user name and user email with git config.

git config --global "Raj"
git config --global [email protected]
Set up a user name & email with git config

2. You can set up your favourite editor with git config like 

git config --global core.editor emacs

If you are on windows, you have to use the full path 

git config --global core.editor "'c:\program files\notepad++\notepad++.exe'"

 View your settings

git config --list

You can view all of your settings  and where they are coming from using:

git config --list --show-origin

View installed Git version 

git --version 

You can check your current version if git with git --version command

check git version with command line
Starting Your First Project with Git 

5. Git init command will create a new repository. Possibly this is the first command you will run to start your new project.

git init

Convert current directory into the git repository

git init [your directory]

Create an empty git repository in the specified directory

6. Cloning a repository (local or remote)

Internally, git clone using git init, to create a new repository

git clone

And here is how you can clone a repository from remote 

ssh://[email protected][username]/[repository-name].git
Git Essential Commands

There are many git commands and in this section I will focus on essential git commands.

7. Git status -  One of the important command. Mainly it shows 

  • Your current branch 
  • List of files which are committed or pending changes
  • Also,  you can get a list of individual files in untracked directories with 

    git status -u  ( or git status --untracked-files)

8. Git add command will use to add new files to the staging area.

Basically staging is a middle step between your working directory and repository.

Git Staging Area

You can add single or multiple files with git add command 

Lets start with adding a single file to the staging area

git add [your file name]
git add single file

Lets add multiple files

git add -A

Add all files to the staging area that are new or changed 

You can use git add . ( instead of git add -A )

git add .

9. Git commit - Record your local changes to your local  repository  

Git commit -m “your message here”

Here the -m option will set commit message for you 

Git Commit

10. Amend your last commit message

git commit --amend -m "New commit message"

In case you overwrite your last commit message. You can use --amend option 

11. Git clean command 

git clean  

It will remove the untracked files from the working directory .

Untracked files are those files that are in your repository directory but have not yet been added to the staging area

Chapter 3:

Git Inspection

This chapter is about Inspecting your code with Git Inspection commands.

In this chapter I’ll show you those git commands that are useful to inspect your Git repository.

And here you can learn how to inspect a specific commit, how to inspect history and more... 

So let's dive in ...

1. Git log : Git log command help you to see all previous commit messages

git log

This is how it will show on console  

Git log command

2. With summary option you can see all the changes in details 

git log --summary

You can view all difference of changes of each commit with -p option 

git log -p
Git log command with -p option

4. With stat option it will show you the number of lines added or removed on each commit.

git log --stat

 View only one line changes. It helps to have a quick look on previous commits.

git log --oneline
Git log command with -oneline option

6. You might have a long history of commits. And what if you are interested in a few recent commits?

Use -number of lines option with git log.

git log -2

And it will show you only  2 recent commits like this.

Git log command to view few recent commits

7. Also you can filter all commits of an author 

git log --author <name>

There is a git command to summarises git log command output. 

git shortlog

git shortlog command is SUPER helpful for the release announcement. 

Do you know how?

As each commit will be grouped by author and title.

And this command is my favourite one.

Have a look on the snapshot below .

Git shortlog command

9. View all commits by tags or branches

git log --graph --all --decorate

Inspect a specific commit

git show head

this command will show you commit information, diffstat and patch of the tip of the current branch

git show head command

11. Inspect History Visually 

In the above section, you learned the git log command from git-bash. And in this section, I will show you how you can visually inspect repositories. 

gitk command from git bash

Gitk is first of its kind graphical repository browser. And it is beneficial for you to visualising the history of a repository.

Gitk - graphical repository browser

This is about code inspection with Git.  And the next lesson you will know git commands for code comparison.

So keep reading

Chapter 4:

Git Comparison

Git Comparison is all about  -  what happens where.

In this chapter I’ll show you how you can compare two different branches.

And here you can learn  advance concept like git verify-commit, git blame and more.. 

Lets start .

Git provides a few powerful git commands to compare your code. And one of them is diff command.

It let you compare different versions of your files.

1. You can compare the different versions of a file. And git diff command is handy to compare files.

git diff head

Compare your working directory with local repository

 git diff [source branch] [target branch]

Compare two different branched in git

2. You might want to see commit information of a file for each line. Git Annotate command helps to view file lines with commit information.

And to use this command is straight forward 

git annotate <filename>

I prefer to use git blame. Git blame command is doing the same job as git annotate.

3. Git blame is a powerful command, and it helps you to see who changed what and why.

"Blame", sound like negative. But it's not to feel someone ashamed. You can use git blame to view who modified each line of a file and what revision of a file. It's checking the history of your code file.

git blame <filename>

The output will long, and you might need to restrict it to a few lines. You can use -L option; It will restrict the output to the requested line range.

git blame -L 1,10 <filename>

 In the above command, we are expecting output to show only lines 1 through 10.

4. Git Count-objects and git sizer

If you need to count git unpacked number of object you can use git count-object command.

git count-objects

It will only count "loose" objects - Only those objects that are stored individually in files named. It doesn't count objects that are "packed" together in pack files,

git sizer counts both (and it does not matter with git sizer that weather the objects are loose or packed).

git-sizer --verbose

By default, git-sizer outputs its results in tabular format. And below the screen shot of Linux repository, using the --verbose option

Git command - git sizer command

5. To View all branches and their commit use show-branch

git show-branch

With the git show-branch, you can see up to 29 branches and commits at a time.

6. There are git commands for you can check the GPG signature of commits. 

But why you need to check signature?

To make sure and verify that the source code has not changed since it packed.

git verify-commit -v

-v is for --verbose

In simple words, A digital signature certifies and timestamps a document. And the GPG signature is signed for that users can check and verify that the source code has not changed since its packed.

7. When you want to see commit logs with diff use git whatchanged.  

It's usage is straight forward like many other git commands

git whatchanged

In simple words, 'git log' shows each commit (author, date, message) whereas 'git whatchanged' shows the commit plus files that changed.

This command is a part of git because of historical reasons. And new users are encouraged to use git-log instead.

Chapter 5:


& Merging

In this chapter, I will cover git commands for branching and merging.

I’ll show you how to create, delete and view branches and the powerful branching model Git offers.

And here you can learn branching model, git merge and more..

So let's start

One of the most significant advantages of Git is - branching model.

You can create and manage Git branches very quickly.

Let's see what a branch is in Git?   

branch in Git is simply a lightweight movable pointer to one of these commits.

Git Branch

Branches allow you to work on a different version of files at parallel.

If you edit on one branch, it will be independent of work on another branch.

And then you can merge changes to another branch.

Git Merge

When you use git init, by default, a branch is created, which is master.

But it would be best if you create your own branch as per requirement. 

Branch Type


Master branch

Master branch is the default branch in Git. You always merge in this branch.

Development branch

In this branch, you start your development work.

Feature branch

As the name suggests, you create a feature branch for a new specific feature.

Bugfix branch

Bug branches usually to fix Release branches.

Hotfix branch

You create hotfix git branches for critical, out-of-cycle releases into production.

Release branch

Used for deploying release into the production system.

Here is one of the typical way you can use different branch types 

git branches

so lets start with the question - how to create a new branch in Git?

View branches in Git
git branch

This command will show all the git branches. The branch marked with an asterisk will be your current branch.

If you want to get only your current branch the simple command you can use is

git rev-parse --abbrev-ref head

You can also use the show-branch command to view branches and their commits

git show-branch
Create a branch in Git
git branch <feature_branch>

The above git command will create a branch in git.

If you want to switch and create a new branch, you can use the checkout command with -b option.

git checkout -b <feature_branch>
Delete a branch in Git

In the git repo, you have different branches for different purposes.

And once your work is complete on the feature, you often like to delete the branch. There are many git commands to delete a branch.

Please note here, you can not delete the active branch. In simple words If you are in feature_abc branch then you can not delete feature_abc  branch. You have to switch to a different branch.

Here is how you can delete a branch

git branch -d <branch>

To delete a branch locally there are two option

 1. You can use -d option, if your branch has already been pushed and merged with the remote branch.

2. You can use -D, if you want to force the branch to be deleted.

git branch -D <branch_name>
Switch to a different branch in Git

You can use git checkout command to navigate between the branches.Your branch should exist in the repo otherwise you will get an error.

git checkout <branch_name>

Also you can use git switch command to switch between the branches

git switch <branch_name>
git checkout command and git switch command

To add or delete a git branch remotely, you have to use push command which I will share later in next chapter - Collaboration.

In the next chapter I will show you git commands to push and pull your changes.

Merging in Git

You can use git merge command to integrate your changes from another branch

The target branch (the branch that receives changes) is always the currently checked out branch.

In case you want to merge hotfix branch to master, here is how you can do that. 

git merge hotfix branch

You need to merge in your master on regular intervals. 

Git merge is a new commit.

Git merge combine two different branches 

In regular commit, there is only one parent? But in the merge, there are two.

There are few things your should consider before executing the merge

  1. Make sure the target branch is up to date ( run git fetch to get latest remote changes)
  2. Execute git merge branch name ( it will be your source branch )
Git stash command

Stash In simple terminology, Stash command keeps your changes on a temporarily shelve that you don't need them at the moment, but you may need them.

You can view list of saved stashes with

git stash list

to apply a stash to the current branch

git stash apply

What about if you have many stashes? You have to specify which stash should apply with the number

git stash apply [email protected]{12}

In the next chapter, I will show you exactly, how you can share and manage your code remotely by using git commands.

so let's dive in

Chapter 6:

Git Collaboration

Let's do Git Collaboration.

It is also one of the important aspects to share your code with your team.

Specifically, in this chapter, I'm going to cover all those git commands to mange remote branches like git pull, git push and more... 

Let’s dive in

Before you use git pull or git push

You should have origin and upstream set up before you use git push

If you try to git push without any destination, you will get an error as below

git push show error if no push destination found

You can check which remote server you have configured you can always check with git remote command. With -v option you can see the URL that git has stored.

git remote -v
view remote configuration for git pull or git push command

In this case there is not any any remote server configured.

So let's first configure a remote URL (so we can share code) 

git remote add <shortname> <url>

Below you can see how easy to add a git URL with git remote command 

git demo remote url added

And now if you run again git origin command with -v option you can see the remote URL

git remote -v
view remote url with -v option

All this information stored in config under under.git folder

git config file

Now as you have remote URL setup, you can start sharing your code with others.

With git push,git pull command 

Lets keep reading

1. Git push is used to check in your changes in the remote repository.

git remote add origin <repository_url>

You should have origin and upstream set up before you use git push

Here is how you can setup upstream  

git push --set-upstream origin <branch_name>

Push your branch to the remote

git push -u origin feature_branch_name

2. To execute git pull command you need to write on the command prompt

git pull

In very simple words, git pull does a git fetch followed by a git merge.

3. git fetch command helps you to downloads all the objects and refs from another repository  

git fetch
Difference between Git fetch and Git pull 

git fetch 

This command only doing one thing - download the data from the remote repository.

It will not merge the downloaded changes with your repository.

So it's always safe to run git fetch at any time.

git pull 

It does a git fetch followed by a git merge.

a. Git pull tries to merge remote changes with your local changes so conflicts might happen. 

b. before running git pull, make sure there are no changes in the current working directory. 

This is all about git collaboration and in the next chapter you will learn top 5 git commands to boost your productivity.

so keep reading...

Chapter 7:

Git Productivity

(Bonus Cheat Sheet )

In this chapter I will cover how you can  boost your productivity with git commands.

Specifically, in this chapter, I'm going to cover available  tools to help with day to day git commands and important productivity hacks.

In this chapter, I have a bonus for you - git Cheat Sheet. Simply download and refer it later.

Lets start.. 

In this chapter I will show productive git commands.

These are graphics tool and help to boost your productivity.

1. The first graphics tool is git citool.

git citool

In simple words this is Graphical alternative to git-commit.

git citool productivity tool

2. You can cleanup unnecessary files with git gc (garbage collection) command. Also it will optimize your local repository. 

git gc

For example - if I run git gc command in my sample.project repository , the following message will display.

git gc tool

The git gc (garbage collection) command run automatically on several git commands like git pull, merge, commit. But you can invoke it manually with git gc command.

3. The gitk command will launch the Gitk user interface.

gitk command from gitbash

With gitk, you can get current state of the repository

  • You can see on the upper left panel commits to the repository
  • And on the lower right, you can see the list of files impacted by the selected commit.
  • On the lower left box you can see the commit details and full diff.
  • Once yo click a file in the lower right panel, it  focuses the diff in the lower left pane to the relevant section.
gitk user Interface

4. You can run merge conflict resolution tools to resolve merge conflicts

It general, you need to run git mergetool after git merge.

git mergetool

You can configure a mergetool of your choice. 

To know which editor are supported by git, use the help command 

git mergetool --tool-help

To configure Notepad++ as your preferred editor, using one of the below commands:

git config merge.tool npp
git mergetool --tool=npp
git mergetool -t npp

You can set a different path to notepad++ executable, by using the command:

git config mergetool.npp.path "C:\Program Files\Notepad++\notepad++.exe"

And you can run this git command like 

git mergetool --tool=npp

5. git reflog command takes many git commands, and different options 

git reflog <subcommand> <options>

The reflogs are useful in various Git commands. reflogs to specify the old value of a reference. 

Lets take an example
[email protected]{5} indicates - whern head used to be five moves ago.

git reflog git command

In this post I explain you all the main git commands.

And in this chapter I explain how you can boost your productivity with five git commands.

GIT Commands Bonus

You can download the git cheat sheet which you can use as a reference.  It contain 51+ useful git commands.

Download Git Commands Cheat Sheet

Now its your turn to share your thoughts. 

If you have any question do not feel hesitate to contact me 

how you like my post on git commands ..

.. Leave a comment and let me know ..

let me know


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}