How do I rebase a chain of local git branches?
One-line:
git rebase --onto branch1 branch1tmp branch2
That supposes to make a branch1tmp
on branch1
before rebasing branch1
.
git checkout branch1
git branch branch1tmp
git rebase master
git rebase --onto branch1 branch1tmp branch2
That being said, check what ORIG_HEAD
references.
From git rebase man page:
ORIG_HEAD
is set to point at the tip of the branch before the reset.
So check if this would work (and scale better):
git checkout branch1
git rebase master
git rebase --onto branch1 ORIG_HEAD branch2
git rebase --onto branch2 ORIG_HEAD branch3
...
git rebase master branch1
git rebase --onto HEAD ORIG_HEAD branch2
git rebase --onto HEAD ORIG_HEAD branch3
# ...
git rebase --onto HEAD ORIG_HEAD branchN
I have a git alias script to do this:
rebase-chain = "!f() { \
for i in ${@}; do \
git rebase --onto HEAD ORIG_HEAD $i; \
done; \
}; f"
which can make the workflow like:
git rebase master branch1 # start with a normal rebase
git rebase-chain branch2 branch3 branch4 ... branchN
bonus
assuming you have bash-completion
for git
:
__git_complete "git rebase-chain" _git_branch
This question is a subset of Modify base branch and rebase all children at once
I recently started using git-chain which is a tool to solve this problem.
Basically you specify a chain
git chain setup -c myfeature master branch1 branch2
Once this chain is set up, you can pull in commits from master
, then you can run git chain rebase -c myfeature
and the tool figures out all the refs and rebases everything nicely for you.
As an added benefit, it also calculates and handles any amendments or new commits to branch1
and branch2
.