Git Cheat Sheet – 40 Git Commands You Should Know

Here are the list of most used 40 Git command that a developer mostly used in a day-to-day coding. is a distributed version control system that helps developers collaborate on projects of any scale.

Table of Contents

Introduction

Hello Folks!

Git is a distributed version control system that helps developers collaborate on projects of any scale. As a developer you should know following commands which might speedup your day-to-day coding activity.

1. git config

#CommandDescription
1git config --global user.name <name>Define the author name to be used for all commits by the current user.
2git config --global user.email <email>Define the author email to be used for all commits by the current user.
3git config --global alias.<alias-name> <git-command>Create shortcut for a Git command. E.g. alias.glog log --graph --oneline will set git glog equivalent to git log --graph --oneline.
4git config --system core.editor <editor>Set text editor used by commands for all users on the machine. <editor> arg should be the command that launches the desired editor (e.g., vi).
5git config --global --editOpen the global configuration file in a text editor for manual editing.

2. git branch

#CommandDescription
1git branchExisting branches are listed. Current branch will be highlighted with an asterisk.
2git branch <branch>Create a new branch from current branch.
3git branch -a --mergedList outdated branches that have been merged into the current one.
4git branch -d <branch>Deletes the branch only if the changes have been pushed and merged with remote.
5git branch -D <branch>Force delete a local branch.
6git branch | grep -v "sandbox" | grep -v "main" | xargs git branch -DDelete all branch from local except main and sandbox branch.

3. git diff

#CommandDescription
1git diff HEADShow difference between working directory and last commit.
2git diff --cachedShow difference between staged changes and last commit.

4. git log

#CommandDescription
1git log -<limit>Limit number of commits by <limit>. E.g. git log -5 will limit to 5 commits.
2git log --onelineCondense each commit to a single line.
3git log -pDisplay the full diff of each commit.
4git log --statInclude which files were altered and the relative number of lines that were added or deleted from each of them.
5git log --author= "<pattern>"Search for commits by a particular author.
6git log --grep="<pattern>"Search for commits with a commit message that matches «pattern>.
7git log <since> .. <until>Show commits that occur between <since> and <until>. Args can be a commit ID, branch name, HEAD, or any other kind of revision reference.
8git log -- <file>Only display commits that have the specified file.
9git log --graph --decorate--graph flag draws a text based graph of commits on left side of commit msgs. --decorate adds names of branches or tags of commits shown.

5. git rebase

#CommandDescription
1git rebase -i <base>Interactively rebase current branch onto <base>. Launches editor to enter commands for how each commit will be transferred to the new base.

6. git pull

#CommandDescription
1git pull <remote>It is used to fetch and download content from a remote repository and immediately update the local repository to match that content.
2git pull --rebase <remote>Fetch the remote's copy of current branch and rebases it into the local copy. Uses git rebase instead of merge to integrate the branches.

7. git push

#CommandDescription
1git push <remote> --forceForces the git push even if it results in a non-fast-forward merge. Do not use the -force flag unless you're absolutely sure you know what you're doing.
2git push <remote> --allPush all of your local branches to the specified remote.
3git push <remote> --tagsTags aren't automatically pushed when you push a branch or use the -all flag. The -tags flag sends all of your local tags to the remote repo.

8. git remote

#CommandDescription
1git remote -vTo see all remote repositories for your local repository.
2git remote add origin <URL>Add a new remote URL
3git remote set-url origin <URL>To changes an existing remote repository URL.

9. git merge

#CommandDescription
1git merge —squash <branch | commit>Performs a squash merge
2git merge <branch | commit>Merge a specific branch or commit into current branch
3git merge --abort Aborts the merge

10. git reset

#CommandDescription
1git resetReset staging area to match most recent commit, but leave the working directory unchanged.
2git reset <commit>Move the current branch tip backward to <commit>, reset the staging area to match, but leave the working directory alone.
3git reset --hardReset staging area and working directory to match most recent commit and overwrites all changes in the working directory.
4git reset --hard <commit>Same as previous, but resets both the staging area & working directory to match. Deletes uncommitted changes, and all commits after <commit>.

11. Miscellaneous

#CommandDescription
1git shortlog -sn --emailGet commit count by user's email
2git fetch --prune --allTo fetch all the branch from remote and also remove any remote-tracking references that no longer exist on the remote

Let me know in the comments out of the 40 commands which are the one that you are using the most, and you can also suggest me some handy command to add to the above list.

Along with the above commands I have also customise my git configuration with aliases. If you want to know then checkout my .gitconfig file in dotfiles GitHub repository.

Raunak Gupta

Raunak Gupta

I'm Raunak Gupta, a seasoned software developer with over 9 years of experience in a wide range of programming languages, frameworks, and tools. I started my journey as a WordPress & CakePHP developer in 2014, diving deep into the world of OOPs, Request handling, and SEO. Along the way, I crafted numerous dazzling WooCommerce stores, tamed payment gateways, optimized for full filament functionality, and achieved ultra-low latency for lightning-fast load times. My expertise extends to BI tools, website builders, DevOps, and team leadership. I like to help upcoming developers, so I share my experience through this blog and by assisting fellow developers on Stack Overflow, where I've earned a stellar reputation with over 10k+ points of recognition.

Articles: 29

2 Comments

  1. Cheers, mate! I’d also recommend giving “git reset –hard origin/master” a go. It’s saved me loads of times, you know?

Leave a Reply

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