All Articles

Git Commands Cheat Sheet 2026 — Complete Reference with Examples

Published: March 15, 2026 · Updated: May 1, 2026 · 10 min read

Table of Contents

  1. Setup & Configuration
  2. Daily Workflow
  3. Branching & Merging
  4. Remote Repositories
  5. Undoing Mistakes
  6. Stashing
  7. Rebasing
  8. Tags & Releases
  9. History & Diffs
  10. Advanced Tips

Git is the version control system used by virtually every software team. This cheat sheet covers every command you need — from first-time setup to advanced rebasing — with copy-paste ready examples.

1. Setup & Configuration

CommandWhat it does
git config --global user.name "Your Name"Set your display name for commits
git config --global user.email "[email protected]"Set your email for commits
git config --global core.editor "code --wait"Set VS Code as default editor
git config --global init.defaultBranch mainUse main as default branch name
git config --listView all current config values
git initInitialize a new repo in current directory
git clone <url>Clone a remote repo locally
git clone --branch <name> --single-branch <url>Clone only one specific branch (faster)

2. Daily Workflow

Five commands handle 80% of daily Git: git pull → edit → git add .git commit -m "msg"git push.

CommandWhat it does
git statusShow working tree status
git add .Stage all changes
git add -pStage changes interactively (hunk by hunk)
git commit -m "message"Commit staged changes
git commit -am "message"Stage tracked files and commit in one step
git commit --amendModify the last commit
git push -u origin <branch>Push and set upstream tracking
git pullFetch and merge remote changes
git fetchDownload remote changes without merging
Tip: Use git commit --amend --no-edit to add forgotten files to the last commit without changing the message.

3. Branching & Merging

CommandWhat it does
git branchList local branches
git branch -aList all branches including remote
git checkout -b <name>Create and switch to a new branch
git switch -c <name>Modern syntax: create and switch
git branch -d <name>Delete a merged branch (safe)
git branch -D <name>Force-delete a branch
git merge <branch>Merge branch into current branch
git merge --no-ff <branch>Merge with a merge commit
git merge --squash <branch>Squash all commits into one before merging
git cherry-pick <hash>Apply a specific commit to current branch

4. Remote Repositories

CommandWhat it does
git remote -vList remote connections with URLs
git remote add origin <url>Add a remote named "origin"
git remote set-url origin <url>Change remote URL
git push origin --delete <branch>Delete a remote branch
git fetch --pruneRemove stale remote-tracking branches
git push --force-with-leaseSafer force push
Avoid git push --force on shared branches. Use --force-with-lease instead.

5. Undoing Mistakes

CommandWhat it does
git restore <file>Discard unstaged changes to a file
git restore --staged <file>Unstage a file
git reset --soft HEAD~1Undo last commit, keep changes staged
git reset HEAD~1Undo last commit, keep changes unstaged
git reset --hard HEAD~1Undo last commit and discard all changes
git revert <commit>Create a new commit undoing a previous one (safe for shared branches)
git clean -fdRemove all untracked files and directories
git reflogView all HEAD movements — your recovery safety net
Recovery: Accidentally ran git reset --hard? Run git reflog to find the hash, then git reset --hard <hash> to restore it.

6. Stashing

CommandWhat it does
git stashStash current changes
git stash -uStash including untracked files
git stash save "description"Stash with a name
git stash listShow all stashes
git stash popRestore most recent stash and delete it
git stash apply stash@{2}Restore a specific stash (keeps it)
git stash drop stash@{1}Delete a specific stash
git stash clearDelete all stashes

7. Rebasing

Rebase rewrites history for a linear timeline. Use on local branches only.

CommandWhat it does
git rebase mainReplay current branch commits on top of main
git rebase -i HEAD~5Interactive rebase: edit, squash, reorder, drop last 5 commits
git rebase --continueContinue after resolving a conflict
git rebase --abortCancel the rebase

8. Tags & Releases

CommandWhat it does
git tag -a v1.0.0 -m "Release 1.0"Create an annotated tag
git push origin --tagsPush all tags to remote
git tag -d v1.0.0Delete a local tag
git push origin --delete v1.0.0Delete a remote tag

9. History & Diffs

CommandWhat it does
git log --oneline --graph --allVisual branch graph in terminal
git log -p <file>Show all changes to a specific file
git diff --stagedShow staged changes (what will be committed)
git diff main..featureCompare two branches
git blame <file>Show who changed each line and when

10. Advanced Tips

CommandWhat it does
git log -S "function_name"Find commits that added/removed a specific string
git bisect startBinary search to find the commit that introduced a bug
git submodule update --init --recursiveInitialize and update all submodules
git config --global alias.lg "log --oneline --graph --all"Create a shortcut alias

Also explore: Git keyboard shortcuts in your IDE

View Git Shortcuts Reference