Tutorial
The Essential Git Commands Every Developer Must Know
A practical reference of essential Git commands for creating snapshots, browsing history, branching, merging, collaboration, and rewriting history.

Maybe you're a beginner with Git like I was, and you find it hard to memorize all those commands—especially if you don't like memorizing a lot of things. Here is a list of the essential Git commands every developer should know.
In this article you will see:
- Creating snapshots
- Browsing history
- Branching & merging
- Collaboration using Git & GitHub
- Rewriting history
Creating Snapshots#
Initializing a repository#
git initStaging files#
Stages a single file
git add file1.jsStages multiple files
git add file1.js file2.jsStages with a pattern
git add *.jsStages the current directory and all its content
git add .Viewing the status#
Full status
git statusShort status
git status -sCommitting the staged files#
Commits with a one-line message
git commit -m "Message"Opens the default editor to type a long message
git commitSkipping the staging area#
git commit -am "Message"Removing files#
Removes from working directory and staging area
git rm file1.jsRemoves from staging area only
git rm --cached file1.jsRenaming or moving files#
git mv file1.js file1.txtViewing the staged/unstaged changes#
Shows unstaged changes
git diffShows staged changes
git diff --stagedSame as the above
git diff --cachedViewing the history#
Full history
git logSummary
git log --onelineLists the commits from the oldest to the newest
git log --reverseViewing a commit#
Shows the given commit
git show 921a2ffShows the last commit
git show HEADTwo steps before the last commit
git show HEAD~2Shows the version of file.js stored in the last commit
git show HEAD:file.jsUnstaging files (undoing git add)#
Copies the last version of file.js from repo to index
git restore --staged file.jsDiscarding local changes#
Copies file.js from index to working directory
git restore file.jsRestores multiple files in working directory
git restore file1.js file2.jsDiscards all local changes (except untracked files)
git restore .Removes all untracked files
git clean -fdRestoring an earlier version of a file#
git restore --source=HEAD~2 file.jsBrowsing History#
Viewing the history#
Shows the list of modified files
git log --statShows the actual changes (patches)
git log --patchFiltering the history#
Shows the last 3 entries
git log -3git log --author="Mosh"git log --before="2020-08-17"git log --after="one week ago"Commits with "GUI" in their message
git log --grep="GUI"Commits with "GUI" in their patches
git log -S"GUI"Range of commits
git log hash1..hash2Commits that touched file.txt
git log file.txtFormatting the log output#
git log --pretty=format:"%an committed %H"Creating an alias#
git config --global alias.lg "log --oneline"Viewing a commit#
git show HEAD~2Shows the version of file stored in this commit
git show HEAD~2:file1.txtComparing commits#
Shows the changes between two commits
git diff HEAD~2 HEADChanges to file.txt only
git diff HEAD~2 HEAD file.txtChecking out a commit#
Checks out the given commit
git checkout dad47edChecks out the master branch
git checkout masterFinding a bad commit#
git bisect startMarks the current commit as a bad commit
git bisect badMarks the given commit as a good commit
git bisect good ca49180Terminates the bisect session
git bisect resetFinding contributors#
git shortlogViewing the history of a file#
Shows the commits that touched file.txt
git log file.txtShows statistics (the number of changes) for file.txt
git log --stat file.txtShows the patches (changes) applied to file.txt
git log --patch file.txtFinding the author of lines#
Shows the author of each line in file.txt
git blame file.txtTagging#
Tags the last commit as v1.0
git tag v1.0Tags an earlier commit
git tag v1.0 5e7a828Lists all the tags
git tagDeletes the given tag
git tag -d v1.0Branching & Merging#
Managing branches#
Creates a new branch called bugfix
git branch bugfixSwitches to the bugfix branch
git checkout bugfixSame as the above
git switch bugfixCreates and switches
git switch -C bugfixDeletes the bugfix branch
git branch -d bugfixComparing branches#
Lists the commits in the bugfix branch not in master
git log master..bugfixShows the summary of changes
git diff master..bugfixStashing#
Creates a new stash
git stash push -m "New tax rules"Lists all the stashes
git stash listShows the given stash
git stash show stash@{1}Shortcut for stash@
git stash show 1Applies the given stash to the working dir
git stash apply 1Deletes the given stash
git stash drop 1Deletes all the stashes
git stash clearMerging#
Merges the bugfix branch into the current branch
git merge bugfixCreates a merge commit even if fast-forward is possible
git merge --no-ff bugfixSquash merge
git merge --squash bugfixViewing the merged branches#
Shows the merged branches
git branch --mergedShows the unmerged branches
git branch --no-mergedRebasing#
Changes the base of the current branch
git rebase masterCherry picking#
Applies the given commit on the current branch
git cherry-pick dad47edCollaboration#
Cloning a repository#
git clone urlSyncing with remotes#
Fetches master from origin
git fetch origin masterFetches all objects from origin
git fetch originShortcut for "git fetch origin"
git fetchFetch + merge
git pullPushes master to origin
git push origin masterShortcut for "git push origin master"
git pushSharing tags#
Pushes tag v1.0 to origin
git push origin v1.0Deletes remote tag
git push origin --delete v1.0Sharing branches#
Shows remote tracking branches
git branch -rShows local & remote tracking branches
git branch -vvPushes bugfix to origin
git push -u origin bugfixRemoves bugfix from origin
git push -d origin bugfixManaging remotes#
Shows remote repos
git remoteAdds a new remote called upstream
git remote add upstream urlRemoves upstream
git remote rm upstreamRewriting History#
Undoing commits#
Removes the last commit, keeps changes staged
git reset --soft HEAD^Unstages the changes as well
git reset --mixed HEAD^Discards local changes
git reset --hard HEAD^Reverting commits#
Reverts the given commit
git revert 72856eaReverts the last three commits
git revert HEAD~3..git revert --no-commit HEAD~3..Recovering lost commits#
Shows the history of HEAD
git reflogShows the history of bugfix pointer
git reflog show bugfixAmending the last commit#
git commit --amendInteractive rebasing#
git rebase -i HEAD~5This article is based on the Git Cheat Sheet from the Mosh course. You can download the original file as a PDF.
Related posts
Domain Is Already Owned by Another Vercel Account – Solution
How to resolve the 'domain is already owned by another Vercel account' error when adding a custom domain, including the Hashnode subdomain case.
Sep 6, 2021 · 2 min read
How to Set Min and Max Range for Radial Bar (Gauge) in ApexCharts
A workaround to customize min and max values for ApexCharts radial bar (gauge) charts, including formulas for percentage conversion and data label formatting.
Sep 7, 2021 · 3 min read
How to Correctly Deploy a Django 3.2 App on Heroku and Avoid 500 Server Error
Step-by-step guide to deploying a Django 3.2 app on Heroku with gunicorn and WhiteNoise, including static files, Procfile, and avoiding common 500 errors.
May 26, 2021 · 4 min read