CH 15 / 75Part 2: Git Basics — Working Locally

Undoing Things, Part 2 (revert and reflog)

What you will learn on this page

  • Understand the difference between git reset and git revert, and how to choose between them
  • Cancel out changes with git revert while keeping the record
  • Find a commit you thought you'd undone with git reflog
  • Understand what even reflog can't recover
On this page8 items

At the end of Part 2, #14, we undid the “Add post office to to-do list” commit along with its changes. This page first gets that commit back. Then it covers another way of undoing: canceling changes out without erasing the record.

The difference between reset and revert#

The top row is git revert: after A, B and C, a commit that cancels out C is added, and C stays in the history. The bottom row is git reset --hard: main moves back to B, and C drops out of the history

Both operations “make C’s changes as if they never happened,” but they go about it in opposite ways.

  • git reset moves the branch back to a past commit. C no longer appears in git log, and the history itself is rewritten
  • git revert adds the exact opposite of C’s changes as a new commit. C stays in the history, and only the contents return to the state before C

The criterion for choosing is whether you’ve shared that commit with anyone.

SituationWhat to useWhy
A commit that exists only on your machinereset (Part 2, #14)Keeps the history clean. Nobody is affected
A commit you’ve shared with othersrevertIt doesn’t rewrite history, so it won’t conflict with other people’s history
When in doubtrevertIt just adds a record; nothing is lost

Sharing is covered in Part 6. In Parts 2 to 5, you’re working only inside a practice folder, so using either one won’t cause trouble. While you can still try both, get a hands-on feel for the difference.

reflog: a record of where HEAD has moved#

git log shows only “commits reachable from the current branch.” The commit you undid no longer appears.

text6 lines
cd ~/git-practice
git log --oneline -n 2

# Output:
# 1acb8f0 Add bread to shopping list
# b90cd28 Stop tracking config.txt

However, Git doesn’t delete the commit itself right away. And it separately records when HEAD moved and to which commit. That’s the reflog (reference log).

text6 lines
git reflog -n 3

# Output:
# 1acb8f0 HEAD@{0}: reset: moving to HEAD~1
# fd7152c HEAD@{1}: commit: Add post office to to-do list
# 1acb8f0 HEAD@{2}: commit: Add bread to shopping list

The movements of HEAD are listed, newest first.

  • HEAD@{0}: Now. Came back to 1acb8f0 via reset
  • HEAD@{1}: Just before that. Created the fd7152c commit
  • HEAD@{2}: Further back

fd7152c at HEAD@{1} is the “Add post office to to-do list” commit we undid. Once you know the hash, you can go back to it.

Getting back a commit you undid#

text9 lines
git status -s

# Output:
# (nothing is displayed)

git reset --hard fd7152c

# Output:
# HEAD is now at fd7152c Add post office to to-do list

The branch points to fd7152c again, and the “Go to the post office” line is back in todo.txt.

text6 lines
git log --oneline -n 3

# Output:
# fd7152c Add post office to to-do list
# 1acb8f0 Add bread to shopping list
# b90cd28 Stop tracking config.txt

revert: canceling out while keeping the record#

Now let’s cancel out the recovered “Add post office to to-do list” commit, this time with revert.

text6 lines
git revert --no-edit HEAD

# Output:
# [main bb5686f] Revert "Add post office to to-do list"
#  Date: Thu Oct 22 01:03:21 2026 +0900
#  1 file changed, 1 deletion(-)

HEAD specifies the commit you want to cancel out. Commits other than the most recent one can be specified by hash, as in git revert 1acb8f0. Without --no-edit, an editor opens so you can edit the message of the canceling commit.

text6 lines
git log --oneline -n 3

# Output:
# bb5686f Revert "Add post office to to-do list"
# fd7152c Add post office to to-do list
# 1acb8f0 Add bread to shopping list

The original commit fd7152c remains, and the canceling commit bb5686f has been added on top. The “Go to the post office” line is gone from todo.txt, but the history still shows that it was “added once and later undone.”

What even reflog can’t recover#

The reflog is powerful, but it isn’t all-powerful.

What can be recoveredWhat can’t be recovered
Anything you’ve committed at least onceEdits you never committed (things discarded by the “erasing” operations in Part 2, #14)
Commits undone with reset or amendOld reflog entries past their expiry
Operations on another PC (the reflog exists only on that PC)

By default, undone commits can be traced from the reflog for roughly 30 days. After that, Git may delete them when it cleans up.

There’s one takeaway from this: as long as you’ve committed it, Git can recover almost anything. Committing frequently at natural breakpoints in your work is the most reliable insurance.

FAQ#

Q. The same hash appears in the reflog several times.
A. It means HEAD came back to that commit several times. The reflog isn’t a record of “which commits exist” but of “how HEAD moved,” so the same commit can appear repeatedly.

Q. What happens if you revert a commit that was itself a revert?
A. It cancels the cancellation, and the original change comes back. All three commits remain in the history.

Summary#

  • reset moves the branch back and rewrites history; revert adds a canceling commit and keeps the history
  • Cancel out shared commits with revert. When in doubt, use revert
  • git reflog is a record of HEAD’s movements, and you can use it to find the hash of a commit you undid
  • Edits you never committed can’t be recovered, even with the reflog
  • Frequent commits are the most reliable insurance

Next page#

16. Git saves snapshots, not diffs ── This is where Part 3 begins. We look at what Git saves inside a commit, starting from how it works.