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)?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With