如何在不更改提交哈希的情况下更改 git commit 消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5212957/
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-19 05:09:35  来源:igfitidea点击:

How to change git commit message without changing commit hash

githistory

提问by maaartinus

The title is not exact, but I can't express it better in a single line.

标题不准确,但我无法用一行更好地表达它。

  • I actually know how to change git commit message like here.
  • But I know it always changes the SHA-1 too, which I want to avoid.
  • 我实际上知道如何像这里一样更改 git commit 消息。
  • 但我知道它也总是会改变 SHA-1,我想避免这种情况。

I only want to seea different message in git-log. I thought it could be done somehow using git-notes, but I haven't managed it.

我只是想看看在不同的消息git-log。我认为可以使用 以某种方式完成git-notes,但我还没有做到。



Explanation:

解释:

I need it in order to fix errors in the commit messages. I always write there the name of a document containing my communication with the customer (it looks just like T1234 Replace foo by bar). The communication tends to be quite long, so I can loose a lot of time till I find out I was being mislead by wrong document name in the commit message.

我需要它来修复提交消息中的错误。我总是在那里写下包含我与客户通信的文档的名称(看起来就像T1234 Replace foo by bar)。沟通往往很长,所以我可以浪费很多时间,直到我发现我被提交消息中的错误文档名称误导了。

Use of git-notes

用于 git-notes

It looks like git-notesin fact works as stated here. However I alwaysuse

看起来git-notes实际上就像这里所说的那样工作。不过我总是

git log --oneline

so I neversee it. Concerning the comment about making git lie to the user: IMHO, this would be acceptable when this happened only when using a special switch like --replace-messages-by-notes, wouldn't it? As I always use an alias instead of using git logdirectly, I'd get what I want without typing a lot.

所以我从来没有看到它。关于让 git 向用户说谎的评论:恕我直言,只有在使用特殊开关(如--replace-messages-by-notes)时才会发生这种情况,不是吗?因为我总是使用别名而不是git log直接使用,所以我不需要输入很多东西就可以得到我想要的东西。

Do you think it's a reasonable feature request or would you recommend another workflow to me?

你认为这是一个合理的功能请求还是你会向我推荐另一个工作流程?

采纳答案by Mark Longair

As various people have pointed out (e.g. in VonC's very useful answer), git notesreally is the mechanism you're looking for. Is it not enough for you to change your alias to the following?

正如许多人指出的那样(例如在VonC 的非常有用的答案中),这git notes确实是您正在寻找的机制。把你的别名改成下面这样还不够吗?

git log --oneline --show-notes

Presumably it's only occasionally that you'll have to add a note to a commit, and the notes will visually stand out in the output of that command.

大概只是偶尔需要向提交添加注释,并且注释将在该命令的输出中直观地突出显示。

If you really want to replace the subject of each commit if notes exist, you could always create a script along the lines of:

如果您真的想在存在注释的情况下替换每个提交的主题,您可以始终按照以下方式创建脚本:

for c in $(git rev-list HEAD)
do
    n=$(git notes show $c 2> /dev/null)
    m=$(git show --oneline $c|head -1)
    if [ -n "$n" ]
    then
       m=${m/ */ $n}
    fi
    echo $m
done

... but that's a lot uglier for little gain, in my opinion.

......但在我看来,这更丑陋而收效甚微。

回答by VonC

git notesis the only way to have a different git log message (different than the commit message) without changing the SHA1, as mentioned in the "Notes to Self" article.

git notes是在不更改 SHA1 的情况下获得不同 git 日志消息(不同于提交消息)的唯一方法,如“自我说明”文章中所述

A few remarks though:

不过有几点说明:

  • Notes are organized by namespace, the default one being "commits".
  • Notes don't modify the commit message, they only add to it(which might be why git notesisn't working for you).
  • Notes aren't pushed by default, unless you specify explicitly the refspec for them (refs/notes/*)
  • 注释按命名空间组织,默认为“提交”。
  • 注释不会修改提交消息,它们只会添加到其中(这可能是为什么git notes不适合您的原因)。
  • 默认情况下不会推送注释,除非您明确指定它们的 refspec ( refs/notes/*)

回答by Johannes Rudolph

Technically this seems impossible (at least to me, I'm not a git pro though).

从技术上讲,这似乎是不可能的(至少对我来说,虽然我不是 git pro)。

A git commit stores a tree hash (think: the state of your working directory at that time) with additional commit information. When you change the commit message, the tree hash won't change, however the commit hash will change since it is computed from the commit object, there's no way around it.

git commit 存储树哈希(想想:当时工作目录的状态)和其他提交信息。当您更改提交消息时,树哈希不会更改,但是提交哈希会更改,因为它是从提交对象计算的,没有办法绕过它。

See Progit internalsfor details.

有关详细信息,请参阅Progit 内部结构