Skip to content

Git Undo, Recovery, and Debugging

Category Git & GitHub
Status โœ… Completed
Repo nextwork-git-undo-recovery-and-debugging
Original NextWork guide nextwork.ai

Learning project

Practice work following a NextWork guide, separate from my production projects.

๐Ÿ“š NextWork learning project. This is a practice project I built while working through NextWork's Git Undo, Recovery, and Debugging guide. It is part of my Learning portfolio and is fully separate from my production projects.

Category: Learning โ€บ Git & GitHub
Status: โœ… Completed

Overview

This is a scripted Git session that creates a realistic history with a bug in it. It then undoes the bug safely with git revert, recovers a deleted branch from the reflog, cherry-picks a hotfix onto a release branch, and lets git bisect run find a second bug automatically.

Summary

Reset, revert, reflog recovery, cherry-pick, and an automated bisect all ran. The release tag is pushed to this repo.

The checks behind this are in docs/verification.txt.

Skills & tools

Git reset/revert, git reflog, git cherry-pick, git bisect, Release tags

What I built

  • practice.sh: builds a calculator repo with a test, then works through every recovery tool.
  • The bisect step uses git bisect run python3 test_calc.py, so Git finds the bad commit without any manual steps.
  • Secret mission: v1.0.0 is an annotated tag on this repo (see Releases/Tags).

How to run

bash practice.sh            # replays the whole session in a temp folder
bash practice.sh ./sandbox  # or in a folder you choose

Verification

I ran the verification command on 2026-12-02. The full output is in docs/verification.txt.

bash practice.sh

Notes

docs/post-publish.txt shows the tag push.


Original project: https://nextwork.ai/projects/2a174c0b-492b-472c-b8de-11c58f71253a ยท Part of my NextWork Learning portfolio (hub).

Verification output

From docs/verification.txt:

$ bash practice.sh


=================== Build a practice history ===================

$ git log --oneline
3fa63bb Add note 5
2b01a44 Add note 4
a0aeaca Refactor add (introduces a bug)
3e4e5a6 Add note 3
1081164 Add note 2
6d2628c Add note 1
b99b2c1 Add calculator with add()

=================== Undo a local commit with reset (soft vs hard) ===================

$ git reset --soft HEAD~1

$ git status --short
A  scratch.txt

$ git restore --staged scratch.txt

=================== Undo a public commit with revert (history is kept) ===================

$ git revert --no-edit a0aeaca
[main a7d060b] Revert "Refactor add (introduces a bug)"
 Date: Thu Jan 1 09:00:00 2026 -0600
 1 file changed, 1 insertion(+), 1 deletion(-)

$ python3 test_calc.py
tests pass

$ git log --oneline -4
a7d060b Revert "Refactor add (introduces a bug)"
3fa63bb Add note 5
2b01a44 Add note 4
a0aeaca Refactor add (introduces a bug)

=================== Recover lost commits with reflog ===================

$ git switch -c experiment
Switched to a new branch 'experiment'

$ git switch main
Switched to branch 'main'

$ git branch -D experiment
Deleted branch experiment (was 6c17056).
(branch deleted; now find it again)

$ git reflog -8
a7d060b HEAD@{0}: checkout: moving from experiment to main
6c17056 HEAD@{1}: commit: Add multiply (experiment)
a7d060b HEAD@{2}: checkout: moving from main to experiment
a7d060b HEAD@{3}: revert: Revert "Refactor add (introduces a bug)"
3fa63bb HEAD@{4}: reset: moving to HEAD~1
93cd737 HEAD@{5}: commit: Accidental scratch commit
3fa63bb HEAD@{6}: commit: Add note 5
2b01a44 HEAD@{7}: commit: Add note 4

$ git branch recovered 6c17056

$ git log --oneline -1 recovered
6c17056 Add multiply (experiment)

=================== Cherry-pick a hotfix across branches ===================

$ git switch -c release-1.x a0aeaca~1
Switched to a new branch 'release-1.x'

$ git switch main
Switched to branch 'main'

$ git switch release-1.x
Switched to branch 'release-1.x'

$ git cherry-pick a9057a5
[release-1.x f1b7525] Hotfix: document input validation
 Date: Thu Jan 1 09:00:00 2026 -0600
 1 file changed, 1 insertion(+)
 create mode 100644 SECURITY.md

$ git log --oneline -3
f1b7525 Hotfix: document input validation
3e4e5a6 Add note 3
1081164 Add note 2

$ git switch main
Switched to branch 'main'

=================== Find a bug with git bisect ===================

$ git bisect start HEAD 5732fdc
Bisecting: 0 revisions left to test after this (roughly 1 step)
[d265a9e701d0b929997b618a8418798daa0d683a] Add note 8

$ git bisect run python3 test_calc.py
running 'python3' 'test_calc.py'
Traceback (most recent call last):
  File "/tmp/tmp.W5EU76HXxZ/test_calc.py", line 2, in <module>
    assert add(2, 3) == 5
           ^^^^^^^^^^^^^^
AssertionError
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[f686efc207dd6a6904e42431c93125dbb23ea27c] Tweak add
running 'python3' 'test_calc.py'
Traceback (most recent call last):
  File "/tmp/tmp.W5EU76HXxZ/test_calc.py", line 2, in <module>
    assert add(2, 3) == 5
           ^^^^^^^^^^^^^^
AssertionError
f686efc207dd6a6904e42431c93125dbb23ea27c is the first bad commit
commit f686efc207dd6a6904e42431c93125dbb23ea27c
Author: Learner <learner@example.com>
Date:   Thu Jan 1 09:00:00 2026 -0600

    Tweak add

 calc.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
bisect found first bad commit

$ git bisect reset
Previous HEAD position was f686efc Tweak add
Switched to branch 'main'
bisect blamed: f686efc Tweak add

=================== Secret mission: tag a release ===================

$ git revert --no-edit HEAD~2
[main a148c1e] Revert "Tweak add"
 Date: Thu Jan 1 09:00:00 2026 -0600
 1 file changed, 1 insertion(+), 1 deletion(-)

$ python3 test_calc.py
tests pass

$ git tag -a v1.0.0 -m First stable release

$ git tag -n
v1.0.0          First stable release

[exit code 0]