CodeRigor

FREE LESSON

Undo, reflog & lost-commit recovery

Undo a shared change with a new commit and recover an unreferenced local tip without destroying later work.

Undo is not one operation. First name the thing you want to change: an unstaged file edit, the next staged snapshot, a branch reference, or the effect of an already shared commit. These require different tools. Start with status and a graph rather than reaching for the most destructive command you remember.

Choose the right undo target
start / 4
Choose the right undo target

Undo a published change by adding history

git revert <commit> applies an inverse change and normally records a new commit. Earlier commits remain ancestors, so colleagues can advance through the repair without having their existing history replaced. Reverting one commit does not mean restoring the entire tree to its parent: unrelated later changes should remain. The inverse patch can conflict with subsequent edits, requiring a deliberate resolution. Reverting a merge also requires choosing a mainline parent; it is not covered by the simple single-parent lab.

Git commands
git log --graph --decorate --oneline
git reflog -6
git branch recovered "HEAD@{1}"
git revert --no-edit bad-change

git reset moves a reference and can also change the index or working tree depending on its mode. Soft reset keeps index and working contents; mixed reset normally resets the index while leaving working files; hard reset also replaces tracked working contents and can remove obstructing untracked paths. These modes answer different questions from adding a repair commit to shared history. A hard reset is not a general "undo the bad change but keep later work" operation.

Repair without removing later work
start / 4
Commit arrows point to parents. R reverses B’s effect but retains C and all prior history.
Repair without removing later work
Commit arrows point to parents. R reverses B’s effect but retains C and all prior history.

Find a lost local tip

Reflogs record local reference movement. The HEAD reflog can retain the commit you just left even after its branch name was deleted. HEAD@{1} means the previous recorded value in this repository's HEAD log, not the first parent of HEAD and not a globally portable commit name. Inspect the log because additional operations can change which entry has that index. Quote the expression in shells that treat braces specially.

Create a named branch at the recovered commit before experimenting further. This changes reachability without moving the current main branch or replacing working files. Reflogs are local and expire; unreachable objects can eventually be pruned. They are useful recovery evidence, not a permanent backup service and not a way to retrieve arbitrary teammates' deleted commits that never existed here.

Recover a local tip
start / 4
Recover a local tip

Practice two different repairs

The lab combines a shared-history repair with a local-reference recovery. First inspect the reflog and preserve the abandoned experiment under recovered. Then revert the specific regression while keeping the later documentation. The grader verifies the repaired content, the retained published ancestor, the new repair commit and the recovered experiment. A snapshot that merely looks like an older version is insufficient if it erased the collaboration history or lost the independent experiment.

Watch & read

Free lectures, talks and readings that cover this skill from another angle. All resources →

Review the object/reference model and core workflows here. The authored lesson and linked Git manual cover the current command forms and advanced details used in this lab.