Git Command Cheat Sheet
Scenario-based Git command cheat sheet — undo, revert, branching, rebase, stash, submodules — searchable and copy-ready.
| Scene | COMMAND | Notes | |
|---|---|---|---|
| Config | git config --global user.name "Name" | Set the global committer name | |
| Config | git config --global user.email "you@example.com" | Set the global committer email | |
| Config | git config --global init.defaultBranch main | Default the initial branch to main | |
| Config | git config --global core.autocrlf input | Commit LF, leave checkout as-is (line endings) | |
| Config | git config --global alias.st "status -sb" | Create an alias (git st) | |
| Config | git config --list --show-origin | List all config with its source file | |
| Start | git init | Initialize a repo in the current directory | |
| Start | git clone <url> --depth 1 | Shallow clone (latest commit only, fast for big repos) | |
| Start | git clone -b <branch> <url> | Clone and check out a branch directly | |
| Start | git status -sb | Short status (branch + changes) | |
| Start | git switch -c <new-branch> | Create and switch to a new branch (modern) | |
| Daily | git add -p | Stage hunk by hunk (split your commits) | |
| Daily | git commit --amend --no-edit | Fold changes into the last commit (before pushing) | |
| Daily | git commit -m "feat: message" | Commit with a conventional message | |
| Daily | git restore <file> | Discard working-tree changes (destructive) | |
| Daily | git restore --staged <file> | Unstage a file (keeps changes) | |
| Daily | git diff --staged | Diff of staged-but-uncommitted changes | |
| Daily | git clean -fd | Delete untracked files and dirs (destructive) | |
| Daily | git rm --cached <file> | Untrack but keep locally (e.g. committed .env) | |
| Branch | git branch -a | List all local and remote branches | |
| Branch | git branch -m old new | Rename a branch | |
| Branch | git branch -d <branch> | Delete a merged branch (-D to force) | |
| Branch | git switch <branch> | Switch branches (modern) | |
| Branch | git worktree add ../hotfix hotfix | Worktree: check out another branch in parallel | |
| Branch | git cherry-pick <sha> | Pick a single commit onto the current branch | |
| Merge / Rebase | git merge --no-ff <branch> | Merge with an explicit merge commit | |
| Merge / Rebase | git merge --abort | Abort the merge and restore the pre-merge state | |
| Merge / Rebase | git rebase main | Rebase the current branch onto main | |
| Merge / Rebase | git rebase --continue | Continue a rebase after resolving conflicts | |
| Merge / Rebase | git rebase -i HEAD~3 | Interactive rebase over the last 3 commits | |
| Merge / Rebase | git rebase --onto main <sha> | Transplant a commit range onto another base | |
| Undo / Revert | git commit --amend | Fix the last commit (message or content) | |
| Undo / Revert | git reset --soft HEAD~1 | Undo the commit, keep changes staged | |
| Undo / Revert | git reset --hard HEAD~1 | Undo the commit and discard changes (destructive) | |
| Undo / Revert | git revert <sha> | Safe rollback via an inverse commit (use on shared branches) | |
| Undo / Revert | git reflog | Show HEAD history (recover "lost" commits) | |
| Undo / Revert | git reset --hard <sha> | Hard-move the branch to a commit (destructive) | |
| Undo / Revert | git checkout <sha> -- <file> | Restore one file from a past commit | |
| Remote | git remote -v | List remote URLs | |
| Remote | git remote set-url origin <url> | Change the remote URL | |
| Remote | git fetch --prune | Fetch and prune stale remote branches | |
| Remote | git push -u origin <branch> | First push with upstream tracking | |
| Remote | git push --force-with-lease | Safer force push (rejects if remote moved) | |
| Remote | git push origin --delete <branch> | Delete a remote branch | |
| Remote | git pull --rebase | Pull with rebase (no merge bubbles) | |
| Stash | git stash push -m "wip: msg" | Stash changes with a message | |
| Stash | git stash list | List stashes | |
| Stash | git stash pop | Apply the latest stash and drop it | |
| Stash | git stash apply stash@{2} | Apply a specific stash, keep the entry | |
| Stash | git stash -u | Stash including untracked files | |
| Tag | git tag -a v1.2.0 -m "release" | Create an annotated tag (releases) | |
| Tag | git push origin v1.2.0 | Push one tag (push skips tags by default) | |
| Tag | git tag -d v1.2.0 | Delete a local tag | |
| Tag | git tag --sort=-creatordate | List tags newest first | |
| Submodule | git submodule add <url> <path> | Add a submodule | |
| Submodule | git submodule update --init --recursive | Init all submodules after cloning | |
| Submodule | git submodule update --remote | Update submodules to their upstream latest | |
| Submodule | git clone --recurse-submodules <url> | Clone with submodules included | |
| History | git log --oneline --graph --all -20 | Graph overview of recent commits | |
| History | git log -p <file> | Per-commit patch for one file | |
| History | git log --author="Name" --since="2 weeks ago" | Filter history by author and time | |
| History | git blame -L 10,20 <file> | Line-level blame for lines 10–20 | |
| History | git show <sha> | Show one commit in full | |
| History | git shortlog -sn | Rank contributors by commit count | |
| Troubleshoot | git fsck --lost-found | Check dangling objects (with reflog to recover) | |
| Troubleshoot | git diff main...feature | Three-dot diff: what feature adds since the fork point | |
| Troubleshoot | git bisect start | Bisect to find the commit that broke it | |
| Troubleshoot | git update-index --assume-unchanged <file> | Temporarily ignore local changes | |
| Troubleshoot | git ls-files | xargs rm && git checkout . | Hard-restore the working tree to HEAD (last resort) |
>_ USAGE GUIDE
You use Git daily, but the low-frequency moves — "undo yesterday's commit", "transplant a range of commits onto another base", "recover a vanished commit" — send you searching every time. This cheat sheet is organized by scenario: config, start, daily commits, branching, merge/rebase, undo/revert, remote, stash, tag, submodule, history and troubleshooting, with keyword + scene filtering and one-click copy on every command.
- Browse by scene: pick a scene tab to focus, such as Undo / Revert when fixing history
- Search by keyword:
reset,undo,forceland straight on the relevant commands - Copy and run: click the copy button per row and adjust the arguments in your terminal
- Respect the danger notes: commands marked destructive (reset --hard, clean -fd …) deserve a second look before Enter
>_ FAQ
Q1 · git reset or git revert?
revert creates an inverse commit that cancels a target commit — history only grows, and it is the right tool on shared branches. reset moves the branch pointer itself: fine for cleaning up unpushed local work, but reset plus force-push on a public branch scrambles everyone's history. Rule of thumb: reset rewrites your own history, revert fixes public history.
Q2 · How do I fix a wrong commit message?
Unpushed: git commit --amend rewrites the last commit (add --no-edit to keep the message). Pushed: amend then force-push — on shared branches prefer a documented revert plus a new commit instead. For older commits use interactive rebase (git rebase -i) and mark the line as reword.
Q3 · git push --force vs --force-with-lease?
--force overwrites the remote unconditionally. --force-with-lease first verifies the remote still sits where you last saw it and refuses otherwise — if someone pushed after you, the push fails. Use --force-with-lease essentially always; it only overwrites when nobody else has added commits.
Q4 · A commit disappeared — how do I get it back?
The branch pointer moved but the commit object still exists: git reflog lists HEAD's movements, find the sha, then git reset --hard <sha> or point a new branch at it. Reflog entries live about 90 days by default, and git fsck --lost-found recovers most accident scenes.
Q5 · A secret got committed — what now?
First git rm --cached <file> to untrack, and add it to .gitignore. The history still contains it: for private repos consider rewriting with git filter-repo followed by a force-push; for public repos or anything possibly leaked, rotate the secret immediately — cleaning history does not undo a leak.