Background
I'm trying to write a script that will simplify my backport process using git.
The general process for fixing a bug in the current version goes something like this:
- Branch from
master
to a bug fix branch, e.g.bugfix/abc
- Make all the commits to fix the bug on
bugfix/abc
- Merge
bugfix/abc
tomaster
(without fast-forward merges)
Now when I want to backport the fix to version 1 for example (branch v1
) I do:
- Create a new branch from
bugfix/abc
, e.g.bugfix/def
- Then manually find the commit on
master
that was before the merge commit, e.g.f4d399238f
- Now I use rebase:
$ git rebase --onto v1 f4d399238f bugfix/def
This works great (after working out that I had to use the commit before the merge for the upstream).
Question
How do I find the common ancestor of two branches before a merge commit? (for step 2 of the backport process).
Tried
git merge-base bugfix/abc master
- Since the merge has already been done this just returns the commit at the head of
bugfix/abc
- Since the merge has already been done this just returns the commit at the head of
- Combining the result of #1 to get the child of this commit using
git log
- I tried following How do I find the next commit in git? using various combinations of
--reverse
,--ancestry-path
and--children
but I never got what I expected.
- I tried following How do I find the next commit in git? using various combinations of
Update
The key difference between this question and Find common ancestor of two branches is that the two branches have already been merged. As I mentioned the best commit is returned as the head of the branch that was merged. I need the common ancestor before the merge commit.
Assume that the branches after the bug has been fixed and merged into master look like:
A---B v1
\
C---D---F---H master
\ /
E---G bugfix/abc
Running $ git merge-base master bugfix/abc
will return G but I need to get D (or even F would do for the purpose of using rebase --onto
).
Once I get D I would then run:
$ git branch bugfix/def bugfix/abc
$ git rebase --onto v1 D bugfix/def
$ git checkout v1
$ git merge bugfix/def
To end up with the desired result of:
E'---G' bugfix/def
/ \
A---B--------I v1
\
C---D---F---H master
\ /
E---G bugfix/abc