15

I have a 'master' branch and several topic branches. Assume that the master branch is used primarily as a release candidate and no development work happens on this branch.

The topic branches are several and are shared by the team. Some of the branches have more than one developer working on them. All topic branches are rebased from the master branch regularly.

To clean up the history in the 'master' branch, I did a 'git merge --squash' when merging code from topic to master branches. This worked perfectly fine.

Now - when topic branches are rebased -- the commits are getting duplicated. Is there a way to clean up the commits on the topic branches after the 'git merge --squash' has been successful?

mustard
  • 715
  • 1
  • 5
  • 18
  • It wont work perfectly fine in the case of [deleted files](http://stackoverflow.com/a/14343784/281545). Maybe not the way to go - see [here](http://stackoverflow.com/questions/1464642/git-merge-squash-repeatedly) – Mr_and_Mrs_D May 08 '13 at 18:51

2 Answers2

18

Lets assume you have the following scenario:

A - B - C (master)
 \
  D - E (topic)

If you merge topic into master with --squash you will get something like

A - B - C - F (master)
 \
  D - E (topic)

Where F contains all changes from D and E. Rebasing topic on master makes no sense since the topic branch is already in master (through F). Instead of rebasing you could move the topic branch to F, e.g.

git checkout master
git branch -f topic F

Which yields:

A - B - C - F (master/topic)

All you need to do now is to push up the moved topic branch:

git push -f origin topic
rtn
  • 127,556
  • 20
  • 111
  • 121
  • Be careful with squash - F may contain files you deleted in your topic branch see http://stackoverflow.com/a/14343784/281545 – Mr_and_Mrs_D May 08 '13 at 18:49
  • 3
    I don't get it. What if I have `D - E - G` on `topic`, where `F` only contains changes from `D - E`? Will the above erase `G`? This is the most common scenario for me - I create a PR `x`, then need changes from that PR in another one `y`, so I branch off `x`, then squash-merge `x` to `master`. How do I reconcile `y` with `master`? – crizzis Aug 18 '22 at 13:09
4

I used to do same thing as Magnus just with couple more commands:

git checkout master
git merge --squash topic
git commit -m "Add topic feature"

git branch -D topic
git checkout -b topic

git push -f origin topic
Wojtek Kruszewski
  • 13,940
  • 6
  • 38
  • 38