Advanced
Searching inLabs
How to search
  • Search a task or a tool (lvm, selinux, @podman); filter by level and by book or course.
  • With an empty search box, a filter alone lists the labs — for example all hard labs of one course.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Lab 7.1Git, Chapter 7

Find the bad commit with bisect, park work with stash, release with a tag

Level
Medium
40 min
Machines
any computer with Git 2.30 or newer, Bash
Status
Not started

Goal

A script worked in version 1 and is broken now, 20 commits later. You build that history with a small loop, then use git bisect run to find the bad commit automatically, git blame and git log -S to see who changed the line, git stash to park unfinished work during the hotfix, and an annotated tag to mark the fixed release.

Before you start

Tasks

  1. Make 20 commits with a loop. In every commit, check.sh prints OK; only in commit 13 the line becomes echo FAIL; exit 1, and that stays until the end. Tag the first commit v1.0 (annotated).
  2. Start git bisect with the newest commit as bad and v1.0 as good. Let Git test every step with git bisect run ./check.sh.
  3. Write down the commit Git found, then end bisect.
  4. Show who changed the line with git blame, and find the commit with git log -S "FAIL".
  5. Before you fix it, change notes.txt (unfinished work). Park it with git stash push -m, check that the tree is clean.
  6. Fix check.sh, commit, and make the annotated tag v1.1 with a message.
  7. Bring your stashed work back and show the stash list is empty.

Check your work

./check.sh; echo "exit=$?"
git tag -n
git stash list
git status --short
  • ./check.sh prints OK and exit=0.
  • git tag -n shows v1.0 and v1.1 with their messages.
  • git stash list is empty and git status --short shows M notes.txt (your work is back).
  • Bisect found the commit with the message change 13.

Hints

Show the hints

Solution

Show the solution
  1. Build the history.
    printf '#!/bin/sh\necho OK\n' > check.sh && chmod +x check.sh
    echo "notes" > notes.txt
    git add . && git commit -qm "change 0"
    git tag -a v1.0 -m "First good version"
    for i in $(seq 1 20); do echo "step $i" >> notes.txt; [ "$i" -eq 13 ] && printf '#!/bin/sh\necho FAIL; exit 1\n' > check.sh; git commit -qam "change $i"; done
    ./check.sh; echo $?
    FAIL
    1
  2. Let Git search.
    git bisect start main v1.0
    git bisect run ./check.sh
  3. Read the result.
    <hash> is the first bad commit
        change 13
    git bisect reset
  4. Who changed it, and when.
    git blame check.sh
    git log -S "FAIL" --oneline
  5. Park the unfinished work.
    echo "half-done idea" >> notes.txt
    git stash push -m "wip notes"
    git status --short
  6. Hotfix and release tag.
    printf '#!/bin/sh\necho OK\n' > check.sh
    git commit -qam "Fix check.sh (bug from change 13)"
    git tag -a v1.1 -m "Hotfix: check.sh prints OK again"
  7. Get the work back.
    git stash pop
    git stash list
    git push origin v1.1

    The last command needs a remote; tags are not pushed by a normal git push.

My notes

No notes yet.

Lessons for this lab

Commands in this lab