0

I have two branches, master and feature. I want to merge feature into master, but I have multiple changes in the feature branch with messy commits. I don't want to squash these messy commits in the feature branch but I want to squash them when I merged the feature branch into master. One way is using the following command in master:

git merge --squash feature

But the above command will squash all commits and will create a single commit which is not desired. I want to squash some commits, not all of them. For example, I want to squash 3 out of 10, but only while merging into master.

What should I do?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Ali
  • 9
  • 2
  • Create a new branch as a "copy" of `feature`, then do this: [How do I squash specific commits on a local branch?](https://stackoverflow.com/questions/24310554/how-do-i-squash-specific-commits-on-a-local-branch), then merge the new branch (leaving `feature` alone). – mkrieger1 Jul 14 '22 at 14:39
  • Take a look at the Squashing Commits part, https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History. – ElpieKay Jul 14 '22 at 14:41

1 Answers1

0

Here's one option:

  1. git checkout -b feature_merge feature. This effectively makes a temporary copy of the feature branch that you can use for squashing while leaving feature intact.
  2. git rebase -i $(git merge-base HEAD master)
  3. Squash the commits you want to squash
  4. git checkout master && git merge feature_merge
  5. git branch -d feature_merge
0x5453
  • 12,753
  • 1
  • 32
  • 61