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:

  1. Working directory

    git restore

    Discard edits

  2. Staging area

    git restore --staged

    Unstage

  3. Repository

    git reset / git revert

    Undo commits

It's like fixing a mistake in a newspaper:

SituationIn a newspaperIn Git
Still typingDelete what was typedgit restore
Sent to the editorAsk for it backgit restore --staged
Printed, not deliveredReprint the pagegit reset
Delivered to readersPrint a correctiongit revert

A delivered paper can't be taken back, only corrected. That's the difference between reset and revert.

A safe place to practice

Terminal
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

Terminal
echo "BROKEN CODE" >> app.txt
git restore app.txt
cat app.txt
Output
line 1

The 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:

Terminal
echo "junk" > temp.txt
git clean -n
git clean -f
Output of -n
Would remove temp.txt

Some history to undo

Terminal
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 --oneline
Output (hashes simplified)
d4d4d4d (HEAD -> main) Add line 4
c3c3c3c Add line 3
b2b2b2b Add line 2
a1a1a1a Add app file

git reset: undo commits

git reset moves the branch back, as if the later commits never happened:

Before
  1. A
  2. B
  3. C
  4. Dmain
After
  1. A
  2. Bmain
  3. C
  4. D

git reset HEAD~2: C and D are no longer on main.

The mode decides what happens to their changes:

ModeThe changes go to
--softThe staging area
--mixed (default)The working directory, unstaged
--hardNowhere: they're deleted

1.--soft: turn commits into one

Terminal
git reset --soft HEAD~2
git commit -m "Add lines 3 and 4"
git log --oneline
Output
e5e5e5e (HEAD -> main) Add lines 3 and 4
b2b2b2b Add line 2
a1a1a1a Add app file

The two commits were undone with their changes kept staged, so one commit replaced them.

2.--mixed: undo, keep the edits

Terminal
git reset HEAD~1
git status
Output
Changes not staged for commit:
        modified:   app.txt

The commit is gone; its changes are in the file, ready to stage again. Commit them again to continue:

Terminal
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

Terminal
git reset --hard HEAD~1
cat app.txt
Output
line 1
line 2

4.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:

Terminal
git reflog
Output
b2b2b2b HEAD@{0}: reset: moving to HEAD~1
e7e7e7e HEAD@{1}: commit: Add lines 3 and 4
Terminal
git reset --hard e7e7e7e

Lines 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.

Before
  1. A
  2. B
  3. Cmain
After
  1. A
  2. B
  3. C
  4. C′main

git revert C adds C′, which undoes C. Nothing is removed.

Terminal
git revert e7e7e7e --no-edit
git log --oneline
cat app.txt
Output
f6f6f6f (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 2

The 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

Terminal
git restore --source=a1a1a1a app.txt

This 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

SituationCommand
Throw away unstaged editsgit restore file
Delete new filesgit clean -n, then -f
Unstage, keep the editsgit restore --staged file
Fix the last commitgit commit --amend
Combine recent commitsgit reset --soft HEAD~N
Undo commits, keep the changesgit reset HEAD~N
Throw commits awaygit reset --hard HEAD~N
Undo a pushed commitgit revert hash
Restore one filegit restore --source=hash file
Find a lost commitgit 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~1 removes 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.