متقدم
البحث فيالمعامل
طريقة البحث
  • ابحث عن مهمة أو أداة (lvm، selinux، ‎@podman)؛ وصفِّ حسب المستوى والكتاب أو الدورة.
  • مع مربع بحث فارغ، الفلتر وحده يعرض المعامل — مثلًا كل المعامل الصعبة لدورة واحدة.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

معمل 7.1Git, الفصل 7

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

المستوى
متوسط
40 دقيقة
الأجهزة
any computer with Git 2.30 or newer, Bash
الحالة
لم يبدأ

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

الهدف

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.

قبل أن تبدأ

المهام

  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.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.

تلميحات

اعرض التلميحات

الحل

اعرض الحل
  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.

ملاحظاتي

لا توجد ملاحظات بعد.

دروس هذا المعمل

الأوامر في هذا المعمل