
51+ Best Git Commands
Definitive Guide
Git commands are essential. They play a significant role in managing your source code effectively.
To know Git commands and use Git best practices is an important skill. As a developer, you have to master this skill at some stage.
Bottom line...
Git commands are the backbone. And for a software developer, a must-have skill.
Download 51 Git Commands
It is a long post.
So, I've created a downloadable pdf with a one-line summary of these git commands.
You can download the 51 Git Commands from here.

Download Git Commands Cheat Sheet
In this ultimate guide, I will show you all essential 51 Git commands with a bonus.
Keep reading...
Git Commands - Basic Git Commands
In this basic git commands section, I will cover all the git commands you are already using on a day to day basis.
Git is a distributed version control system. As Git is open source so you can modify and share Git source code.
Git only needs local resources & files to operate most of the time.
(you can commit your changes without active internet)

#1. git config

git config command
After installing Git, you need to set up your identity:
- Your name and
- Your email addres.
Git config helps to get and set global options.
git config --global user.name "Rajeev Bera"
git config --global user.email [email protected]

It is important to set up your name and email.
Why?
Git commit uses this information.
You can set up your favorite editor with git config like
git config --global core.editor emacs
If you are on windows, you have to use the full path of your favourite program. Here I am using notepad++
git config --global core.editor "'c:\program files\notepad++\notepad++.exe'"
#2. git init

git init command
Git init command will create a new repository.
Possibly this is the first command you will run to start your new project.
Following git command will convert current directory into your git repository.
git init
Create an empty git repository in the specified directory
git init [your directory]
#3. git clone

git clone command
Cloning a repository (local or remote).
Internally, git clone using git init, to create a new repository.
git clone
And this is how you can clone a repository from remote.
ssh://[email protected]/[username]/[repository-name].git
#4. git add

git add command
Git add command will be used to add new files to the staging area.
Staging is a middle step between your working directory and repository.
You can add single or multiple files with git add command.
Let’s start by adding a single file to the staging area.
git add [your file name]

Let’s add multiple files. The following command adds all the files to the staging area that are new or changed.
git add -A
You can use git add . ( instead of git add -A )
Like - git add .
Staging area in Git
Basically staging is a middle step between your working directory and repository.

#5. git status

git status command
Git status is one of the important git commands.
Mainly git status shows
- Your current branch.
- List of files that are committed or pending changes.
git status
Also, you can get a list of individual files in untracked directories with
git status -u or git status --untracked-files
#6. git commit

git commit command
Record your local changes to your local repository.
git commit -m “your message here.”
Here the -m option will set a commit message for you.
And you can amend your last commit message with the amend option.
git commit --amend -m "New commit message."
#7. git clean

git clean command
It will remove the untracked files from the working directory.
git clean
Untracked files are those in your repository directory but have not yet been added to the staging area.
#8. git version

git version command
You can check the current version of git with git --version command.
git --version
I am using used Git scm, version - 2.27.0
Also I prefer to use small case for all git commands.
Why?

To make sure same set of commands works on Linux.
Git is case-sensitive, as it was originally built by Linus Torvalds for Linux kernel's version control system.

Git Commands - Git Inspection Command
In this section, I will cover all those git commands that are super helpful in inspecting your code.
And here, you can learn how to inspect a specific commit, examine the history, and more.
So let's dive in...
#9. git log

git log command
Git log command helps you to see all previous commit messages.
git log
See below the screenshot, how it will show on the console.

With the summary option, you can see all the changes in detail.
git log --summary
You can view all difference of changes of each commit with -p option
git log -p

View only one line changes. It helps to have a quick look at previous commits.
git log --oneline

#10. git shortlog

git shortlog command
There is a git command to summarise the git log command output.
git shortlog
It is one of the git commands that are super helpful in the release announcement.
Do you know how?
Author and title will group each commit. And this command is my favorite one.
Have a look at the snapshot below.

#11. git show

git show command
If you want to inspect a specific commit, git show is super helpful.
git show head
This command will show you commit information, diffstat, and patch of the tip of the current branch

#12. git diff

git diff command
You can compare the different versions of a file. And git diff command is handy to compare files.
Compare your working directory with the local repository.
git diff head
Compare two different branches in git.
git diff [source branch] [target branch]
Git Commands - Git Comparison
Git Comparison is all about - what happens where.
In this section, I’ll show you how you can compare two different branches.
And here you can learn advanced concepts like git verify-commit, git blame, and more...
Let’s start.
#13. git annotate

git annotate command
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 insted of git annotate.
Git blame command is doing the same job as git annotate.
#14. git blame

git blame command
Git blame is a powerful command, and it helps you to see who changed what and why.
"Blame,” sounds negative. But it's not to make someone feel 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 limit 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.
#15. git count-objects

git count-objects command
If you need to count the unpacked git number of objects, 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.
#16. git-sizer

git-sizer command
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 screenshot of Linux repository, using the --verbose option.

#17. git show-branch

git show-branch command
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.
#18. git verify-commit

git verify-commit command
There are git commands for you can check the GPG signature of commits.
But why do you need to check the signature?
To make sure and verify that the source code has not changed since it was 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 so users can check and verify that the source code has not changed since it's packed.
#19. git whatchanged

git whatchanged command
When you want to see commit logs with diff use git whatchanged.
Its usage is straight forward as 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.
Git Commands - Git Branching & Merging
Git Comparison is all about - what happens where.
In this section, I’ll show you how you can compare two different branches.
And here you can learn advanced concepts like git verify-commit, git blame, and more useful git commands.
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?
A branch in Git is simply a lightweight movable pointer to one of these commits.

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.

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 | Description |
---|---|
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. |
So lets start with branching & merging Git commands...
#20. git branch

git branch command
Git branch command will show all the git branches. The branch marked with an asterisk will be your current branch.
git branch
Delete a branch locally.
There are two options.
1. You can use -d option if your branch has already been pushed and merged with the remote branch.
git branch -d <branch>
2. You can use -D, if you want to force the branch to be deleted.
git branch -D <branch_name>
#21. git rev-parse

git rev-parse command
If you want to get only your current branch the simple command you can use is
git rev-parse --abbrev-ref head
#22. git checkout

git checkout command
If you want to switch and create a new branch, you can use the checkout command with -b option.
git checkout -b <feature_branch>
#23. git switch

git switch command
This git command provides a very simple and clean alternative to "checkout".
Git switch command has a very clear purpose - switching and creating branches!
(i) Navigate between the branches
git switch <branch-name>
(ii) Create a new branch ( with -c option)
git switch -c <new-branch-name>
Here the new-branch-name is the name of a new local branch you want to create.
#24. git merge

git merge command
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 branches to master, here is how you can do that.

You need to merge with your master on regular intervals.
Git merge is a new commit.
Git merge combine two different branches
In a 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
Make sure the target branch is up to date ( run git fetch to get latest remote changes)
Execute git merge branch name ( it will be your source branch )
#25. git stash

git stash command
Stash command keeps your changes on a temporary shelf.
Stash, in simple terminology, this will keeps your changes on a temporary shelf that you don't need 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}
Git Commands - Git Collaboration
Git Collaboration is also one of the important aspects to share your code with your team.
Specifically, in this section, I'm going to cover all those Git commands, which are super helpful in managing your remote branches and more...
Let’s dive in
#26. git push

git push command
In simple words, git push command is used to send your local changes to remote repo.
pushing exports commits to remote branches.
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
#27. git pull

git pull command
The git pull command is doing two things at the same time.
Git pull command will download content from a remote repo and immediately update your local repository.
git pull <remote>
In very simple words, git pull does a git fetch followed by a git merge.
git pull = git fetch + git merge
#28. git fetch

git fetch command
The git fetch command is one of the important git commands of collaborative git workflows.
And the git fetch command is used to pull the updates from the remote.
git fetch command helps you to downloads all the objects and refs from another repository
(i) To fetch the remote repository
git fetch
(ii) To fetch all the branches simultaneously
git fetch -all
Difference between Git fetch and Git pull
Git fetch
This command only does 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.
#29. git remote

git remote command
A remote repository in Git is a common repository, where all team members exchange their changes.
And git remote command helps you to manage connections to those remote repositories.
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
To add a new remote, use the git remote add command.
git remote add <shortname> <url>
The git remote add command takes two arguments: A unique remote name and remote URL
Below you can see how easy to add a git URL with git remote command

#30. git cherry-pick

git cherry-pick command
Cherry picking in Git means to choose a commit from one branch and apply it to some other branch.
Cherry picking is the act of picking a commit from a branch and applying it to another. git cherry-pick can be useful for undoing changes.
Here is how you can use git cherry-pick command
First make sure sure you are on the branch, where you want to apply the commit. And then execute the following command
git cherry-pick <commit-hash>
git cherry-pick is a powerful command but not always a best practice. If not used correctly git cherry command might cause duplicate commits.
Sometimes a pull request might get closed without merging into master and in that case It's helpful to restore commits with cherry-pick git command.
Git Commands - Advanced Git Commands
It’s time to shift gear a little bit.
In this section of Git commands, I will show you more powerful features of Git.
You can use these Git commands in your workflow and work more efficiently.
Keep reading...
#31. git bisect

git bisect command
git bisect is one of the powerful and useful git commands.
It is fast as it uses binary search to find the commit that introduced a bug.
Let’s say there is a bug in your code base introduced by your team member and you are unsure when it happened.
If you can find a commit where the code is good and another commit where it doesn’t. Then git bisect will help you to trace the commit that caused the issue.
You'll use the git commands like this:
# start up git bisect
git bisect start
# give git a commit where there is not a bug
git bisect good a123
# give git a commit where there is a bug
git bisect bad z123
After finding the commit that introduced the bug, you can use git bisect reset to reset things back to the original.
#32. git rebase

git rebase command
There are two powerful git commands that integrate changes from one branch to another.
One is git merge and second is git rebase.
Git merge is always recording changes moving forward and git rebase has a powerful history rewriting feature.
Git rebase reapply commits on top of another base
Git rebase command takes the commits in your current working branch and applies them to the head of the passed branch.
Git rebase <base>
#33. git reset

git reset command
The git reset command is one of the powerful git commands. It helps you to undo changes.
The git reset command allows you to RESET your current head to a specified state. You can reset the state of specific files as well as an entire branch.
Git reset comes in various flavors:- Soft, Mixed and Hard
And it's easy to use git reset command like -
git reset
git reset --hard HEAD
#34. git restore

git restore command
git restore can be used to reset files to certain revisions.
git-restore is a one of the useful git commands that can revert uncommitted changes (changes in your working copy or in staging area).
Git restore command will not update your branch. This command can also be used to restore files in the index from another commit.
git restore --staged <file name>
#35. git revert

git revert command
Git revert is like a undo command .In simple words revert command revert some existing commits.
git revert simply creates a new commit that is the opposite of an existing commit.
It leaves the files in the same state as if the commit that has been reverted never existed.
git revert HEAD
Git revert vs Git restore vs Git reset
Git-revert command
- git-revert command will make a new commit that reverts the changes made by other commits.
Git restore command
- Git restore command will restore files in the working tree
- This command will not update your branch.
- With the help of git restore command you can also restore files in the index from another commit
Git-reset command
- Git reset command is about updating your branch
- This command can change the commit history.
- Git reset can also be used to restore the index, overlapping with git restore.
#36. git rm

git rm command
git rm is used to remove a file from a Git repository.
With the following command you can remove a file both from the Git repository and the filesystem.
git rm <your file name>
And in case if you only want to remove a file from the repository you can use the --cached option. And the file will not be deleted from the filesystem.
git rm<your file name> --cached
#37. git tag

git tag command
In Git, tags allow you to identify specific releases of your code.
And to create a tag on your current branch, run this:
git tag <tagname>
You can add a description with your tag by using -a option
git tag <tagname> -a
There are two types of tags in Git: annotated and lightweight
You can view all the tags in git with simple command
git tag
#38. git verify-tag

git verify-tag command
This git command simply checks the GPG signature of tags.
With -v option , you can print the raw gpg status output to standard error.
git verify-tag <tag>
#39. git verify-commit

git verify-commit command
Like git verify-tag, git verify-commit checks the GPG signature of commits.
But why do you need to check signatures?
To make sure and verify that the source code has not changed since it was 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 so that users can check and verify that the source code has not changed since it's packed.
#40. git worktree

git worktree command
In simple scenarios, you have one worktree for a single git repository.
And git worktree allows you to have multiple working directories associated with a single git repository.
It's useful for many good reasons like running two different branches of your code.
git worktree add
With the above command a new working tree is associated with the repository.
This new working tree is called a "linked working tree" as opposed to the "main working tree" prepared by "git init" or "git clone".
A repository has one main working tree (if it’s not a bare repository) and zero or more linked working trees.
#41. git mv

git mv command
Git mv is a rename command.
git mv command takes two arguments, a source and a destination
git mv <old-file-name> <new-file-name>
Internally it's removing a file and adding a new file with the different name and with the same content.
Git Commands - Git Productivity
In this secion of git commands, I will cover how you can boost your productivity with simple git commands.
Specifically, I'm going to cover available tools to help with day to day git commands and important productivity hacks.
Lets start..
#42. git help

git help command
To get help is an option in the Git. And git help command can be accessed from your Git bash.
To start with git help you can type the command.
git help
If you want to see a list of all git commands, use the following command.
git help -a
And it's easy to get help on one particular command.
git help <command name>
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.
#43. git citool

git citool command
In simple words git citool is a graphical alternative to git-commit.
In this graphical interface you can review all the modified files.
git citool
Also you can use this tool to enter a commit message into the current branch.

#44. git gc

git gc command
You can cleanup unnecessary files with git gc (garbage collection) command.
Also it will optimize your local repository. Git gc is one of the useful git commands.
git gc
For example - if you run git gc command in my sample.project repository , the following message will display.

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

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

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 you click a file in the lower right panel, it focuses the diff in the lower left pane to the relevant section.

#46. git gui

git gui command
git gui is a user interface. It allows you to make changes in the git repository.
With git gui you can add new commits, create new branches, do local merges, and do fetch and push.
git gui
Git Gui focuses on refining individual commits and a single single file annotation. But Git Gui does not show project history.
#47. git prune

git prune command
The git prune command is useful and a powerful git command.
It helps to delete unreachable, lost or "orphaned" Git objects. Any commit that cannot be accessed through a branch or tag is considered unreachable or not present.
In general, git prune is not executed directly.
Prune is known as a garbage collection command and is a child command of the git gc command.
You can see what branches are only exist on your local repo and and no longer tracked with the following git command
git remote prune origin --dry-run
And if you run again without --dry-run to actually prune the local branch.
git remote prune origin
#48. git submodules

git submodules command
Submodules, allow you to reuse code from another repository inside your own repository.
A submodule in a git repository is like a separate directory. Git submodule is a useful feature when you have a project in git which depends on some other projects.
The .gitmodules file provides URLs for submodules. And this file is committed in your repository like any other versioned file.
And to add a new submodule, you can use the following command.
git submodule add <URL>
#49. git mergetool

git mergetool command
You can run merge conflict resolution tools to resolve merge conflicts.
In 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"
#50. git reflog

git reflog command
git reflog command takes many git commands, and different options
git reflog <subcommand> <options>
This command is used for Git to record updates made to the tip of branches.
The reflogs are useful in various Git commands. reflogs to specify the old value of a reference.
Let's take an example -
[email protected]{5} indicates - when the head used to be five moves ago.
#51. git update-index

git update-index command
With this command you can Ignore those files which are already managed by Git locally.
git update-index function has several options which you can check with the --help option.
And here I will show you two different ways you can use git update-index command to ignore files (even if they are already managed by Git).
git update-index --assume-unchanged
and the second way is
git update-index --skip-worktree
Download 51 Git Commands
It is a long post.
So, I've created a downloadable pdf with a one-line summary of these git commands.
You can download the 51 Git Commands from here.

Download Git Commands Cheat Sheet
In this post I have shared all important Git commands. Now It's your turn to share your all time favourite Git commands.
Leave a comment and let me know.