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?
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.
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.
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.
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…
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With