All Articles
Git Commands Cheat Sheet 2026 — Complete Reference with Examples
Published: March 15, 2026 · Updated: May 1, 2026 · 10 min read
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
| Command | What 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 main | Use main as default branch name |
| git config --list | View all current config values |
| git init | Initialize 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.
| Command | What it does |
| git status | Show working tree status |
| git add . | Stage all changes |
| git add -p | Stage 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 --amend | Modify the last commit |
| git push -u origin <branch> | Push and set upstream tracking |
| git pull | Fetch and merge remote changes |
| git fetch | Download 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
| Command | What it does |
| git branch | List local branches |
| git branch -a | List 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
| Command | What it does |
| git remote -v | List 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 --prune | Remove stale remote-tracking branches |
| git push --force-with-lease | Safer force push |
Avoid git push --force on shared branches. Use --force-with-lease instead.
5. Undoing Mistakes
| Command | What it does |
| git restore <file> | Discard unstaged changes to a file |
| git restore --staged <file> | Unstage a file |
| git reset --soft HEAD~1 | Undo last commit, keep changes staged |
| git reset HEAD~1 | Undo last commit, keep changes unstaged |
| git reset --hard HEAD~1 | Undo last commit and discard all changes |
| git revert <commit> | Create a new commit undoing a previous one (safe for shared branches) |
| git clean -fd | Remove all untracked files and directories |
| git reflog | View 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
| Command | What it does |
| git stash | Stash current changes |
| git stash -u | Stash including untracked files |
| git stash save "description" | Stash with a name |
| git stash list | Show all stashes |
| git stash pop | Restore 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 clear | Delete all stashes |
7. Rebasing
Rebase rewrites history for a linear timeline. Use on local branches only.
| Command | What it does |
| git rebase main | Replay current branch commits on top of main |
| git rebase -i HEAD~5 | Interactive rebase: edit, squash, reorder, drop last 5 commits |
| git rebase --continue | Continue after resolving a conflict |
| git rebase --abort | Cancel the rebase |
| Command | What it does |
| git tag -a v1.0.0 -m "Release 1.0" | Create an annotated tag |
| git push origin --tags | Push all tags to remote |
| git tag -d v1.0.0 | Delete a local tag |
| git push origin --delete v1.0.0 | Delete a remote tag |
9. History & Diffs
| Command | What it does |
| git log --oneline --graph --all | Visual branch graph in terminal |
| git log -p <file> | Show all changes to a specific file |
| git diff --staged | Show staged changes (what will be committed) |
| git diff main..feature | Compare two branches |
| git blame <file> | Show who changed each line and when |
10. Advanced Tips
| Command | What it does |
| git log -S "function_name" | Find commits that added/removed a specific string |
| git bisect start | Binary search to find the commit that introduced a bug |
| git submodule update --init --recursive | Initialize and update all submodules |
| git config --global alias.lg "log --oneline --graph --all" | Create a shortcut alias |