git-chain
A powerful tool for managing and rebasing chains of dependent Git branches (stacked branches).
What Problem Does Git Chain Solve?
When working on complex features, developers often create a series of branches where each branch builds upon the previous one. For example:
I---J---K feature-2
/
E---F---G feature-1
/
A---B---C---D master
When new changes are added to the master branch, updating all branches in the chain becomes tedious and error-prone:
- You need to rebase
feature-1 onto the updated master
- Then rebase
feature-2 onto the updated feature-1
- Repeat for any additional branches in the chain
Git Chain automates this entire process. It keeps track of relationships between branches and handles the rebasing for you.
Key Concepts
- Chain: A sequence of branches that build upon each other, with a designated root branch.
- Root Branch: The foundation branch (typically
main or master) that the chain ultimately merges into.
- Branch Order: The sequence in which branches depend on each other in the chain.
Note:
- A branch can belong to at most one chain.
- The root branch is not part of the chain, but serves as its foundation.
How Git Chain Works
Git Chain stores branch relationships in your repository's Git config, tracking:
- Which chain a branch belongs to
- The order of branches within a chain
- Each branch's root branch
Git Chain offers two strategies for updating branches:
- Rebase: Rewrites branch history by replaying commits on top of the updated parent branch
- Merge: Preserves branch history by creating merge commits that incorporate changes from the parent branch
When operating on chains, Git Chain:
- Determines the correct fork-point for each branch using
git merge-base --fork-point
- Updates each branch in sequence, preserving the dependency order
- Handles edge cases like squash merges and chain reorganization
Rebase Strategy: How git-chain Updates Your Branches
Basic Concept
When you run git chain rebase, git-chain intelligently updates each branch in your chain to incorporate changes from its parent branch. Think of it like moving your work to sit on top of the latest version of your parent branch. This rewrites commit history, giving a cleaner, linear history but generating new commit hashes.
How It Works
Order Matters: Branches are updated in the order they appear in the chain, starting from the one closest to the root branch. This ensures each branch builds upon an already-updated parent.
Finding the Right Starting Point: For each branch, git-chain determines where your branch originally split from its parent. This point (called a "fork-point") is crucial for keeping only your changes when rebasing.
What is a fork-point? A fork-point is the specific commit where you originally created your branch from its parent. It's more intelligent than just finding a common ancestor - Git uses its reflog (a history of where branch tips have been) to determine the exact point where your branch's history forked from the parent branch. This is especially useful when the parent branch has been rebased or reorganized since you created your branch. When rebasing, Git needs to know this point to correctly identify which commits belong to your branch (and should be moved) versus which commits were already in the parent branch (and should be left alone).
Smart Detection: git-chain uses Git's sophisticated "fork-point" detection, which is smarter than simple ancestry checking. It:
- First checks if your branch can be simply fast-forwarded
- If not, uses Git's history records (reflog) to find the original branching point
- Falls back to a regular merge-base if fork-point detection fails
Note on the Fallback Mechanism: Sometimes Git can't determine the fork-point, particularly in these situations:
- When older reflog entries have been cleaned up by
git gc
- If you created your branch from an older commit (not the tip) of the parent branch
- After certain operations that affect repository history
When Git's fork-point detection fails, git-chain automatically falls back to using git merge-base, which finds the most recent common ancestor between two branches. While this ensures rebasing can proceed, it might be less precise than using the true fork-point.
Handling Squash Merges: If you've squash-merged a branch into its parent (combining all commits into one), git-chain detects this and automatically creates a backup branch before resetting the branch to its parent. You can control this behavior with the --squashed-merge flag (see Command Options below).
The Actual Rebasing: For each branch, git-chain runs a command similar to:
git rebase --keep-empty --onto <parent_branch> <fork_point> <branch>
This moves your changes to sit on top of the updated parent branch.
To read more about fork-point, see: https://git-scm.com/docs/git-merge-base#_discussion_on_fork_point_mode
Command Options and Flags
Git Chain's rebase command offers customization through its flags:
--step, -s: Rebase one branch at a time, requiring manual confirmation between steps
git chain rebase --step
Perfect for carefully managing complex rebases where conflicts might occur.
--ignore-root, -i: Skip rebasing the first branch onto the root branch
git chain rebase --ignore-root
Useful when you want to update relationships between chain branches without incorporating root branch changes.
--continue: Resume a chain rebase after resolving conflicts
git chain rebase --continue
After resolving a rebase conflict and completing the git-level rebase (git rebase --continue), use this to continue rebasing the remaining branches in the chain. Uses saved merge bases from the original run.
--skip: Skip the current conflicted branch and continue with the rest of the chain
git chain rebase --skip
When a rebase conflict occurs and you don't want to resolve it, use --skip to restore the conflicted branch to its original position and continue rebasing the remaining branches. This aborts any in-progress git rebase automatically.
--abort: Abort a chain rebase and restore all branches
git chain rebase --abort
Rolls back the entire chain rebase by restoring all branches to their original positions before the rebase started. Aborts any in-progress git rebase and cleans up the state file.
--status: Show the current chain rebase state
git chain rebase --status
Displays the status of each branch in an ongoing chain rebase, including which branches have been completed, skipped, or are still pending. Reports "No chain rebase in progress" when no state file exists.
--quit: Discard the chain rebase state without touching any branch
git chain rebase --quit
Removes the state file and leaves every branch exactly where it is. This is the escape hatch for a state file the other recovery commands cannot read โ they all parse it first, so a corrupt or unrecognised one stops --continue, --skip, --abort and --status alike. Unlike --abort, nothing is restored; use --abort when you want the chain rewound.
--no-fork-point: Compute merge bases without consulting reflogs
git chain rebase --no-fork-point
By default git-chain uses git's fork-point calculation, which reads the parent branch's reflog, to decide where each branch's own commits begin. Use this flag when that reflog is missing or misleading โ after a fresh clone, or once it has been expired or rewritten โ to fall back to a plain merge-base.
--cleanup-backups: Delete backup branches after successful rebase
git chain rebase --cleanup-backups
After a successful rebase, automatically deletes any backup-<chain>/<branch> branches that were created during squash-merge reset. Can also be combined with --continue and --skip.
--squashed-merge=<mode>: How to handle branches detected as squash-merged
git chain rebase --squashed-merge=reset # Default: auto-backup + reset to parent
git chain rebase --squashed-merge=skip # Skip the squash-merged branch
git chain rebase --squashed-merge=rebase # Force normal rebase despite detection
When git-chain detects that a branch has been squash-merged into its parent:
reset (default): Creates a backup branch (backup-<chain>/<branch>) before resetting the branch to match its parent with git reset --hard. This is the safest option โ your original commits are preserved in the backup branch.
skip: Leaves the branch untouched and continues with the next branch in the chain.
rebase: Ignores the squash-merge detection and performs a normal rebase. This may cause conflicts if the branch has multiple commits.
Examples (git chain rebase)
Here are some common scenarios and how to handle them with git-chain rebase:
1. Standard chain update
Scenario: You want to update all branches in the chain to incorporate changes from their parent branches.
Solution:
git chain rebase
This rebases all branches in the chain sequentially, starting from the one closest to the root branch.
2. Updating just the relationship between chain branches
Scenario: You want to update only relationships between branches in a chain, not incorporating new root branch changes.
Solution:
git chain rebase --ignore-root
This skips rebasing the first branch onto the root branch.
3. Handling squash-merged branches
Scenario: A branch in your chain was squash-merged into its parent, and you want to skip it during rebase.
Solution:
git chain rebase --squashed-merge=skip
This skips any branches detected as squash-merged, leaving them untouched while rebasing the rest of the chain.
4. Recovering from rebase conflicts
Scenario: A rebase conflict occurred and you need to resolve it and continue.
Solution:
# After resolving the conflict in the git-level rebase:
git add <resolved-files>
git rebase --continue
# Then resume the chain rebase for remaining branches:
git chain rebase --continue
Or if you want to skip the conflicted branch and continue with the rest:
git chain rebase --skip
Or if you want to abort the entire chain rebase and restore all branches:
git chain rebase --abort
5. Careful rebasing with potential conflicts
Scenario: You anticipate conflicts and want to handle each branch separately.
Solution:
git chain rebase --step
This rebases one branch at a time, waiting for your confirmation between steps.
Handling Rebase Conflicts
When rebasing branches in a chain, conflicts can sometimes occur. Git Chain saves the chain rebase state to .git/chain-rebase-state.json, enabling proper recovery via --continue and --abort.
Conflict Detection: When a rebase conflict occurs, git-chain:
- Pauses the rebasing process at the conflicted commit
- Saves the chain rebase state (original branch refs, merge bases, per-branch status)
- Leaves the repository in a conflicted state for you to resolve
- Provides numbered recovery steps with
--continue, --skip, and --abort instructions
Resolution Process:
- The conflicted files will be marked with conflict markers (
<<<<<<<, =======, >>>>>>>)
- Resolve conflicts manually by editing the conflicted files
- Add the resolved files with
git add <file>
- Continue the git-level rebase with
git rebase --continue
Continuing After Resolution:
- After resolving the conflicts and completing the git-level rebase, resume the chain rebase:
git chain rebase --continue
- Git Chain loads the saved state and continues rebasing the remaining branches using pre-computed merge bases
- After all branches are rebased, the state file is cleaned up and you are returned to your original branch
Skipping a Problematic Branch:
- If you don't want to resolve the conflict for a particular branch, skip it and continue:
git chain rebase --skip
- This aborts the in-progress git rebase, restores the conflicted branch to its original position, and continues rebasing the remaining branches
Aborting the Entire Chain Rebase:
- If you decide to cancel the entire chain rebase and restore all branches:
git chain rebase --abort
- This restores all branches to their original positions, aborts any in-progress git rebase, and cleans up the state file
External Abort Detection:
- If you run
git rebase --abort directly (bypassing git-chain), git-chain detects this when you run --continue
- It compares the branch's current commit with the saved original ref to determine if the rebase was aborted externally
- You'll be prompted to use
--skip to skip the branch or --abort to cancel the entire chain rebase