git merge with --no-ff and --squash

2019-03-28 18:03发布

问题:

I am using the git flow way of managing branches in my repo, as described in: http://nvie.com/posts/a-successful-git-branching-model/

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 lumped together (or squashed) into a single diff 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 stems from how "squashing" and "preserving history" are orthogonal requirements - see Squashing all of my commits (including merges) into one commit without altering history

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

回答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