Git and GitHub · Part 1 · Git basics
Undoing things
Which undo, for which mistake
Almost any mistake in Git can be undone. The right command depends on where the mistake is, in the three areas from Lesson 4:
Working directory
git restore
Discard edits
Staging area
git restore --staged
Unstage
Repository
git reset / git revert
Undo commits
It's like fixing a mistake in a newspaper:
| Situation | In a newspaper | In Git |
|---|---|---|
| Still typing | Delete what was typed | git restore |
| Sent to the editor | Ask for it back | git restore --staged |
| Printed, not delivered | Reprint the page | git reset |
| Delivered to readers | Print a correction | git revert |
A delivered paper can't be taken back, only corrected. That's the difference between reset and revert.
A safe place to practice
cd ~/Desktop
mkdir undo-practice
cd undo-practice
git init
echo "line 1" > app.txt
git add app.txt
git commit -m "Add app file"Throwing away edits
1.git restore: undo changes to a file
echo "BROKEN CODE" >> app.txt
git restore app.txt
cat app.txtline 1The file is back to the last commit. git restore . does it for every file. To unstage instead, use git restore --staged, as in Lesson 4.
2.git clean: delete new files
git restore only works on tracked files. For new files, preview first with -n, then delete with -f:
echo "junk" > temp.txt
git clean -n
git clean -fWould remove temp.txtSome history to undo
echo "line 2" >> app.txt
git commit -am "Add line 2"
echo "line 3" >> app.txt
git commit -am "Add line 3"
echo "line 4" >> app.txt
git commit -am "Add line 4"
git log --onelined4d4d4d (HEAD -> main) Add line 4
c3c3c3c Add line 3
b2b2b2b Add line 2
a1a1a1a Add app filegit reset: undo commits
git reset moves the branch back, as if the later commits never happened:
- A
- B
- C
- Dmain
- A
- Bmain
- C
- D
git reset HEAD~2: C and D are no longer on main.
The mode decides what happens to their changes:
| Mode | The changes go to |
|---|---|
--soft | The staging area |
--mixed (default) | The working directory, unstaged |
--hard | Nowhere: they're deleted |
1.--soft: turn commits into one
git reset --soft HEAD~2
git commit -m "Add lines 3 and 4"
git log --onelinee5e5e5e (HEAD -> main) Add lines 3 and 4
b2b2b2b Add line 2
a1a1a1a Add app fileThe two commits were undone with their changes kept staged, so one commit replaced them.
2.--mixed: undo, keep the edits
git reset HEAD~1
git statusChanges not staged for commit:
modified: app.txtThe commit is gone; its changes are in the file, ready to stage again. Commit them again to continue:
git commit -am "Add lines 3 and 4"Same message, same changes, but a new commit, made at a new time, so it has a new hash: e7e7e7e.
3.--hard: throw it all away
git reset --hard HEAD~1
cat app.txtline 1
line 24.git reflog: get a commit back
Git keeps a log of everywhere HEAD has been, so a commit removed by reset can be found again:
git reflogb2b2b2b HEAD@{0}: reset: moving to HEAD~1
e7e7e7e HEAD@{1}: commit: Add lines 3 and 4git reset --hard e7e7e7eLines 3 and 4 are back. The reflog only keeps commits, and one no branch points to stays for 30 days by default. Edits that were never committed can't be rescued.
git revert: the safe undo
reset rewrites history, which is fine on one computer. Once commits are pushed and others have them, it breaks their copies. git revert deletes nothing: it adds a new commit that does the opposite of an old one.
- A
- B
- Cmain
- A
- B
- C
- C′main
git revert C adds C′, which undoes C. Nothing is removed.
git revert e7e7e7e --no-edit
git log --oneline
cat app.txtf6f6f6f (HEAD -> main) Revert "Add lines 3 and 4"
e7e7e7e Add lines 3 and 4
b2b2b2b Add line 2
a1a1a1a Add app file
line 1
line 2The lines are gone, and the history shows exactly what was undone. Without --no-edit, Git opens the editor for the message. Older commits can be reverted too, but if later commits changed the same or nearby lines, Git stops with a merge conflict for a person to resolve.
Bringing back one file
git restore --source=a1a1a1a app.txtThis puts app.txt back to how it was in that commit, without changing the history. Commit it to keep it, or run git restore app.txt to cancel.
Which one to use
| Situation | Command |
|---|---|
| Throw away unstaged edits | git restore file |
| Delete new files | git clean -n, then -f |
| Unstage, keep the edits | git restore --staged file |
| Fix the last commit | git commit --amend |
| Combine recent commits | git reset --soft HEAD~N |
| Undo commits, keep the changes | git reset HEAD~N |
| Throw commits away | git reset --hard HEAD~N |
| Undo a pushed commit | git revert hash |
| Restore one file | git restore --source=hash file |
| Find a lost commit | git reflog |
Older tutorials use git checkout -- file to discard edits. In 2019, Git 2.23 split checkout into git restore for files and git switch for branches. The old command still works, but the new ones are clearer.
Practice
In undo-practice, try each command once: git restore a junk line, git clean two junk files, combine three “wip” commits with reset --soft, rescue a reset --hard with reflog, and revert a commit.
Then decide: reset or revert?
- A. A password was committed 2 minutes ago. Nothing is pushed.
- B. A commit from last week broke the live site, and the whole team has it.
- C. The last 4 commits are “fix”, “fix again”, “fix pls” and “ok works”, none pushed.
Show the answers
- A. Reset. It never left the computer, so
git reset HEAD~1removes it from the history. Take the password out before committing again. - B. Revert. It's shared, so the history has to stay the same for everyone.
- C. Reset with --soft.
git reset --soft HEAD~4, then one commit with a good message.