These recipes are written with the caveats attached: what changes, what is recoverable through the reflog, and what is not. Copy the command, read the note, and check `git status` before running anything destructive.
Git Commands
Most Git problems are not about learning a new command; they are about remembering which of three similar commands is safe. `reset --soft`, `--mixed` and `--hard` differ in what they do to the index and working tree. `revert` and `reset` differ in whether history is rewritten. `force` and `force-with-lease` differ in whether you can overwrite someone else's work.
What people use these for
- Undo the last commit without losing the file changes.
- Move a commit from one branch to another without merging.
- Find the commit that introduced a bug with a scripted bisect.
- Clean up a feature branch before opening a pull request.
Guides and reference
10 pagesGit Cheat Sheet
A practical Git cheat sheet: status, staging, commits, branches, remotes, undo, stash, log and recovery commands, with the safest form of each.
Revert a Commit
Revert a commit with git revert without rewriting history. Learn the command, conflict handling, reverting merges, and when to use reset instead.
Undo Commit
Undo an unpushed commit with git reset. Compare --soft, --mixed and --hard, recover with reflog, and know when to use revert instead.
Cherry Pick
Cherry-pick a specific commit onto another branch. Learn the command, conflict resolution, ranges, -x provenance and when to avoid it.
Interactive Rebase
Clean up a branch with git rebase -i. Learn pick, squash, fixup, reword, edit and drop, conflict handling, and the force-with-lease rule.
Git Stash
Use git stash to park changes, switch branches, and restore them later. Compare pop and apply, include untracked files, and recover dropped stashes.
Git Bisect
Use git bisect to find the commit that introduced a bug in log2(n) steps. Learn the manual and automated workflows, and how to reset afterward.
Git Submodule
Manage Git submodules: add, clone with --recursive, update to the recorded commit, sync URLs, and remove a submodule cleanly.
Discard Changes
Discard tracked and untracked changes safely. Learn git restore, the legacy checkout form, clean previews, and how to recover if you discard too much.
Git Branches
List local and remote branches, delete merged branches safely, prune stale remote refs, and rename a branch without losing tracking.
The reflog is your safety net
`git reflog` records where HEAD has been, including commits that are no longer on any branch. After an accidental reset or rebase, the old tip is usually still there for 30 to 90 days. `git reset --hard ORIG_HEAD` or a specific reflog entry can restore it. The net does not cover uncommitted changes, and it does not survive aggressive garbage collection.
Rewrite history only before sharing
Rebase, amend and reset rewrite commit IDs. On a branch only you own, that is fine. On a shared branch, it forces everyone else to reconcile divergent history. `git push --force-with-lease` is the least bad way to update a rewritten branch, because it refuses if someone else pushed in the meantime; plain `--force` does not.
Frequently asked questions
Does git reset --hard delete my work?
It discards uncommitted changes in tracked files and moves the branch. Committed work can usually be recovered through the reflog; uncommitted work cannot.
When should I use revert instead of reset?
Use revert on shared branches. It creates a new commit that undoes the change, so nobody else's history is rewritten.
Is rebase better than merge?
Rebase produces a linear history and is easier to read; merge preserves the real branch topology. The right choice depends on team policy and whether the branch is shared.