6

After invoking git merge --no-commit <commit>, performing a commit will result in a merge commit with two (or more) parents. What command to invoke to create a simple commit instead (without having to re-perform the merge command with the --squash option)?

mklhmnn
  • 3,930
  • 2
  • 14
  • 8

3 Answers3

4

According to the git-merge man page, the --squash option does not record $GIT_DIR/MERGE_HEAD. $GIT_DIR/MERGE_HEAD is responsible for creating merge commits; you can see this in the Git sources, file builtin/commit.c:

in_merge = file_exists(git_path("MERGE_HEAD"));
...
if (in_merge) {
... // Perform merge_commit
}

Solution: after having performed a normal merge, simply get rid of $GIT_DIR/MERGE_HEAD to avoid getting a merge commit. You may manually clean up $GIT_DIR/MERGE_MSG and $GIT_DIR/MERGE_MODE as well or leave this task up to Git upon successful commit.

mstrap
  • 15,236
  • 6
  • 50
  • 77
1

The best solution without any hackery in the .git directory is to use stashing and resetting.

>git merge --no-commit $otherbranch
>git stash
>git reset HEAD
>git stash pop
>git commit -a

This should be trivial to automatize if you need this more often.

Note that this seems to work without the stash part at first sight, but fails when files are added, so the extra trip through the stash cannot be left out.

LiKao
  • 10,010
  • 5
  • 47
  • 85
0

You could try a git merge --squash.

See the question "In git, what is the difference between merge --squash and rebase?"

from:

      X                   stable
     /                   
a---b---c---d---e---f---g dev

to:

      X-------------------G stable
     /                   
a---b---c---d---e---f---g dev

It will produced a squashed commit on the destination branch, without marking any merge relationship.

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • Thanks. I know about the squash-option (see the text in braces), but how to change my Index/Working Tree state *after* I have performed git-merge without the squash option to not create a merge commit? – mklhmnn Jun 15 '10 at 13:11