Undoing changes
Understand the idea
Revert adds a new commit that undoes a change. Reset moves a branch label and can throw shared history away.
The idea
git revert adds a that undoes an earlier change and leaves history in place. git reset moves a label. --soft keeps the index and files, --mixed keeps the files and clears the index, --hard makes the files match the commit you named. git stash parks a dirty tree so you can switch branches without committing it.
What changes
Revert changes history by adding a node. Reset changes which node the label names, and maybe the . git restore copies a file back from the index or from HEAD without moving the branch. A later push --force is the dangerous version of reset: it moves the remote label even when that drops commits. A normal push refuses that case.
| Command | History | Files |
|---|---|---|
| git revert | New commit, old one stays | The change is reversed |
| git reset --soft | Label moves back | Index and files stay |
| git reset --hard | Label moves back | Files match the target |
| git stash | No new commit | Dirty edits are set aside |
The command
Change a file, stash it, and run git stash list then git stash pop. On a commit you have not pushed, try git reset --mixed HEAD only when you mean to uncommit. Prefer git revert HEAD once the commit might be shared. The force-push article linked below is the remote form of this same choice.
The mistake
Check your graph
After revert, the old commit is still on the graph and a new commit sits on top. After reset, the branch label has moved and git reflog still names the commit you left.
Your repository
Stash a dirty tree, then compare reset and revert on a commit you have not pushed.
Step 1
Make a dirty edit
In the editor, add this line to README.md: temporary note
Step 2
Park the edit
Stash sets the working tree back to the commit and keeps your edit in the stash list.
Step 3
See the parked edit
The list is the shelf. The branch did not move.
Step 4
Bring the edit back
pop restores the working tree. The editor shows the temporary line again.
Step 5
Stage the note
The popped edit is back in the working tree. Add it before the commit.
Step 6
Commit something you can undo
This commit is the one revert will reverse.
Step 7
Undo with a new commit
Revert adds a commit that reverses the last change. The old commit stays on the graph.
Step 8
See both commits
The original commit and the revert are both still there.
The editor edits the working tree. Start a repository and the files appear here.
Commit graph
No commits yet. git init creates the first one.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 150 XP. You can always revisit it.