Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to rebase multiple branches in a single lineage

I have a two branches that I keep on top of upstream/master. One branch has the other as an ancestor so they form a line.

U1 (upstream/master)
  \
   A -- B (fixes)
         \
          C -- D (features)

Later, upstream/master moves forward...

U1 -- U2 (upstream/master)
  \
   A -- B (fixes)
         \
          C -- D (features)

... and I want to rebase both branches on top.

U1 -- U2 (upstream/master)
        \
         A' -- B' (fixes)
                 \
                  C' -- D' (features)

I know two ways to do it, each with drawbacks.

git rebase upstream/master fixes
git rebase fixes features

These two commands sometimes work and sometimes give me merge conflicts on the second command.

git rebase upstream/master features
# figure out the hash code of the new commit corresponding with B'
git branch -f fixes <sha of B'>

This only involves a single rebase, but moving the branches is tedious and error prone.

I looked into rebase --preserve-merges, but that doesn't seem applicable because there are no merge commits.

Is there a better way to accomplish the rebase?

like image 457
Craig P. Motlin Avatar asked Feb 25 '16 05:02

Craig P. Motlin


People also ask

How to rebase a branch in Git?

Here are the steps to follow while rebasing a branch: You should receive the latest changes from a remote git repository. Thus the first step is running git fetch: The second step is running git rebase. Rebase is a Git command which is used to integrate changes from one branch into another.

Can I REBASE the layout-tweaks branch onto the main branch?

I tried to rebase the layout-tweaks branch onto the latest version of the main branch to get it up-to-date with all the latest changes. If the changes between main and your feature branch are unrelated git happily rebases your feature branch for you.

What happens when you REBASE to the master branch?

If you use the rebase command onto master branch, you will update feature A branch base commit. In other words, you would get all of the latest changes from master branch in feature A branch as if those commits happened first prior to you making commits to feature A branch, making sure feature A branch is up to date with master branch.

Should you REBASE first or merge changes?

With fewer commits and less potential conflicts we can then rebase on the changes in the main branch. Normally, this strategy is a nice way to save yourself a headache by squashing first and rebasing on new changes after. Merge commits, oh no…


2 Answers

One way to avoid the merge conflicts is to move the branches from the pre-rebase history to the post rebase one. This is admittedly terrible because it requires figuring out the new version of each commit, but might be faster than merging unnecessarily.

eg.

git rebase upstream/master features

U1 -- U2 (upstream/master)
        \
         A' -- B' 
                 \
                  C' -- D' (features)

git branch -f fixes (SHA for B') # Change where the branch is pointing.

U1 -- U2 (upstream/master)
        \
         A' -- B' (fixes)
                 \
                  C' -- D' (features)
like image 56
mfarrugi Avatar answered Oct 17 '22 14:10

mfarrugi


As of git 2.38, we can now rebase with the --update-refs flag.

To turn this on globally:

git config --global rebase.updateRefs true
like image 1
Craig P. Motlin Avatar answered Oct 17 '22 14:10

Craig P. Motlin