git-bisect-rebase
A script that attempts to rebase the current branch onto a target branch, and if the rebase fails due to conflicts, uses git bisect to find the latest commit that can be successfully rebased onto.
Overview
When rebasing a long-running feature branch onto a target branch (like main or develop), conflicts can arise that make the rebase difficult or impossible to complete. This script intelligently handles such situations by:
- First attempting a direct rebase
- If that fails, using
git bisectto find the latest commit in your branch that can be successfully rebased onto the target branch without conflicts - Performing the rebase up to that successful point
- Automatically handling uncommitted changes by stashing and restoring them
Once you have found the latest commit that can be rebased cleanly, you can exammine the tree and see what specifically caused the conflict. You can rebase try resolving the conflicts at that point for the one single issue.
Usage
git bisect-rebase <target-branch>Parameters
<target-branch>: The branch to rebase onto (e.g.,main,develop,origin/main)
How It Works
Phase 1: Direct Rebase Attempt
- Stashes any uncommitted changes (if present)
- Attempts to rebase the current branch directly onto the target branch
- If successful, restores stashed changes and exits
Phase 2: Bisect-Assisted Rebase (if direct rebase fails)
- Aborts the failed rebase
- Finds the common ancestor between the current branch and target branch
- Uses
git bisectto binary search for the latest commit that can be rebased onto successfully - Tests each commit by attempting a rebase (using an internal test function)
- Once the optimal commit is found, performs the rebase to that point
Examples
# Rebase current feature branch onto main
git bisect-rebase main
# Rebase onto a remote branch
git bisect-rebase origin/develop
# Rebase onto a specific branch
git bisect-rebase release/v2.0Use Cases
- Long-running feature branches: When your branch has diverged significantly from the main branch
- Complex merge conflicts: When a direct rebase results in too many conflicts to resolve manually
- Incremental rebasing: When you want to rebase as much as possible without getting stuck on conflicts
- Branch maintenance: Keeping feature branches reasonably up-to-date with minimal manual intervention
What Happens During Bisect
The script uses an internal test function that:
- Attempts to rebase the current commit being tested
- Returns "good" (exit 0) if the rebase succeeds
- Returns "bad" (exit 1) if the rebase fails
- Automatically cleans up and restores the original state after each test
Requirements
- Git repository with at least two branches
- Standard Git tools (
git bisect,git rebase,git stash) - Bash shell environment
Important Notes
- Uncommitted changes: The script automatically stashes and restores uncommitted changes
- Bisect cleanup: After completion, run
git bisect resetto clean up the bisect state - Partial success: Even if not all commits can be rebased, you'll get as many as possible
Workflow Example
# Starting scenario: feature branch with 20 commits, conflicts prevent full rebase
git checkout feature/new-api
git bisect-rebase mainYou can then try to rebase onto the first unsuccessful commit found by the bisect:
git rebase bisect-badThis will hopefully present you with a simpler conflict to resolve. Once you have resolved any conflicts, you can try the rebase again from that point.
Cleanup
After the script completes, make sure to clean up:
git bisect reset