Search code examples
gitgit-mergesquash

Why can't I use git merge --squash with --no-ff?


I'm trying to use the git merge --squash with the --no-ff parameter, but git does not allow. Someone have any sugestion to help me?

I can't use fast forward merge and I need to use the --squash parameter to group a lot of commits that were made in another branch.

Thanks!


Solution

  • It probably doesn't let you because such a command wouldn't make sense.

    The documentation for --squash says (emphasis mine):

    --squash
    Produce the working tree and index state as if a real merge happened (except for the merge information), but do not actually make a commit or move the HEAD, nor record GIT_DIR/MERGE_HEAD to cause the next git commit command to create a merge commit. This allows you to create a single commit on top of the current branch whose effect is the same as merging another branch (or more in case of an octopus).

    The --no-ff flag does:

    Create a merge commit even when the merge resolves as a fast-forward.

    You are essentially asking git to make a commit and NOT make a commit at the same time.

    If you want to preserve all of the history of your branch, you should use the --no-ff flag. Commit d is a merge commit that has two parents, a and c.

    a ------ d -- ....
     \      /
      b -- c  
    

    If you want all of the commits on that branch to be treated as a single unit of work, then use --squash. Commit d is a regular commit that contains all of the changes from commits b and c but has only one parent, a.

    a ---- d -- ....
     \      
      b -- c