How to use the default git commit message after resolving merge conflicts?
Solution 1
Obviously the "right" answer here is to get your developer to follow correct practice for your team when generating merge commits. Note that the behavior you want used to be the default, and that only recently has git begun demanding "human generated" commit messages for merges. That was for a reason, and it wasn't so that developers would short-circuit the process with a meaningless message.
Maybe the developer is generating merge commits when s/he should be rebasing instead?
That said, the merge commit is the output of git fmt-merge-msg
, to which you would have to feed the parents of the merge commit.
Solution 2
Per the docs, I just tried this simple command and it worked for me:
git commit --no-edit
Afterward, run git log
to confirm that the default message has been used.
Solution 3
By default when a merge fails the commit message that was to be used is saved in a file in the git folder, usually .git/MERGE_MSG
. After the conflicts are resolved running git commit
will feed this saved message to the default editor.
If the message is not being picked up on its own it could be feed to the git command using the --file
option, which reads the commit message from a file:
git commit --file .git/MERGE_MSG
Solution 4
Just set the editor to a command that does nothing:
GIT_EDITOR=true git commit
Solution 5
git commit --file .git/MERGE_MSG
as already mentioned is fine but it ignores a few points:
- The current directory is not the top-most directory
- The current repository is a Git submodule thus having no the
.git
directory but a file.git
.
And optionally:
-
MERGE_MSG
contains some information about files with conflicts.
The first two points can be used with git rev-parse
:
git commit -F "$(git rev-parse --git-dir)/MERGE_MSG"
Or, alternatively, with a Git alias:
commit-merge = !cat $(git rev-parse --git-dir)/MERGE_MSG | git commit -F -
This works both in "normal" repositories and submodules. If the #
-marked conflict markers should be discarded, for simplicity, only the first line for the merge message could be taken:
git commit -m $(head -1 $(git rev-parse --git-dir)/MERGE_MSG)
Or another alias:
commit-merge = !head -1 $(git rev-parse --git-dir)/MERGE_MSG | git commit -F -
I had to use the -F
key in aliases because I couldn't make Git emit quotes to be processed in the generated commands with bash (otherwise git commit
would complain for partial commits during merges).
Git 2.12.0 that was released two days ago introduces git merge --continue
to make a merge commit that was stopped on conflicts during the merge. It works fine for submodules as well, but does not accept --no-edit
, at least yet, so an editor is suggested to change the commit message before concluding the merge.
yoyodyn
Updated on July 05, 2022Comments
-
yoyodyn almost 2 years
After doing a merge and resolving conflicts, is there an "easy" way to just accept the default generated commit message from the command line? One of our developers will resolve all the conflicts, and then do a
git commit -m"Merge Commit"
which replaces the generated commit message that listed all the conflict files. I would like to have a different flag that would just take the current file without modification. I know there is a -F or --file= option, but that requires knowing the file name all the time.Thank you
-
newpxsn over 11 yearsI like this one. :) Though I still argue that the question is evidence of a deeper problem in the development process. If the coders are in a situation where they "want" a nonsense merge message, they probably aren't really merging two forks and are just integrating new changes from upstream. That's a rebase.
-
Chronial over 11 yearsOh, I agree that you probably want your coders to think about their merge messages – but that wasn’t the question ;). But also you often need to do trivial merges because you can’t do a rebase as your branch is published.
-
Maic López Sáenz over 11 yearsHow is this command used? Documentation says that it expects the merge objects to be provided by stdin. I tried piping into it, and also providing through a file, the revision numbers of the parents, but it kept throwing back a
fatal: Error in line 1:...
. -
yoyodyn over 11 yearsI am not quite sure how this works. How does changing the editor prevent the git commit -m"Merge Commit" syntax from over writing the default generated commit message? My problem is that I want to keep all the merge conflict files listed in the commit message.
-
Chronial over 11 yearsIt doesn’t – there is no way in git (or most other tools for that case) to stop it from doing what it’s been explicitly told. If you don’t want to set a commit message, don’t use
-m
. My answer will just prevent an editor to pop up. -
Joshua Hoblitt almost 9 yearsThis is extremely useful for scripting "experiments" with git. Eg., gist.github.com/jhoblitt/6bb4305e0b548e8e0de9
-
Lyubomyr Shaydariv about 7 yearsOr with shorter output:
git log -1
. -
amoebe almost 5 yearsThis seems to also work for the
merge
command as ingit merge master --no-edit
. -
bignay2000 about 4 yearsHow exactly would a rebase not also cause conflict, versus a merge?
-
Dhir Pratap over 3 years@randombits it would still require conflict resolution, but would not generate a merge commit.