Git: Squash feature branch and merge commit, rebase on master

Viewed 50

I have the following scenario:

M
|
|
o  X
| /| 
|/ |
a  b
| /
|/
o

I checked out b, then ran git merge a. There were some conflicts which were resolved in the merge commit X. I also did some misc changes within X.

I want to squash X and b, and then rebase the squash on M. The goal is to avoid re-doing the changes that were done to build X.

(X+b)
  |
  |
  M
  |
  |
  o
  |
  |
  a
  |
  |
  o

Any ideas on how to do this?

2 Answers

You can :

# from X :
git reset --soft o    # <- the parent commit of a and b
git commit            # this will create 'b + X'

# then rebase :
git rebase M

You may have extra conflicts to fix when rebasing.


An alternative way can be :

  • first merge M and X
  • then use reset --soft + commit to create a single commit
# from commi M :
git merge X
# fix possible merge conflicts

git reset --soft M
git commit    # commit the content of the merge
              # as a single regular commit on top of M

Alternative method:

git checkout M
git merge --squash X
git commit -m "implemented feature X"
Related