Git Commands

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

distributed version control system - Git

#1. git config 

1
git config command

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]

Set up a user name & email with git config

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  

2
git init command

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

3
git clone command

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

# 4
git add command

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]

git add single file

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.

Git Staging Area

#5. git status

# 5
git status command

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

# 6
git commit command

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

# 7
git clean command

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

# 8
git version command

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?

git scm download

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.

check git version with command line

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

# 9
git log command

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.

Git log command

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

Git log command with -p option

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

git log --oneline

Git log command with -oneline option

#10. git shortlog

# 10
git shortlog command

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.

Git shortlog command

#11. git show

# 11
git show command

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

git show head command

#12. git diff

# 12
git diff command

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

# 13
git annotate command

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

# 14
git blame command

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

# 15
git count-objects command

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

# 16
git-sizer command

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.

Git command - git sizer command

#17. git show-branch

# 17
git show-branch command

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

# 18
git verify-commit command

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

# 19
git whatchanged command

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?   

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

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

# 20
git branch command

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

# 21
git rev-parse command

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

# 22
git checkout command

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

# 23
git switch command

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

# 24
git merge command

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.

git merge hotfix branch

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

  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 )

 

#25. git stash

# 25
git stash command

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 stash@{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

# 26
git push command

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

# 27
git pull command

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

# 28
git fetch command

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

# 29
git remote command

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

view remote url with -v option

#30. git cherry-pick

# 30
git cherry-pick command

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

# 31
git bisect command

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

# 32
git rebase command

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

# 33
git reset command

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

# 34
git restore command

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

# 35
git revert command

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

  1. git-revert command will make a new commit that reverts the changes made by other commits.

Git restore command

  1. Git restore command will restore files in the working tree
  2. This command will not update your branch.
  3. With the help of git restore command you can also restore files in the index from another commit

Git-reset command

  1. Git reset command is about updating your branch
  2. This command can change the commit history.
  3. Git reset can also be used to restore the index, overlapping with git restore.

#36. git rm