Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Branches out of sync after squash merge

Tags:

git

merge

squash

I have a develop branch with multiple commits. This branch shall be merged into the master branch. I also would like the master branches commit history to be as clean as possible, so I want to only have one entry for the merge. Thus I do the following:

git merge --squash develop
git commit

Now all changes from develop are in master, and there is only one commit on master. So far so good :-)

Now the problem is, that master and develop seem to be out of sync. I.e. if I make another commit to develop, I cannot merge that into master anymore. Lots of conflicts are popping up.

How exactly is the squash merge supposed to be done, so that both branches are in sync afterwards?

like image 842
Boris Avatar asked Jul 16 '20 09:07

Boris


2 Answers

1) The "typical" workflow your squash strategy is meant to avoid

# Initial situation

A---B---C <<< master
         \
          D---E---F <<< develop

# then the merge is done into master

A---B---C-----------G <<< master
         \         /
          D---E---F <<< develop

# no broken history but now master has three (probably noisy) commits


2) Your described (and unsatisfactory) workflow

# Initial situation

A---B---C <<< master
         \
          D---E---F <<< develop

# then you squash D, E and F into commit G on master

A---B---C---G <<< master
         \
          D---E---F <<< develop

# as you noticed, at this point master's history is "clean" but develop is out of sync


3) The alternative workflow I suggest to you

(git reset --soft <commit> is moving HEAD to point elsewhere but keeps your changes in the working tree, ready to be committed (or not)) (doc)

# Initial situation

A---B---C <<< master
         \
          D---E---F <<< develop

# squash your commits D, E and F on develop itself, THEN bring the new commit on master

git checkout develop
git reset --soft master
git commit -m "This is my message for these three commits"

A---B---C <<< master
         \
          G <<< develop

git checkout master
git merge develop

A---B---C---H <<< master
         \ /
          G <<< develop

# now histories are kept related but you have only one (properly remessaged and squashed) commit

like image 70
Romain Valeri Avatar answered Nov 15 '22 19:11

Romain Valeri


My suggestion is to merge the master branch in the development branch after the merge --squash commit.

So the workflow looks like this:

git checkout master 
git merge --squash develop
git commit

git checkout develop
git merge master
like image 3
learning2code Avatar answered Nov 15 '22 17:11

learning2code