解决合并冲突后如何使用默认的 git commit 消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13961847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:07:18  来源:igfitidea点击:

How to use the default git commit message after resolving merge conflicts?

gitmergemessagecommitconflict

提问by yoyodyn

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.

在进行合并并解决冲突后,是否有一种“简单”的方法可以只接受来自命令行的默认生成的提交消息?我们的一名开发人员将解决所有冲突,然后执行git commit -m"Merge Commit"替换列出所有冲突文件的生成提交消息。我想要一个不同的标志,它只会在不修改的情况下获取当前文件。我知道有一个 -F 或 --file= 选项,但这需要一直知道文件名。

Thank you

谢谢

采纳答案by Andy Ross

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.

显然,这里的“正确”答案是让您的开发人员在生成合并提交时为您的团队遵循正确的做法。请注意,您想要的行为曾经是默认行为,并且直到最近 git 才开始要求“人工生成”的合并提交消息。这是有原因的,并不是让开发人员用无意义的消息来缩短流程。

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.

也就是说,合并提交是 的输出git fmt-merge-msg,您必须将合并提交的父项提供给它。

回答by gMale

Per the docs, I just tried this simple command and it worked for me:

根据文档,我只是尝试了这个简单的命令,它对我有用:

git commit --no-edit

Afterward, run git logto confirm that the default message has been used.

之后,运行git log以确认已使用默认消息。

回答by LopSae

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 commitwill feed this saved message to the default editor.

默认情况下,当合并失败时,要使用的提交消息保存在 git 文件夹中的文件中,通常是.git/MERGE_MSG. 冲突解决后,运行git commit会将此保存的消息提供给默认编辑器。

If the message is not being picked up on its own it could be feed to the git command using the --fileoption, which reads the commit message from a file:

如果消息没有被自己提取,它可以使用该--file选项提供给 git 命令,该选项从文件中读取提交消息:

git commit --file .git/MERGE_MSG

回答by Chronial

Just set the editor to a command that does nothing:

只需将编辑器设置为一个什么都不做的命令:

GIT_EDITOR=true git commit

回答by Lyubomyr Shaydariv

git commit --file .git/MERGE_MSGas already mentioned is fine but it ignores a few points:

git commit --file .git/MERGE_MSG正如已经提到的那样很好,但它忽略了几点:

  • The current directory is not the top-most directory
  • The current repository is a Git submodule thus having no the .gitdirectory but a file .git.
  • 当前目录不是最顶层目录
  • 当前存储库是一个 Git 子模块,因此没有.git目录而是一个文件.git

And optionally:

并且可选:

  • MERGE_MSGcontains some information about files with conflicts.
  • MERGE_MSG包含有关冲突文件的一些信息。

The first two points can be used with git rev-parse:

前两点可以用于git rev-parse

git commit -F "$(git rev-parse --git-dir)/MERGE_MSG"

Or, alternatively, with a Git alias:

或者,使用 Git 别名:

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:

这适用于“普通”存储库和子模块。如果#应该丢弃 -marked 冲突标记,为简单起见,只能采用合并消息的第一行:

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 -Fkey in aliases because I couldn't make Git emit quotes to be processed in the generated commands with bash (otherwise git commitwould complain for partial commits during merges).

我不得不-F在别名中使用密钥,因为我无法让 Git 在生成的命令中使用 bash 发出要处理的引号(否则git commit会在合并期间抱怨部分提交)。



Git 2.12.0 that was releasedtwo days ago introduces git merge --continueto 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.

两天前发布的Git 2.12.0引入git merge --continue了在合并期间因冲突而停止的合并提交。它也适用于子模块,但--no-edit至少目前不接受,因此建议编辑器在结束合并之前更改提交消息。

回答by Alexander Bird

If you really want to enforce this rule, then there might be a way to force this using git hooks.

如果您真的想强制执行此规则,那么可能有一种方法可以使用 git hooks 来强制执行此规则。

Every time there is a merge conflict, then a 'combined diff' will show you which files were conflicted: git diff HEAD HEAD^1 HEAD^2 --name-only. I do not know if technically it's possible for a combined diff to show morefiles than just conflicted ones.

每次发生合并冲突时,“组合差异”都会显示哪些文件发生冲突:git diff HEAD HEAD^1 HEAD^2 --name-only. 我不知道从技术上讲,合并的差异是否有可能显示更多的文件而不仅仅是冲突的文件。

But, assuming it works like we want (which is an assumption), then you can have a git commit-msghook which checks the message that the user typed inand assert

但是,假设它像我们想要的那样工作(这是一个假设),那么你可以有一个 git commit-msghook 来检查用户输入的消息并断言

  1. is this a merge commit?
  2. if so, does combined diff show files?
  3. if so, does the string "Conflicts:" followed by those file names?
  1. 这是合并提交吗?
  2. 如果是这样,合并的差异是否显示文件?
  3. 如果是这样,字符串“冲突:”后跟那些文件名吗?

If those conditions fail, then have the script print to screen an explanation of what's wrong, and then return non-zero to abort the commit. You can have developers install this commit hook, or you can also install it on the server to enforce it for sure.

如果这些条件失败,则让脚本打印以筛选出问题的解释,然后返回非零值以中止提交。您可以让开发人员安装此提交挂钩,或者您也可以将其安装在服务器上以确保强制执行。