Git Bisect - Git Commit 故障排查利器使用详解

『AI先锋杯·14天征文挑战第8期』 10w+人浏览 328人参与

在软件开发过程中,我们会突然遇到代码仓库中的某个功能突然不工作了,却不知道是哪个提交引入的错误。这时,git bisect 就是我们的利器。

下面将探讨 git bisect 的使用技巧,更高效地定位和解决问题。

什么是 Git Bisect

git bisect 是 Git 分布式版本控制系统中的一个非常有用的命令,它通过一个二分查找的过程来确定引入错误的具体提交,这个过程可以显著减少查找错误的时间。

Git Bisect 使用 的基本步骤:

首先,你需要确定一个已知的错误提交(当前发现错误的提交的Commit ID)和一个已知的好提交(代码能正常工作的提交的Commit ID)。

然后,使用 git bisect start 命令开始 bisect 过程

启动Git bisect

git bisect start   ## 开始 bisect 

用git bisect标记

告诉Git哪个是已知的错误的和好的提交

git bisect bad  # 标记当前已知的错误的提交
git bisect good [good_commit_hash]  # 标记已知的好的提交

检查中间提交

Git 将自动检查出位于好和坏提交之间的中间提交,然后需要手工检查这个中间提交是否正常工作


make && ./test # 检查当前的中间提交

标记结果,缩短区间

根据测试结果,确定错误范围,再次使用 git bisect good 或 git bisect bad 来标记新的好的或错误的提交范围。

重复上述二分查找,直到找到引入错误的那个提交。

结束 Bisect 过程

当找到错误的提交后,可以使用 git bisect reset 结束 bisect 过程,并返回到原始的起点

git bisect reset

make[1]: Leaving directory `/usr/local/src/git-2.30.0/git-gui' bindir=$(cd '/usr/local/bin' && pwd) && \ execdir=$(cd '/usr/local/libexec/git-core' && pwd) && \ destdir_from_execdir_SQ=$(echo 'libexec/git-core' | sed -e 's|[^/][^/]*|..|g') && \ { test "$bindir/" = "$execdir/" || \ for p in git git-shell git-cvsserver; do \ rm -f "$execdir/$p" && \ test -n "" && \ ln -s "$destdir_from_execdir_SQ/bin/$p" "$execdir/$p" || \ { test -z "" && \ ln "$bindir/$p" "$execdir/$p" 2>/dev/null || \ cp "$bindir/$p" "$execdir/$p" || exit; } \ done; \ } && \ for p in git-receive-pack git-upload-archive git-upload-pack; do \ rm -f "$bindir/$p" && \ test -n "" && \ ln -s "git" "$bindir/$p" || \ { test -z "" && \ ln "$bindir/git" "$bindir/$p" 2>/dev/null || \ ln -s "git" "$bindir/$p" 2>/dev/null || \ cp "$bindir/git" "$bindir/$p" || exit; }; \ done && \ for p in git-add git-am git-annotate git-apply git-archive git-bisect--helper git-blame git-branch git-bugreport git-bundle git-cat-file git-check-attr git-check-ignore git-check-mailmap git-check-ref-format git-checkout-index git-checkout git-clean git-clone git-column git-commit-graph git-commit-tree git-commit git-config git-count-objects git-credential-cache--daemon git-credential-cache git-credential-store git-credential git-describe git-diff-files git-diff-index git-diff-tree git-diff git-difftool git-env--helper git-fast-export git-fast-import git-fetch-pack git-fetch git-fmt-merge-msg git-for-each-ref git-for-each-repo git-fsck git-gc git-get-tar-commit-id git-grep git-hash-object git-help git-index-pack git-init-db git-interpret-trailers git-log git-ls-files git-ls-remote git-ls-tree git-mailinfo git-mailsplit git-merge-base git-merge-file git-merge-index git-merge-ours git-merge-recursive git-merge-tree git-merge git-mktag git-mktree git-multi-pack-index git-mv git-name-rev git-notes git-pack-objects git-pack-redundant git-pack-refs git-patch-id git-prune-packed git-prune git-pull git-push git-range-diff git-read-tree git-rebase git-receive-pack git-reflog git-remote-ext git-remote-fd git-remote git-repack git-replace git-rerere git-reset git-rev-list git-rev-parse git-revert git-rm git-send-pack git-shortlog git-show-branch git-show-index git-show-ref git-sparse-checkout git-stash git-stripspace git-submodule--helper git-symbolic-ref git-tag git-unpack-file git-unpack-objects git-update-index git-update-ref git-update-server-info git-upload-archive git-upload-pack git-var git-verify-commit git-verify-pack git-verify-tag git-worktree git-write-tree git-cherry git-cherry-pick git-format-patch git-fsck-objects git-init git-maintenance git-merge-subtree git-restore git-show git-stage git-status git-switch git-whatchanged; do \ rm -f "$execdir/$p" && \ if test -z ""; \ then \ test -n "" && \ ln -s "$destdir_from_execdir_SQ/bin/git" "$execdir/$p" || \ { test -z "" && \ ln "$execdir/git" "$execdir/$p" 2>/dev/null || \ ln -s "git" "$execdir/$p" 2>/dev/null || \ cp "$execdir/git" "$execdir/$p" || exit; }; \ fi \ done && \ remote_curl_aliases="git-remote-https git-remote-ftp git-remote-ftps" && \ for p in $remote_curl_aliases; do \ rm -f "$execdir/$p" && \ test -n "" && \ ln -s "git-remote-http" "$execdir/$p" || \ { test -z "" && \ ln "$execdir/git-remote-http" "$execdir/$p" 2>/dev/null || \ ln -s "git-remote-http" "$execdir/$p" 2>/dev/null || \ cp "$execdir/git-remote-http" "$execdir/$p" || exit; } \ done && \
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

☼←安于亥时→❦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值