Undo last commit but keep everything staged
Move HEAD back one commit while preserving index and working tree.
git reset --soft HEAD~1Great for rewriting the latest commit while keeping all changes ready to recommit.
Understand the differences between reset, revert, and restore and use the right recovery command safely.
Move branch tips and manipulate the index intentionally.
Move HEAD back one commit while preserving index and working tree.
git reset --soft HEAD~1Great for rewriting the latest commit while keeping all changes ready to recommit.
Move HEAD back one commit and leave changes in the working tree.
git reset --mixed HEAD~1`--mixed` is the default reset mode and is commonly used when you want to recommit changes differently.
Move HEAD back one commit and discard index and working tree changes.
git reset --hard HEAD~1Very destructive locally. Use only when you are sure you want to discard the changes.
Remove a path from the index without changing its contents in the working tree.
git reset HEAD path/to/fileClassic staging-area reset for one path. Modern workflows may also use `git restore --staged`.
Try to leave local changes intact while backing out of a merge-like operation.
git reset --merge ORIG_HEADUseful after failed merge or pull operations. Git documents `--merge` as intended for resetting out of conflicted merges.
Create new commits that reverse previous commits.
Create a new commit that reverses an earlier commit.
git revert <commit_sha>Preferred for undoing already-shared history because it preserves the public timeline.
Apply inverse changes into the index and working tree first.
git revert --no-commit <start_commit>^..<end_commit>Useful when combining multiple reversions into a single manual commit.
git revert -m 1 <merge_commit_sha>Reverting a merge requires specifying the mainline parent. Be careful because later merges can behave differently after this.
Resume revert after resolving conflicts.
git revert --continueRevert can stop on conflicts just like cherry-pick and rebase.
Restore file content from the index or another revision.
Restore a file in the working tree from the index.
git restore path/to/fileModern command for throwing away local unstaged edits to a path.
Restore the index entry for a path from HEAD.
git restore --staged path/to/fileModern alternative to `git reset HEAD path/to/file` when you only want to affect the staging area.
Copy file contents from a specific commit into the working tree.
git restore --source=<commit_sha> path/to/fileUseful when you need one path as it existed in a different revision without moving branch history.
Select portions of a file to restore interactively.
git restore --patch path/to/filePowerful for partial undo operations at hunk granularity.
Reset a file to another revision in both staging area and working tree.
git restore --source=<commit_sha> --staged --worktree path/to/fileUseful when you want a path to match another commit exactly in both locations.
Choose the right undo command for the situation.
Quick decision guide for the three similarly named commands.
reset = move branch/history
restore = restore file content/index state
revert = add new commit that undoes old commitGit’s official guidance distinguishes these commands clearly: reset moves refs/history, restore restores files/index, and revert records new inverse commits.
Save a recovery pointer before hard reset or risky history edits.
git branch backup-before-resetA simple but strong safeguard before destructive operations like hard reset or forceful history rewriting.
Find the previous branch tip after a bad reset.
git reflogIf you used the wrong reset mode or target, reflog is usually the fastest path back.