Computing Foundations › Git and GitHub › Day 34

Day 34: Undoing Things: Reset, Revert, and Reflog

Day 34 of 365 — Undoing Things: Reset, Revert, and Reflog

After this lesson you will be able to fix a bad commit, un-stage and discard changes, rewind a branch with the right reset mode, safely undo shared history with revert, and recover "lost" commits with the reflog — so that nothing you do in Git ever feels irreversible again.

Course
Computing Foundations
Category
Git and GitHub
Reading time
≈ 40 min
Practical time
≈ 30 min
Lesson duration
1h 10m
Last verified
2026-07-12

Hands-on lab for this lesson

Lab files on GitHub: https://github.com/ai-roadmap-365/ai-roadmap-365.github.io/tree/main/labs/sections/computing-foundations/day-034-undoing-things-reset-revert-and-reflog

  1. Get the hands-on files. Clone the labs repository once (you can reuse this clone for every lesson). This works on macOS, Linux, and Windows (PowerShell or WSL):
    git clone https://github.com/ai-roadmap-365/ai-roadmap-365.github.io.git
    cd ai-roadmap-365.github.io
  2. Open this lesson's lab. Move into the directory for this specific day. Every lab lives at the same predictable path — section / subsection / week / day:
    cd labs/sections/computing-foundations/day-034-undoing-things-reset-revert-and-reflog
  3. Read the lab guide. Open `README.md` in that directory. It lists the exact commands, what each does, the expected output, and how to check your work — read it before running anything.
  4. Run it and check your work. Follow the README's "How to run" section: run the example first to see the finished result, then complete the numbered exercises in `starter/`, then run the tests. The tests pass (exit 0) only when your work is correct.
    bash tests/run_tests.sh   # or the test command named in the lab README

You can also open the lab as a local page (works offline, shows the file tree and expected output).

Learning objectives

By the end of this lesson you will be able to:

Prerequisites

Why this matters


The idea in plain language

Diagram: what can be recovered in Git and what cannot


Historical background


What it is — and what it is not

These commands are:

They are not:


Why it was created and what problems it solves


How it works

The three trees

Diagram: the working tree, the index and HEAD

Fixing the last commit

Diagram: how git commit --amend replaces the previous commit

git commit --amend -m "Fix typo in the data loader"

Un-staging and discarding

Diagram: git restore --staged compared with git restore

git restore --staged notes.md   # un-stage, keep the edits
git restore notes.md            # discard the edits entirely

Reset in depth

Diagram: how far each git reset mode reaches

git reset <commit> always moves the branch pointer. The modes differ in how far the change reaches.

Diagram: git reset modes and which of HEAD, the index, and the working tree each one moves

ModeMoves HEADResets indexResets working treeTypical use
--softyesnonoRedo or combine the last commit, keeping every change staged
--mixed (default)yesyesnoUn-commit and un-stage, to re-stage differently
--hardyesyesyesDiscard commits and uncommitted work, to return to a known state

Undoing a shared commit

Diagram: rewriting history compared with adding an inverse commit

git revert a1b2c3d

Flowchart: git revert adds a new inverse commit while git reset moves the branch pointer backward

The safety net: reflog

Diagram: the steps to recover a lost commit using the reflog

git reflog

An everyday analogy

Diagram: what git clean removes and how to check first

A word processor with three levels of undo.

WritingGit
The text on screenThe working tree
Text selected, ready to saveThe index
The last saved versionHEAD
Correcting the file name you just saved under--amend
Deselecting without deletingrestore --staged
Undoing your typing since the last saverestore <file> — destructive
Publishing a correction noticerevert
Pretending the last chapter was never writtenreset
The application’s crash-recovery logThe reflog

Examples in practice

Diagram: git revert compared with git reset


Implications: security, privacy, performance, scalability, and cost


Alternatives: free, open source, and commercial

Diagram: using git stash compared with resetting to clear the working tree

ToolTypeWhere it fits
git revertFree, built inAnything already shared. The safe default.
git resetFree, built inYour own unshared commits
git restoreFree, built inFiles, staged or unstaged
git stashFree, built inSetting changes aside without committing
git reflogFree, built inRecovery, when something has gone wrong
git fsck --lost-foundFree, built inFinding dangling commits the reflog missed

TermWhat it actually is
Working treeThe files on disk
IndexThe proposed next snapshot
HEADA pointer to your latest commit
AmendReplacing the last commit with a new one
ResetMoving the branch pointer, with varying reach
RevertA new commit that inverts an old one
ReflogA local log of everywhere HEAD has been

When to use it — and when not to

Diagram: choosing between the undo commands


The AI connection


Knowledge check

quiz:
  question: "After `git reset --hard HEAD~2`, a developer realises the two dropped commits were needed. They also had uncommitted edits to a third file. What can be recovered?"
  options:
    - "The two commits, from the reflog; the uncommitted edits are gone permanently"
    - "Both the commits and the edits, since --hard preserves the working tree in the reflog"
    - "Neither, because a hard reset removes commits from the object database"
    - "The uncommitted edits only, since dropped commits are garbage collected immediately"
  answer_index: 0
  explanation: "The commits were unreferenced rather than deleted, and their hashes are in the reflog. The uncommitted edits were never committed, so nothing ever recorded them — which is exactly why --hard is dangerous when the working tree is not clean."

Hands-on exercise

Break a throwaway repository on purpose, then bring it back. Worked through fully in the Day 34 lab directory. Because you build a fresh repository just for this, there is nothing of value to lose — so be fearless.

mkdir /tmp/undo-practice && cd /tmp/undo-practice
git init
git config user.email "you@example.com"
git config user.name "Undo Practice"
echo "line 1" > notes.txt
git add notes.txt
git commit -m "Frist commit"
What you are doingCommand
Fix the misspelled messagegit commit --amend -m "First commit"
Confirm the hash changedgit log --oneline
Make two more commitsedit, add, commit — twice
Simulate the disastergit reset --hard HEAD~2
Find what was lostgit reflog
Bring it backgit reset --hard <hash from the reflog>

Expected output

[main 8c1d2e3] First commit
HEAD is now at 8c1d2e3 First commit
5a6b7c8 HEAD@{0}: reset: moving to HEAD~2
2f3e4d5 HEAD@{1}: commit: Third commit
9a8b7c6 HEAD@{2}: commit: Second commit
8c1d2e3 HEAD@{3}: commit (amend): First commit
2f3e4d5 Third commit
9a8b7c6 Second commit
8c1d2e3 First commit

Validate your work

Troubleshooting

Common mistakes


Practice assignment


Extension challenge


Quiz

Q1. You just ran git commit and immediately noticed a typo in the commit message. Which command fixes it most directly?

  1. git revert HEAD and push
  2. git commit --amend -m "corrected message"
  3. git reset --hard HEAD~1
  4. git restore --staged .
Show answer

Answer: B. git commit --amend -m "corrected message"

git commit --amend replaces the most recent commit with a new one carrying the corrected message. The commit's hash changes because amend creates a new commit object rather than editing the old one in place.

Q2. What does git reset --soft HEAD~1 do to your staged changes and your working files?

  1. It creates a new commit that reverses the last commit
  2. It moves HEAD back one commit and deletes all uncommitted changes in the working tree
  3. It moves HEAD back but leaves the index and working tree alone
  4. It only un-stages files without moving HEAD
Show answer

Answer: C. It moves HEAD back but leaves the index and working tree alone

A --soft reset moves only the branch pointer (HEAD). The index and working tree are left exactly as they were, so every change from the rewound commit remains staged and ready to be re-committed.

Q3. Which git reset mode also overwrites your working-tree files, permanently discarding uncommitted changes?

  1. --soft
  2. --mixed
  3. All three affect the working tree equally
  4. --hard
Show answer

Answer: D. --hard

Only --hard moves all three: HEAD, the index, and the working tree. It forcibly rewrites your files to match the target commit, and any uncommitted changes are destroyed with no undo — which is why it is the one genuinely dangerous mode.

Q4. A bad commit is already pushed and three teammates have pulled it. What is the correct way to undo it?

  1. git revert <hash>, which adds an inverse commit
  2. git reset --hard HEAD~1, then git push --force
  3. git commit --amend, then push again
  4. Delete the .git folder and re-clone
Show answer

Answer: A. git revert <hash>, which adds an inverse commit

Once history is shared, rewriting it with reset and force-push breaks teammates' copies. git revert undoes the commit additively by recording a new inverse commit, so everyone can pull the change and stay in sync.

Q5. How does git revert differ from git reset?

  1. Revert deletes commits while reset keeps them
  2. Revert adds an inverse commit; reset moves the branch pointer
  3. Revert rewrites history by moving the pointer; reset adds a new commit
  4. They are identical; revert is just an alias for reset
Show answer

Answer: B. Revert adds an inverse commit; reset moves the branch pointer

Revert is additive and safe for shared branches: it leaves the original commit in place and records a new commit that cancels it. Reset is subtractive: it moves the branch pointer, rewriting what the history looks like.

Q6. After git reset --hard HEAD~2 drops two commits, how do you get them back?

  1. You cannot; a hard reset permanently deletes commits
  2. Run git pull to re-download them from the remote
  3. Run git reflog for the hash, then git reset --hard to it
  4. Run git commit --amend to rebuild them
Show answer

Answer: C. Run git reflog for the hash, then git reset --hard to it

The dropped commits become unreachable but still exist in the object store, and their hashes remain listed in git reflog. Pointing the branch back at the newest lost commit with git reset --hard restores the whole chain, since each commit points to its parent.

Q7. What is the git reflog?

  1. A shared log on the remote server showing everyone's commits
  2. A list of files currently staged for commit
  3. The same thing as git log, showing only reachable commits
  4. A local record of every position HEAD has occupied
Show answer

Answer: D. A local record of every position HEAD has occupied

The reflog (reference log) records every movement of HEAD in your own repository. It is local and never pushed, and because it references commits even after they fall off a branch, it is the primary recovery mechanism after a mistaken reset.

Q8. Which statement about Git's safety net is accurate?

  1. Committed work is nearly impossible to lose; uncommitted is not
  2. Nothing in Git can ever be recovered once a command runs
  3. Both committed and uncommitted work are always recoverable
  4. Only work pushed to a remote is safe
Show answer

Answer: A. Committed work is nearly impossible to lose; uncommitted is not

Commits are content-addressed objects that linger in the object store and are tracked by the reflog, so committed work is nearly always recoverable. Uncommitted edits in the working tree are not snapshots and not in the reflog, so git reset --hard erases them with no undo — hence the habit of committing or stashing first.

Glossary

amend
Replacing the most recent commit with a new one — used to fix its message or add a forgotten file. Because it builds a new commit object, the commit's hash changes.
reset
A command that moves the current branch pointer to a specified commit, optionally also updating the index and working tree depending on its mode (soft, mixed, or hard).
soft reset
git reset --soft: moves only HEAD to the target commit, leaving the index and working tree untouched, so the rewound changes remain staged.
hard reset
git reset --hard: moves HEAD, the index, and the working tree to the target commit, permanently discarding any uncommitted changes. The one genuinely destructive undo mode.
revert
A command that undoes a commit by creating a new commit which applies its exact inverse, preserving history. The safe way to undo commits that have already been shared.
restore
A command (added in Git 2.23) for undoing changes to files: git restore --staged un-stages a file, while git restore discards a file's uncommitted edits to match the last commit.
reflog
Short for "reference log": a local, private record of every position HEAD has occupied, including commits that no branch points to anymore, making it Git's primary recovery mechanism.
HEAD
A pointer to the commit you currently have checked out — normally the tip of the current branch, and the snapshot you would return to if you discarded everything else.
detached HEAD
The state where HEAD points directly at a specific commit instead of a branch, so new commits belong to no branch and can be lost if you switch away without saving them to one.
force push
git push --force: overwriting the remote branch with your local history, used after rewriting history. It can destroy teammates' commits; git push --force-with-lease is a safer variant that refuses to overwrite work you have not seen.
index
Also called the staging area: a holding area for the changes marked as ready to go into the next commit. git add copies changes into it; a commit freezes its contents into a new snapshot.

Sources and further reading


Kept in this browser, no account needed. Your progress page turns the whole record into one link you can bookmark or open on another device.