676

I find a lot of answers on how to reference a GitHub issue in a git comment (using the #xxx notation). I'd like to reference a commit in my comment, generating a link to the commit details page?

pratnala
  • 3,723
  • 7
  • 35
  • 58
LodeRunner
  • 7,975
  • 3
  • 22
  • 24

5 Answers5

823

To reference a commit, simply write its SHA-hash, and it'll automatically get turned into a link.

the commit 3e5c1e60269ae0329094de131227285d4682b665 solved the issue...

Or use its prefix

the commit 3e5c1e6 solved the issue...

See also:

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
Sebastian Paaske Tørholm
  • 49,493
  • 11
  • 100
  • 118
  • 99
    Note that you needn't write the full hash. As usual, a prefix (2dd190e) suffices. – Daniel Wolf Jul 31 '14 at 18:12
  • 7
    please show the syntax or a link example of `https://github.com/PRJ/issues/NUMBER?VERSION?` – Peter Krauss Jul 16 '15 at 13:27
  • 3
    @PeterKrauss: There is no special syntax. Just write the commit hash and it automatically will be linked. – Sebastian Paaske Tørholm Jul 16 '15 at 13:28
  • hum.. Sorry, where this "commit hash" on my browser? Suppose that I not a programmer ;-) – Peter Krauss Jul 16 '15 at 13:34
  • @PeterKrauss If you go to the commit's page in your browser (for example, by clicking Commits and then clicking one of the individual commits), it'll be at the alphanumeric string at the end of the URL. For example: github.com/bob/coolproject/commit/COMMIT_HASH_IS_HERE – Josh1billion Nov 16 '15 at 22:04
  • 6
    Note that this only works for commits of the repo or its forks you are writing the issue for. I have a case where my fork suddenly was unforked and I noticed this commit trick not working anymore. – mxmlnkn Mar 02 '16 at 03:44
  • @SebastianPaaskeTørholm Can you tell why the [hashes are not rendered as links here](https://github.com/johnjohndoe/Umweltzone/blob/auto-link-commits/CHANGELOG.md)? – JJD Mar 06 '16 at 19:27
  • 2
    You may also want to check the project name, i.e.: whether the comment you're making is the same repository that you commit code into. For me, I have a repository to record all requirements items and the other one to manage my real codes, so when I adding a comment to my requirements repository I write: `repository@commit_hash` – LiuWenbin_NO. Oct 29 '19 at 09:24
  • Would this work if I put down the hash code but haven't pushed the commit with that hash yet? I mean, will the hash change to a link once I push? – Saifur Rahman Mohsin Feb 02 '20 at 18:30
  • Does this work if the commit ID is mentioned within a file in the repository itself e.g. `README.md`? – cst1992 Sep 03 '22 at 09:32
161

Answer above is missing an example which might not be obvious (it wasn't to me).

Url could be broken down into parts

https://github.com/liufa/Tuplinator/commit/f36e3c5b3aba23a6c9cf7c01e7485028a23c3811
                  \_____/\________/       \_______________________________________/
                   |        |                              |
            Account name    |                      Hash of revision
                        Project name              

Hash can be found here (you can click it and will get the url from browser).

enter image description here

Hope this saves you some time.

Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265
  • 4
    And you can also get it with `git log`, it will show up on the lines with `commit `. And if that doesn't work, it could be you did not do the `git push origin master`. Also, there is a bug in github, there must be at least one character after the or it doesn't get detected. It can just be a newline or a period. – Alexis Wilke Jun 15 '19 at 04:39
  • Remove reference to above. It may come on top. – Bhavya Gupta Oct 15 '20 at 08:32
15

If you are trying to reference a commit in another repo than the issue is in, you can prefix the commit short hash with reponame@.

Suppose your commit is in the repo named dev, and the GitLab issue is in the repo named test. You can leave a comment on the issue and reference the commit by dev@e9c11f0a (where e9c11f0a is the first 8 letters of the sha hash of the commit you want to link to) if that makes sense.

Yoseph
  • 608
  • 9
  • 22
  • 19
    This needs a correction (at least on Github) since you need `username/reponame@hash` in order to identify the fork. – pevogam Feb 01 '21 at 16:00
13

Just paste the commit referencing link in the comment github automatically mentions the commit on the comment.

-1

I don't think anyone answered the question as asked, perhaps it wasn't possible a decade ago.

However now, as per the github documentation a hash is not required. It can be done thusly:

Individual account

Username/Repository# and issue or pull request number

example: for the issue at https://github.com/aUser/user-repo/issues/23

use: aUser/user-repo#26

Organization

Organization_name/Repository# and issue or pull request number

example: for the issue at https://github.com/an-org/theirproject/issues/1000

use: an-org/theirproject#1000

stichResist
  • 535
  • 4
  • 2