0

I have some scripts that query our host (Gitlab) for all of a project's tags and the commit hashes that the tags point to. Then it does some more work with those hashes.

This generally works, but I have one commit where I get a different hash than what is displayed with a git log on that commit.

The thing is, it's not exactly wrong. The hash that is returned does let me checkout the correct commit:

>>git checkout 6a923
HEAD is now at ae67e044

and

>>git log -1 6a923
commit ae67e044359c86781fd7c1b74016f858f00584f8 (HEAD, tag: <correct tag>)

but it obviously doesn't serve when I want to do things like compare hashes later on.

I thought hashes were unique. This seems to be some kind of alias hash. What is going on here?

Additional Info:

>>git cat-file -p 6a923
object ae67e044359c86781fd7c1b74016f858f00584f8
type commit
tag <correct tag>
tagger <correct tagger> 1558448485 -0400
  • 3
    Just to be sure, by chance, do you have a tag named `6a923`? – KamilCuk Oct 21 '20 at 15:07
  • I do not. All the tags begin with a letter or a "1" – theredwagoneer Oct 21 '20 at 15:11
  • 2
    I wanted to ask the exact same thing. Could be a branch name as well. – kriegaex Oct 21 '20 at 15:12
  • What does `git log -1 6a923` say? – kriegaex Oct 21 '20 at 15:14
  • @kriegaex I've added log result to question as well. It is not a branch or tag name. Using the full hash gives the same results. – theredwagoneer Oct 21 '20 at 15:20
  • 4
    Show us in the question the output of: `git cat-file -p 6a923` – eftshift0 Oct 21 '20 at 15:28
  • The hash is just a SHA1 of the contents of the commit. If both commits contain the same contents (somehow), or a collision occurs (which can happen in SHA1) they could have the same hash. I don't think they are guaranteed to be unique – Liam Oct 21 '20 at 15:37
  • Does this answer your question? [Is it possible to get identical SHA1 hash?](https://stackoverflow.com/questions/2479348/is-it-possible-to-get-identical-sha1-hash) – Liam Oct 21 '20 at 15:37
  • @Liam No. I understand your point, but I'm not talking about two commits with the same hash. I'm talking about two hashes pointing to the same commit. – theredwagoneer Oct 21 '20 at 15:39
  • Also see [Is there duplicated SHA commits? [duplicate]](https://stackoverflow.com/questions/34888175/is-there-duplicated-sha-commits) which lead to the above – Liam Oct 21 '20 at 15:39
  • That's impossible. A hash is a hash of the contents of the commit. A commit can't have **two contents**. I'm guessing you've got a weird named tag or branch – Liam Oct 21 '20 at 15:42

1 Answers1

6

6a923 is just an annotated tag that points to revision ae67e04435. https://git-scm.com/book/en/v2/Git-Basics-Tagging

eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • Exactly. The output of `git cat-file -p 6a923` implies it is a tag. If you issue `git cat-file -t 6a923`, it should print `tag`. But this result contradicts the OP's statement, claiming that it was not a tag name. It looks as though it is, however. – kriegaex Oct 21 '20 at 15:45
  • This is the right answer. Thank you! @kriegaex It doesn't contradict my statement. It is not a tag name. It is the hash of the annotated tag. I did not know about annotated tags, and I definitely didn't know they had their own hashes. – theredwagoneer Oct 21 '20 at 15:54
  • Ah yes, of course, the annotated tag's hash. You should see the object representing the tag via `ls -l .git/objects/6a/923*` and be able to uncompress it via ZLIB, basically you should see pretty much the same as with `cat-file`, only starting with `tag \0` or so. – kriegaex Oct 21 '20 at 16:01
  • You can verify my last statement by a Perl script with these 3 lines: `#!/bin/bash`, `FILE="$1"`, `perl -MCompress::Zlib -e 'undef $/; print uncompress(<>)' < "$FILE"`. Save it as `git-deflate.sh` and run `./git-deflate.sh .git/objects/6a/923...` (full name). – kriegaex Oct 21 '20 at 16:03
  • BTW, annotated tags must have their own objects with hashes because they need to be able to store comments like commits. My Git was a little rusty, but now it is coming back. – kriegaex Oct 21 '20 at 16:06
  • I noticed you are new here, @theredwagoneer. Please note that you should mark this correct answer as accepted in order to close it. Just click the grey checkmark, making it green. As soon as you have enough reputation points to upvote the answer, you can also do that. – kriegaex Oct 22 '20 at 00:18