Git:如何编辑/重写合并提交的消息?
如何编辑或重写合并提交的消息?
如果 git commit --amend
是最后一次提交 (HEAD
),则有效,但如果它出现在 HEAD
之前怎么办?
git rebase -i HEAD~5
不列出合并提交。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果将
--preserve-merges
选项(或其同义词-p
)添加到git rebase -i
命令,那么 git 会尝试在变基时保留合并,而不是线性化历史记录,并且您也应该能够修改合并提交:注意。从 git v2.22 开始,
--perserve-merges
已被弃用,取而代之的是--rebase-merges
(https://www.infoq.com/news/2019/07/git-2-22-rebase-merges/)。If you add the
--preserve-merges
option (or its synonym,-p
) to thegit rebase -i
command then git will try to preserve the merges when rebasing, rather than linearizing the history, and you should be able to amend the merge commits as well:Note.
--perserve-merges
has been deprecated in favour of--rebase-merges
as of git v2.22 (https://www.infoq.com/news/2019/07/git-2-22-rebase-merges/).请注意,启动 git1.7.9.6 (和 git1.7.10+),
git merge
本身总是会触发编辑器,以便您向合并添加详细信息。它还引入了一个环境变量
GIT_MERGE_AUTOEDIT
来帮助旧脚本拒绝这种行为。请参阅“预期 Git 1.7.10”:
莱纳斯说:
请注意,在 Git 2.17(2018 年第 2 季度)之前,“
git rebase -p
”会损坏合并提交的日志消息,现已修复。请参阅 commit ed5144d(2018 年 2 月 8 日),作者:格雷戈里·埃雷罗(``)。
建议者:Vegard Nossum (
vegard
) 和 昆汀·卡萨诺瓦斯 (casasnovas
)。(由 Junio C Hamano --
gitster
-- 合并于 提交8b49408,2018 年 2 月 27 日)。使用 Git 2.23(2019 年第 2 季度),“< code>merge -c” 期间的指令
git rebase --rebase-merges
" 应该让用户有机会编辑日志消息,即使不需要创建新的合并和替换现有的一个(即快进),但没有。
已更正。
请参阅 提交 6df8df0(2019 年 5 月 2 日),作者:菲利普·伍德(
菲利普伍德
)。(由 Junio C Hamano --
gitster
-- 合并于 提交c510261,2019 年 6 月 13 日)Note that, starting git1.7.9.6 (and git1.7.10+),
git merge
itself will always trigger the editor, for you to add details to a merge.It also introduces an environment variable
GIT_MERGE_AUTOEDIT
to help older scripts decline this behavior.See "Anticipating Git 1.7.10":
Linus said:
Note that, before Git 2.17 (Q2 2018), "
git rebase -p
" mangled log messages of a merge commit, which is now fixed.See commit ed5144d (08 Feb 2018) by Gregory Herrero (``).
Suggested-by: Vegard Nossum (
vegard
), and Quentin Casasnovas (casasnovas
).(Merged by Junio C Hamano --
gitster
-- in commit 8b49408, 27 Feb 2018)With Git 2.23 (Q2 2019), A "
merge -c
" instruction during "git rebase --rebase-merges
" should give the user a chance to edit the log message, even when there is otherwise no need to create a new merge and replace the existingone (i.e. fast-forward instead), but did not.
Which has been corrected.
See commit 6df8df0 (02 May 2019) by Phillip Wood (
phillipwood
).(Merged by Junio C Hamano --
gitster
-- in commit c510261, 13 Jun 2019)仅使用原始命令的另一个很好的答案 - 作者:knittl https://stackoverflow.com/a/7599522/94687:
或更好(更正确)的最终 rebase 命令:
顺便说一句,使用原始命令可能有一个很好的“功能”,即不消耗太多 CPU 并让您等待未知的时间,直到 Git 完成对提交列表的思考在 git rebase -p -i HEAD^^^^ 的情况下需要重新设置基础(这样的命令将导致仅包含 4 个最后提交的列表,在我的情况下合并为最后一个)就我而言,大约需要 50 秒!)。
Another nice answer using only primitive commands -- by knittl https://stackoverflow.com/a/7599522/94687:
or a better (more correct) final rebase command:
BTW, using the primitive commands might have the nice "feature" of not consuming too much CPU and making you wait unknown time until Git finishes thinking about the list of commits needing to be rebased in the case of
git rebase -p -i HEAD^^^^
(such a command which would result in a list of only 4 last commits with the merge as last one in my case in my case took around 50 secs!).对于当前的 Git 版本(2020+),只需执行 git rebase -i -r,
然后在编辑器中将
merge -C
替换为merge -c
。这将在变基期间在编辑器中打开合并提交的消息,您可以在其中更改它(感谢 VonC 的 提示)。For current Git versions (2020+), just do
git rebase -i -r <parent>
,then replace in the editor
merge -C
withmerge -c
. This will open the merge commit's message in the editor during rebasing, where you can change it (thanks to VonC for the hint).git merge --edit
即使在非交互式合并的情况下也允许您给出评论。
git merge --edit --no-ff
如果您遵循 git flow 并在开发分支上重新建立基础并在没有快进的情况下合并到其中,这会很有用。
git merge --edit
Allows you to give the comment even in case of non-interactive merge.
git merge --edit --no-ff
can be useful if you follow git flow with rebasing on development branch and merging into it with no fast forward.
从 2021 年开始更新,
-p
已弃用。使用
--rebase-merges
代替。Update from 2021,
-p
is deprecated.Use
--rebase-merges
instead.使用
--rebase-merges
(或缩短的-r
)标志:然后将提交旁边的“pick”文本更改为“edit”或“reword”更改:
--rebase-merges
(或缩短的-r
)标志取代了已弃用的--preserve-merges
(或缩写的-p
)文档:https://git-scm.com/docs/git-rebase#Documentation/git-rebase.txt--r
Use the
--rebase-merges
(or the shortened-r
) flag:Then change the 'pick' text to 'edit' or 'reword' next to the commit to change:
The
--rebase-merges
(or the shortened-r
) flag replaces the deprecated--preserve-merges
(or the shortened-p
)Documentation: https://git-scm.com/docs/git-rebase#Documentation/git-rebase.txt--r
git rebase -i HEAD~5
命令会弹出编辑器。它列出了指定的提交(在本例中为五个)。第一列包含每次提交的pick
。只需在该编辑器中将pick
替换为reword
并保存并关闭编辑器即可。然后 git 将为您将pick
更改为reword
的每个提交弹出编辑器,并让您编辑提交消息。The
git rebase -i HEAD~5
command pops up the editor. It lists the specified commits (in this case five of them). The first column containspick
for every commit. Just replacepick
withreword
in that editor and save+close the editor. Then git will pop up the editor for every commit where you changedpick
toreword
and will let you edit the commit message.