How to change git commit message without changing commit hash

前端 未结 3 889
失恋的感觉
失恋的感觉 2021-01-03 21:16

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 alwa
相关标签:
3条回答
  • 2021-01-03 21:58

    git notes is 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.

    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 notes isn't working for you).
    • Notes aren't pushed by default, unless you specify explicitly the refspec for them (refs/notes/*)
    0 讨论(0)
  • 2021-01-03 22:07

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

    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.

    0 讨论(0)
  • 2021-01-03 22:07

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

    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.

    See Progit internals for details.

    0 讨论(0)
提交回复
热议问题