1

I have a feature branch that I've been working on for some time in parallell with master. During this time, after making some changes directly to master, I've merged those changes into my feature branch. I've also merged some small feature into the feature branch, because the small feature is sort of a part of the bigger feature.

Now I want to merge my feature branch onto master, but first I want to squash some of the commits on the feature branch into fewer ones (or a single one), since I have many of the type "Work in progress", "--- WIP", etc. that I don't want to clutter up master.

So currently I have something like this

                        [dev/feature]
                             |              
                        Finally done!
                             |
                             | 
 [dev/smallfeature]          |                             [master]
          |                  |                                |
          |   Merge branch dev/smallfeature into dev/feature  |
          |                 /|                                |
          |----------------/ |                                |
          |                  |                                | 
          |      Merge branch master into dev/feature         |
          |                  |\                               |
          |                  | \------------------------------|
Made the small feature work  |                                |
          |                  |                         Fixed some typos
          |                  |                                | 
          |                  |                                |
          |             Work in progress...                   |
          |                  |                                |
          |      Merge branch master into dev/feature         |
          |                  |\                               |
          |                  | \------------------------------|
          |                  |                                |
   Experimenting...          |                      Released version 1.2.3
          |                  |                                |
          |             Working on it...                      |
          |                  |                                |
          |                  |                                |
          |                  |                                |
          |                   \-------------------------------|
          |                                                   |
          |                                                   |
          |                                            Fixed some colors 
          |                                                   |
          |                                                   |
           \--------------------------------------------------|
                                                              |

And I want to end up with something like this

      [master]
         |
         |
    Feature added
         |
         |
  Fixed some typos
         |
         |
Released version 1.2.3
         |
         |
  Fixed some colors
         |
         |

I'm using IntelliJ, and when I go to interactively rebase from the Working on it...-commit, I can see the commits that were made directly on master in the dialog as well. Will I screw something up if I select "squash" on those as well? They are already on master, so what will happen if I squash them and then merge my feature branch onto master? Is this even possible?

Magnus W
  • 11,902
  • 10
  • 61
  • 135

1 Answers1

1

In order to achieve the result you're describing, you can simply do a squash merge:

git switch master
git merge --squash dev/feature
git commit -m "Feature added"

Git will combine the files in dev/feature with the ones in master and construct a new tree on top of master. Since you've already merged master into dev/feature, the resulting tree will end up containing only the changes introduced in dev/feature.

Enrico Campidoglio
  • 47,702
  • 11
  • 112
  • 146
  • I like this, it seems safer than messing around with rebase and produces exactly the result I was looking for. For others who end up here, I also found [this answer](https://stackoverflow.com/a/43551395/930640) useful for understanding the difference between a "squash merge" and rebase. – Magnus W Sep 04 '20 at 12:45