Git Command Cheatsheet
Quick reference for the most commonly used Git commands. This cheatsheet provides fast access to essential Git operations.
Getting Started
Clone a Repository
# Clone via HTTPS
git clone https://github.com/user/repo.git
# Clone via SSH
git clone git@github.com:user/repo.git
# Shallow clone (faster)
git clone --depth 1 <url>
# Clone specific branch
git clone -b branch-name <url>Checking Status of Your Files
View File Status
git statusView Short Status
git status --shortView in Machine Readable Format
git status --porcelainThe --porcelain option causes git status to display the status in a machine-readable format, which is intended to be easy to parse by scripts and other automated tools.
Viewing the Commit History
View Git Logs
git logView Logs in Single Line
git log --onelineView N Numbers of Logs
git log -nSingle Line & N Number of Logs
git log --oneline -nLog with Patch Changes
git log --patchShowing Stats
git log --statShows how many files were changed, and how many lines in those files were added and removed. It also puts a summary of the information at the end.
Short Stats
git log --shortstatGraphical View
git log --graphLogs Without Merge Commits
git log --no-mergesShowing References with Dates
git log --pretty=referenceView Git History of Specific Line
git log --pretty=short -u -L <start line>,<end line>:<file path>Search for Code Within Git History
git log -S "CodeSnippet or searchString"Search for Commit Message Within Git History
git log --grep=<pattern>Commit
Add Commit Message
git commit -m "your message"Add Headline and Details
git commit -m "Headline" -m "details"Re-use Commit Message from Another Commit
git commit --reuse-message=<commit-sha>
# or
git commit -C <commit-sha>Re-use Commit Message and Enable Editing
git commit --reedit-message=<commit-sha>
# or
git commit -c <commit-sha>Add Something on Last Commit
git commit --amendAdd Something on Last Commit Without Updating Message
git commit --amend --no-editStaging Files (Git Add)
Add Specific Files
git add file.txt
git add file1.txt file2.txtAdd All Changes
git add .
git add --all
git add -AInteractive Staging (Patch Mode)
# Stage changes interactively
git add -p
# Stage specific hunks
git add --patch file.txtAdd Modified Files Only
# Don't add new files
git add -uViewing Differences (Git Diff)
View Unstaged Changes
git diffView Staged Changes
git diff --staged
# or
git diff --cachedCompare Branches
git diff branch1..branch2
git diff main...featureCompare Commits
git diff commit1 commit2
git diff HEAD~1Diff Specific File
git diff file.txt
git diff --staged file.txtShow Only Changed Files
git diff --name-only
git diff --name-statusMerging Branches
Merge Branch into Current
git merge branch-nameMerge with No Fast-Forward
git merge --no-ff branch-nameSquash Merge
git merge --squash branch-name
git commit -m "Squashed changes"Abort Merge
git merge --abortRemote Repositories
View Remotes
git remote
git remote -vAdd Remote
git remote add origin https://github.com/user/repo.git
git remote add upstream https://github.com/original/repo.gitRemove Remote
git remote remove remote-nameChange Remote URL
git remote set-url origin new-urlFetching & Pulling
Fetch from Remote
git fetch
git fetch origin
git fetch --allPull Changes
git pull
git pull origin main
git pull --rebasePull with Fast-Forward Only
git pull --ff-onlyPushing Changes
Push to Remote
git push
git push origin mainPush and Set Upstream
git push -u origin branch-namePush All Branches
git push --allPush Tags
git push --tags
git push origin v1.0.0Force Push (Use with Caution!)
git push --force-with-leaseDelete Remote Branch
git push origin --delete branch-nameRestoring Files (Git Restore)
Discard Changes in File
git restore file.txt
git restore .Unstage File
git restore --staged file.txtRestore from Specific Commit
git restore --source=HEAD~1 file.txt
git restore --source=main file.txtInteractive Restore
git restore -p file.txtGit Branching
Show Current Branch
git branch --show-currentList of All Local Branches
git branch
# or
git branch --listList All Remote Branches
git branch --remotesList Branch with Last Commit
git branch --verbose
# or
git branch -vSee Merged Branches
git branch --mergedCreate a New Branch
git branch -b "branch name"Rename a Branch
git branch -m "the renamed branch"Delete a Branch
git branch --delete "branch name"Force Delete a Branch
git branch --delete --force "branch name"
# or
git branch -D "branch name"Delete Remote Branch
git push origin --delete "branch name"Checkout to a Specific Branch
git checkout "branch name"Switch Between Recent Two Branches
git checkout -Git Switch (Git 2.23 and onwards)
Create a Branch
git switch --create "branch name"Checkout to a Specific Branch
git switch "branch name"Switch Between Recent Two Branches
git switch -Git Tags
Git supports two types of tags: 1. lightweight tag, 2. annotated tag
Add a Lightweight Tag
git tag "tag name"Add an Annotated Tag
git tag --annotate "tag name"
# or
git tag -a "tag name"Add Tag to a Specific Commit
git tag --annotate "tag name" <commit-sha>Push Tags to Origin
git push origin 'tag name'Push All Tags at Once
git push origin --tagsList All Tags
git tag --list
# or
git tagList Remote Tags
git ls-remote --tags originGit Stash (Temporary Storage)
Stash Changes
git stashAdd Custom Message When Stashing
git stash push -m "your message"Include Untracked (New) Files
git stash --include-untracked
# or
git stash -uInclude Untracked + Ignored Files
git stash --allView Stash List
git stash listApply Last Stash Changes
git stash popApply Nth Stash Changes
git stash pop <stash-head>Apply Stash Without Deleting It
git stash apply <stash-head>Delete a Stash
git stash drop <stash-head>❗ Clear All Stashes
git stash clear⚠️ Make sure you know what you are doing!
See Stash Changes
git stash show <stash-head>View Patch Changes
git stash show --patch <stash-head>Create Branch from Stash
git stash branch "branch-name" <stash-head>Useful when reapplying stash might create conflict and you want to test your stash changes or make a new branch.
Reset Changes
Reset Last Commit
git reset HEAD~1Reset Last N Number of Commits
git reset HEAD~nReset Modes
--soft: does not reset the index or working tree files--hard: reset the index + working tree files--mixed: reset the index but not working tree files
Reset HEAD to Origin/Remote Branch
git reset origin/<branch-name>Reset Last Merge Commit
git reset --merge HEAD~1Abort Merge Commit (Not Committed Yet)
git reset --merge
# or
git merge --abortRevert Changes
Revert a Commit
git revert <commit-sha>Edit Message
git revert --edit <commit-sha>Don't Edit Message
git revert --no-edit <commit-sha>Stage Changes Instead of Auto Commit
git revert --no-commit <commit-sha>Revert Range of Commits
git revert first-bad-commit^..last-bad-commitPatch
Make Patch File
git diff > fileName.patchMake Patch File from Stage
git diff --staged > fileName.patch
# or
git diff --cached > fileName.patchApply Patch Changes
git apply fileName.patchPatch from N Top Commits
git format-patch -n <sha>Apply Patch Changes (format-patch)
git am < file.patchRebase Git Commit History
Rebase Your Branch Head
git rebase <parent-branch>Interactive Rebase
git rebase --interactive <commit-sha>^The ^ (caret) means parent to that commit
Continue Rebasing
git rebase --continueAbort Rebase
git rebase --abortAdd Fixup Commit
git commit --fixup=<commit-sha>Auto Squash Commit
git rebase --interactive --autosquash <commit-sha>Debugging with Git (Bisect)
Start Debugging
git bisect start
git bisect good sha-of-good-commit
git bisect bad sha-of-bad-commitIf bad commit is not provided, default is the last commit. Then Test your application and provide info about which state is good and which state is bad.
Specify Good State
git bisect goodSpecify Bad State
git bisect badTerminate Bisect
git bisect resetReflog (Insurance in Git)
View Reference Logs
git reflogReference logs expiry time: default is 90 days
Set Expiry Time
git config gc.reflogExpire <time>
# set for 90 days
git config gc.reflogExpire 90.days
# set never
git config gc.reflogExpire neverView Expiry Time
git config --get gc.reflogExpireUnset Expiry Time
git config --unset gc.reflogExpireTree View of Git Reflog (Works with gitk)
gitk --all `git reflog | cut -c1-7`Recover Deleted Commit
Delete or reset your last commit (mostly used: git reset --hard HEAD~1)
To get back to previous state:
git reset --hard HEAD@{1}If you made any commit after deleting or want to get back an old deleted commit:
- List the reflog history:
git reflogFind your commit from the reflog history
Add commit using git cherry-pick command:
git cherry-pick <commit-sha | @HEAD{number}>You can also copy commit message from the reflog:
git commit --reuse-message=<commit-sha | @HEAD{number}>
# or
git commit -C <commit-sha | @HEAD{number}>Copy commit message from the reflog and enable editing:
git commit --reedit-message=<commit-sha | @HEAD{number}>
# or
git commit -c <commit-sha | @HEAD{number}>Recover Deleted Branch
If you delete your old branch or accidentally delete any branch:
- List the reflog history:
git reflogFind your last commit on your deleted branch
Checkout to your branch:
git checkout -b "branch-name" <commit-sha | @HEAD{number}>❌ Don't Use These Commands
⚠️ Ensure that you are doing what you are supposed to do.
Clean Old or Unapproachable Reflog Entries
git reflog expireDelete Reflog Entries
git reflog delete💣 This command causes data loss (use this at your own risk!)