Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: after preparing a real merge commit, how to create a simple commit?

Tags:

git

git-merge

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)?

like image 202
mklhmnn Avatar asked Jun 14 '10 15:06

mklhmnn


People also ask

Does a merge create a new commit?

Merge branchesGit creates a new commit (M) that is referred to as a merge commit that results from combining the changes from your feature branch and master from the point where the two branches diverged.

Do I need to commit after merge?

Merge is just like any other DML and will require a commit or rollback as any other DML statement at the end of the transaction.

Does git merge always create commit?

This command merges the specified branch into the current branch, but always generates a merge commit (even if it was a fast-forward merge). This is useful for documenting all merges that occur in your repository.


2 Answers

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.

like image 162
mstrap Avatar answered Oct 31 '22 12:10

mstrap


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.

like image 24
LiKao Avatar answered Oct 31 '22 14:10

LiKao