git merge with --no-ff and --squash
Asked Answered
S

1

6

I am using the Git Flow way of managing branches in my repository.

Thus the sequence of commands I should use would be as follows:

git checkout mybranch
git pull --rebase origin develop
git checkout develop
git merge --no-ff mybranch

However, there is one thing that I would like to do differently, in some cases:

I would like to preserve all of my commits on my feature branch (mybranch), but have them squashed into a single commit when merging into develop.

So this is what I think the sequence of commands should be:

git checkout mybranch
git pull --rebase origin develop
git checkout develop
git merge --no-ff --squash mybranch

Would I be doing things wrong if I were to combine --no-ff with --squash?

I am hesitant to try this out because "squashing" and "preserving history" are orthogonal requirements (see this answer).

My rationale is that I want to preserve history on one branch (mybranch) and squash on another branch (develop) --> because these actions are performed in separate branches, this is OK.

Shelah answered 30/5, 2013 at 2:15 Comment(2)
Why don't you just try it. You can always do a hard reset if it doesn't work. Just make a temporary branch at the old spot. Go on. Try it. You know you want to...Onstage
idownvotedbecau.se/noattemptJankey
C
1

I tried combining --squash and --no-ff together and got:

fatal: You cannot combine --squash with --no-ff.

A git merge basically is a squash. After resolving conflicts, it will appear as one commit on develop. The diff of the merge revision is the result of conflict resolution. And yes the changes on mybranch are preserved. Just do "gitk mybranch &" after the merge to verify.

Besides The Shadow is correct. Do not be afraid. Create a new branch, play around and see what the results are.

The damage was done in your "git pull --rebase origin develop" command.

Answer (assumes you have local develop branch based on origin/develop):

git checkout develop
git pull
git merge --no-ff mybranch
Cabbala answered 30/5, 2013 at 4:31 Comment(3)
Who in the world is "The Shadow"? Did you mean "Cupcake"?Unwisdom
My guess is that someone changed their name or deleted an answer since my post.Cabbala
Can't edit the post because "queue is full"... Seems I'm not the first one to try. The sentences "A git merge basically is a squash. After resolving conflicts, it will appear as one commit on develop." are misleading. git merge only squashed with the aptly named --squash option. Otherwise it brings all commits from one branch into the other.Bookbinder

© 2022 - 2024 — McMap. All rights reserved.