24

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.

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


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.

Use of git-notes

It looks like git-notes in fact works as stated here. However I always use

git log --oneline

so I never see 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 log directly, I'd get what I want without typing a lot.

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

Community
  • 1
  • 1
maaartinus
  • 44,714
  • 32
  • 161
  • 320
  • 1
    This sounds exactly like `git-notes` would be the solution, so perhaps it would help if you clarified why `git-notes` doesn't work for you. Why do you want to change what `git-log` says (effectively making `git-log` lie to the user)? – JesperE Mar 06 '11 at 20:01
  • Yes, git log should lie... but the lie is actually correct, because the entered commit msg was e.g. referring a wrong document. When I blindly follows what git-commit says, I just lose a non-negligible amount of time until I find out the mistake. – maaartinus Mar 06 '11 at 21:31
  • 2
    You don't actually want to change the commit message (and as others have said, it's fundamentally contrary to the way git works). But I don't think that's a problem - sounds like your real question is "how do I make `git log` show me notes *instead of* commit messages, if they're present?" – Cascabel Mar 06 '11 at 22:53
  • 1
    Right... now it's actually obvious to me, but it wasn't when I started the question. – maaartinus Mar 07 '11 at 00:16
  • 1
    You could use [`git replace`](http://www.kernel.org/pub/software/scm/git/docs/git-replace.html) to do what you want, but I would be wary of using it to replace a large number of commits (it could lead to performance problems due to a very large number of files in `.git/refs/replace/` — at least until the repository is repacked). – Chris Johnsen Mar 07 '11 at 04:05
  • I'm quite sure, I'd replace only few of them. It doesn't happen daily that I completely blow my commit message. – maaartinus Mar 07 '11 at 06:34

3 Answers3

18

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 (from the original Aug. 2010 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/*)
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • The "notes to self" website's SSL cert has expired - the content can be on the [wayback machine](https://web.archive.org/web/20160304024648/http://git-scm.com/2010/08/25/notes.html) (it seems to redirect to the git-scm blog). The git-scm blogpost itself no longer exists. – snakecharmerb Jun 18 '21 at 11:47
  • 1
    @snakecharmerb Thank you for the feedback. I have edited the answer accordingly, but also referenced the original article (still available): https://scottchacon.com/2010/08/25/notes.html – VonC Jun 18 '21 at 13:18
13

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.

Community
  • 1
  • 1
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • Your simple solution will probably suffice. The script would need a lot of tuning (colors, conditional use of pager, replacing the log message only while leaving decorations by tags, etc.), which would be too much work for me at the moment. Thx a lot. – maaartinus Mar 07 '11 at 06:31
9

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.

Johannes Rudolph
  • 35,298
  • 14
  • 114
  • 172
  • You're right, but I don't need to change the *stored* message, I need only to change the *displayed* one. – maaartinus Mar 06 '11 at 21:57
  • 1
    It looks like the progit internals link is dead, but you can find it on [archive.org](https://web.archive.org/web/20110310011515/http://progit.org/book/ch9-2.html) – jrh Feb 26 '20 at 02:01