متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 156Git · CI/CD

How to find the commit that broke something

It worked in version 1.4.0, it is broken now, and there are 200 commits in between. Reading them all takes a day. git bisect does a binary search: it checks out a commit in the middle, you say "good" or "bad", and after about 8 steps (log₂ 200) it names the exact commit — and with a test script it does it alone. This how-to finds a regression by hand and automatically.

  • 5 الدروس
  • 3 الأوامر

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

$ مستخدم عادي# بصلاحية root (أو مع sudo)

156.1

The quick answer

git bisect start
git bisect bad
git bisect good v1.4.0
git bisect good
git bisect bad
git bisect reset

After start, mark the current commit bad and the last known good one; then test each commit Git checks out and answer good or bad until it prints … is the first bad commit.

الأوامر في هذا الدرس

156.2

Step by step

  1. Know a good and a bad point — a tag, a commit, a date. A test that tells good from bad in a minute (a unit test, a curl, a script).
  2. Start and mark both ends (quick answer). Git checks out the middle commit:
git bisect good v1.4.0
Bisecting: 98 revisions left to test after this (roughly 7 steps)
[4f2e1d0…] Refactor session handling
  1. Test and answer — build/run your test, then git bisect good or git bisect bad. A commit you can not test (does not build): git bisect skip.
  2. Automatic — a script that exits 0 for good and 1 for bad; Git runs the whole search alone:
test-login.sh
#!/usr/bin/env bash
set -euo pipefail
pip install -q -r requirements.txt
pytest -q tests/test_login.py
chmod +x test-login.sh
git bisect start HEAD v1.4.0
git bisect run ./test-login.sh
3f9c2e1… is the first bad commit

Exit code 125 from the script means "skip this commit".

  1. Read the result:
git show 3f9c2e1 --stat
git bisect log > bisect.log

Attach the log to the ticket.

  1. Finish — always go back to where you were:
git bisect reset

156.3

Check that it worked

git bisect names one commit; checking out the commit before it passes the test, and the named commit fails it — then you know exactly which change to fix or revert.

الأوامر في هذا الدرس

156.4

Common problems

ProblemCauseFix
The result looks wrongOne good/bad answer was a mistake (flaky test).git bisect log, edit it, git bisect replay; use a stable test.
Many commits do not buildBroken intermediate commits.git bisect skip (or exit 125 in the script).
You are left in "detached HEAD"Bisect still running.git bisect reset.
The test script needs files that old commits lackThe script lives in the repo.Keep the script outside the repo (or in /tmp).
Merges make the result a merge commitThe bug came from the combination.git bisect start --first-parent to stay on main's line, then inspect that merge.
Takes long to build each stepHeavy builds.Faster test (one unit test), cache dependencies.

الأوامر في هذا الدرس

156.5

Go further

1About how many steps does bisect need for 200 commits?

About 8 — it halves the range each time (log₂ 200).

2How do you make git bisect run the search alone?

git bisect run ./script.sh — exit 0 = good, 1 = bad, 125 = skip.

الأوامر في هذا الدرس

كل الأوامر في هذا الفصل