Git Cheat Sheet is a table you can use directly: 149 commands grouped under eleven scenarios — Setup and configuration, Everyday commits, Branching, Merging and rebasing, Undo and reset, Remote collaboration, Stash, Tags, Submodules and worktrees, History and troubleshooting, and Ignore and attributes. Each entry carries a one-line explanation, an example you can paste straight into a terminal and a two-level Caution / Danger risk label, so commands that discard uncommitted changes or rewrite shared history stand out at a glance.
Git Cheat Sheet is a table you can use directly: 149 commands grouped under eleven scenarios — Setup and configuration, Everyday commits, Branching, Merging and rebasing, Undo and reset, Remote collaboration, Stash, Tags, Submodules and worktrees, History and troubleshooting, and Ignore and attributes. Each entry carries a one-line explanation, an example you can paste straight into a terminal and a two-level Caution / Danger risk label, so commands that discard uncommitted changes or rewrite shared history stand out at a glance.
It is made for three moments: you remember what you want to do but not how the command is spelled (search “undo commit”, “delete remote branch”); you know the command but are not sure whether it will lose anything (read the risk level and the notes); and you are new to Git and want to understand what add, commit and push actually move around (start with the four-areas diagram at the top and the operation diagrams below).
At the bottom of the page there is an in-memory Git terminal simulator: it supports 30+ common subcommands and the commit graph on the right updates live with every command, with 10 practice levels from “first commit” to “resolve a merge conflict”. It is not real Git and never touches your computer or the network, but the semantics follow the official manual, so what you practice transfers straight to a real machine.
How to use
01Search— type a command name or what you want to do (such as “undo”, “force push”, “delete branch”) into the box at the top; multiple words are separated by spaces and all of them must match. Results keep the original grouping.
02Browse by scenario— click a group chip to show one category only, or use the group index under “the four areas” to jump to a section; tick “risky commands only” for a quick review before you run something.
03Copy— click the blue command text or the copy icon on the right to copy the command; the second icon copies the example, and under “Common recipes” you can copy a whole recipe at once.
04Favorites— click the star to pin a frequently used command to “My favorites” at the top of the page; favorites are stored in this browser (localStorage) and do not sync across devices.
05Practice— scroll to the practice terminal, pick a level and type commands towards its goal; each command is checked automatically, and when you are stuck use Hint or Show answer, or Reset to return to the level's initial state.
Input and output examples
Typing “undo commit” into the search box returns these entries from the Undo and reset group (excerpt):
Completing level 4, “undo the latest commit but keep the changes”, in the simulated terminal:
text
git reset --soft HEAD~1 undo the latest commit, keep the changes staged Caution
git reset HEAD~1 undo the latest commit, changes return to the working tree unstaged
git reset --hard <commit> branch pointer, index and working tree all go back to commit Danger
git revert <commit> create a new commit that cancels the change; history is untouched and it is safe to push
bash
user@toolun-lab:~/project (main)$ git log --oneline
7f3a1c2 (HEAD -> main) wip: messy commit
2b9e0d4 init: add README
user@toolun-lab:~/project (main)$ git reset --soft HEAD~1
HEAD is now at 2b9e0d4 init: add README
--soft: moves only the branch pointer; the index and working tree are kept (the cancelled commit's content stays staged)
Tip: the discarded commit 7f3a1c2 can still be recovered with git reflog
user@toolun-lab:~/project (main)$ git status -s
A app.js
A secret.env
✔ Passed: undo the latest commit but keep the changes.
How it works
The four areas: most commands just move data
Almost every Git command can be read as “move content from one area to another”. The working tree is the files you can see and edit; the index (staging area) is a list of what the next commit will look like; the local repository (.git) holds all the commits; the remote repository is the same set of things on another machine.
add stages, commit stores, push publishes; fetch and restore run the other way
With this picture in mind, many “how do I undo this” questions have a single answer: changed something that was never added — bring it back with git restore <file>; added something you do not want to commit — git restore --staged; committed and want it gone — git reset; already pushed — only git revert, which appends an inverse commit, is safe.
Commits are snapshots, branches are just pointers
A commit object records a snapshot of the entire directory tree (not a diff), the ids of its parent commits, the author and the timestamps, and the message. All of that is hashed with SHA-1 (new repositories may use SHA-256) into the commit id, so any change — even one character in the message — produces a completely new id. That is why --amend, rebase and cherry-pick all “rewrite history”: they create new commits, and the old ones are simply no longer referenced.
A branch is a 40-character file under .git/refs/heads/<name> whose content is a commit id. HEAD usually points to a branch name. Creating a branch is therefore instant (writing a small file), switching branches repoints HEAD to another name and updates the working tree to that commit's snapshot, and deleting a branch just deletes a pointer — the commits themselves do not vanish immediately.
Choosing between merge and rebase
The same starting point, the two histories produced by merge and rebase
merge faithfully records what happened: the two lines meet at a merge commit, and anyone can see that the feature was developed in parallel. rebase makes the history look as though it had always been linear — git log is easier to read and git bisect is easier to use — at the cost of changing every commit id. Rule of thumb: rebase freely on your own branches that have not been pushed; merge anything others may be building on. git pull --rebase is that rule applied daily — it moves your few unpushed commits onto the remote's latest state and avoids a pile of “Merge branch 'main' of …” commits.
The three modes of reset
Which area each mode drags back to the target commit
--soft suits “splitting or squashing commits”: the pointer goes back and the content stays staged. --mixed (the default) suits “re-choosing what to commit”. --hard is the real discard: changes in the working tree that were never committed cannot be recovered by any means, while the commits you dropped still sit in the object database — git reflog shows every move of HEAD, and git reset --hard HEAD@{1} takes you back to before the reset. By default the reflog keeps 90 days of entries (30 days for unreachable ones) — Git's window for regret.
How the risk levels are decided
“Danger” goes to two kinds of command: those that discard uncommitted working-tree content (restore, reset --hard, clean, stash drop, checkout -- <file>), and those that rewrite history others may already have (push --force, branch -D on an unmerged branch). “Caution” goes to commands that change state in a recoverable way or whose blast radius you should confirm first (commit --amend, rebase, merge --squash, push --delete). Read-only commands (log, diff, status, show, blame) carry no label.
Scope and limitations
Command sources
— based on the official manuals (git-scm.com/docs, Git 2.4x); git switch / git restore need version 2.23 or newer, and the older checkout spellings are listed alongside.
Selection criteria
— commands and options used in daily development, collaboration and troubleshooting; every entry can be traced to a manual page. Plumbing commands (hash-object, cat-file, update-ref and so on) and platform-specific operations (GitHub PR or GitLab MR web workflows) are not included.
Simulated terminal
— implements the core semantics of common subcommands (fast-forward / three-way merges, per-file conflict detection, the three reset modes, linear rebase, stash, remote push and pull); interactive rebase, bisect, submodules, hooks and the full option set of configuration files are not supported, and a rebase that hits a conflict aborts automatically instead of stopping for you to resolve it.
What it does not do
— it does not connect to a real repository, does not read the .git on your computer and provides no graphical conflict resolver.
Favorites and recents
— stored in this browser only; clearing site data removes them.
Typical use cases
Committed too early, wrote the wrong message, committed to the wrong branch
Search “undo” or open the Undo and reset group, first separating “not yet pushed” (reset / --amend) from “already pushed” (revert), then pick a command. Under “Common recipes” there are complete plays such as “committed on main, want to move it to a new branch”.
Tidy up small commits before merging
git rebase -i HEAD~N with squash / fixup / reword; the rebase card in the diagram shows how the history goes from a fork to a straight line and why the old commits turn grey.
First collaboration: remotes, tracking, rejected pushes
The Remote collaboration group runs from remote add to push -u and --force-with-lease; practice levels 8 (push) and 9 (undo a pushed commit) cover the two most common “cannot push” moments.
Recovering things that “disappeared”
git reflog, git fsck --lost-found and git stash list sit next to each other under Undo and reset; the “recover a deleted branch” recipe gets it done in two steps.
FAQ
git reset or git revert — which one do I pick?
Look at whether the commit has been pushed. Not pushed: reset drags the pointer straight back and the history stays clean. Already pushed: revert appends an inverse commit without rewriting what others already have; otherwise the next person to pull hits a fork.
Why do “Merge branch 'main' of …” commits appear after git pull?
Because you have unpushed commits and the remote has new ones, and a default pull merges the two sides. To avoid it, use git pull --rebase, or set git config --global pull.rebase true once.
What is the difference between --force and --force-with-lease?
--force overwrites the remote branch unconditionally. --force-with-lease first checks that the remote branch is still the commit you saw when you last fetched, and refuses if someone has pushed since — keeping “I overwrote a colleague's commits” from happening. Always use the latter for a push after rewriting history.
Why do the commit ids in the simulator always look the same?
Real Git hashes content, author and timestamps together; the simulator substitutes a logical clock for real time and uses deterministic hashing, so the same sequence of operations produces the same ids, which makes checking against the reference answer easier. Its ids deliberately differ from what real Git would compute.
Do the commands work on Windows?
The commands themselves are cross-platform. The shell is what differs: in PowerShell, && needs version 7.x, quoting paths is safer, and line endings are best set via the core.autocrlf entry under Setup and configuration.
Privacy
Everything on this page (the command table, the diagrams, the simulated terminal) is static content downloaded with the page and runs locally in your browser; searching, copying, adding favorites and practicing send no requests to any server. Favorites are stored in this browser's localStorage and cannot be read by the site. The external links point to the official Git documentation and are handled by those sites once clicked.
References & further reading
Git Reference Manual(访问日期:2026-09-09)— the authoritative manual page for every command; every entry on this page was checked against it.
Pro Git, 2nd Edition (Chinese)(访问日期:2026-09-09)— Chapter 3, “Branching”, and section 7.7, “Reset Demystified”, are the best introduction to pointers and the three areas.
git-reset(1) manual page(访问日期:2026-09-09)— the official table of how the three modes affect HEAD, the index and the working tree.
git-rebase(1) manual page(访问日期:2026-09-09)— the “Recovering from upstream rebase” section explains why shared branches should not be rebased.
149 Git commands grouped by scenario, with explanations, examples, risk levels, search, copy, favorites, diagrams, and a practice terminal
Total 149 commands
Data flows between the four areas: add to the staging area, commit to the local repository, push to the remote; the other direction uses fetch / restore / switch. pull = fetch + merge.
Diagram: what each command does to the history graph
Circles are snapshots and arrows point to the parent, green labels are branches, purple is the branch HEAD is on; grey circles are no longer referenced by any branch (recoverable with reflog), and filled circles are the ones this action creates.
Commit: append a node at the tip of the current branch
git add . && git commit -m "…"
Each commit records its own parent. A branch is just a movable pointer to a commit, so committing advances main and HEAD together.
Branch: add a pointer, copy no files
git switch -c feature
The new branch and main point at the same commit, so creating one is instant. Commits made on feature afterwards advance only feature.
Fast-forward merge: move the pointer when main has no commits of its own
git switch main && git merge feature
No new commit is created and the history stays a straight line. To keep a record that a branch once existed, merge with --no-ff instead.
Three-way merge: create a merge commit when both sides have new commits
git merge feature
Git finds the common ancestor b2, combines the changes each side made relative to b2, and records m6 with both of them as its parents. You only get a conflict where the same area changed differently.
Rebase: replay the commits from feature on top of main
git switch feature && git rebase main
The history becomes a straight line, but c3 and d4 are replaced by new commits c3′ and d4′ with different ids. Never rebase a branch you have already pushed, or everyone else's history will diverge from yours.
reset: move the branch pointer back, orphaning those commits for now
git reset --hard HEAD~1
--soft moves only the pointer; --mixed also updates the index; --hard overwrites the working tree as well. The orphaned commit c3 is still recoverable from the reflog, which keeps entries for at least 30 days by default.
revert: cancel a commit with an inverse commit, without rewriting history
git revert HEAD
c3 stays in the history. The new commit r4 contains the inverse of what c3 changed. Use this rather than reset when the commit has already been pushed.
cherry-pick: take a single commit across
git cherry-pick x7
Replays the change x7 introduced relative to its parent onto main, producing x7′ with the same content and a different id. This is the usual way to carry a single fix to a release branch.
Practice terminal: type it out in the browser
A purely in-memory Git simulator (not real Git, and it never goes online): it supports the common subcommands init / add / commit / branch / switch / merge / rebase / reset / revert / stash / tag / remote / push / pull, and the graph on the right updates live. 10 levels run from "the very first one" to "resolving a conflict". Linux command practice is inthe Linux command reference.
git-lab — bashTab to complete · ↑↓ history · Ctrl+L to clear
Toolun Git practice terminal: an in-memory Git you can experiment with freely, and cannot break.
Type git help for the supported subcommands. Pick a lesson on the right and run the commands; the commit graph updates after every command.