Git Advanced Workflows: Rebasing, Cherry-picking, and Bisect

Beyond basic commits and merges, Git provides powerful tools for history manipulation and debugging. These three commands are the ones that separate intermediate Git users from advanced ones.

Rebase

Rebasing replays your commits on top of another branch, producing linear history without merge commits. The basic use: after working on a feature branch while main has moved forward, git rebase main updates your feature branch to start from the latest main commit. Conflicts are resolved per commit (not all at once as in merge). Interactive rebase (git rebase -i HEAD~5) lets you reorder, squash, edit, or drop the last 5 commits — essential for cleaning up messy commit history before a PR. Golden rule: never rebase commits that have been pushed to a shared remote branch that others are working from.

Cherry-pick

Cherry-pick applies a specific commit from one branch to another without merging the entire branch. Use case: a bug fix is committed to a feature branch and you want to backport it to the release branch without merging the entire feature.

git checkout release-1.0
git cherry-pick abc1234  # apply commit abc1234 from any branch to current branch

Cherry-pick creates a new commit with the same changes but a different hash. Useful for hotfixes, backporting, and selective changes between long-lived branches.

Bisect

Git bisect performs a binary search through commit history to find which commit introduced a bug. Start bisect, mark the current bad commit, mark a known good commit, and Git automatically checks out the midpoint. You test, mark it good or bad, and Git narrows down — finding the culprit commit in O(log n) steps.

git bisect start
git bisect bad           # current commit is bad
git bisect good v2.0     # v2.0 tag was known good
# Git checks out midpoint — run your test, then:
git bisect good   # or
git bisect bad
# Repeat until Git identifies the introducing commit
git bisect reset  # return to original HEAD when done

Bisect is invaluable when a regression appears but the commit range is large — it turns hours of manual investigation into minutes of systematic search.

上一篇 在德国找公寓:没人告诉你的流程细节
下一篇 Git高级工作流:变基、精选提交和二分查找