64

Is there a convention for referring to a previous commit in a git commit message?

Example commit message:

Fixed bug such and such introduced in a1b2c3e4

In particular, is there a convention that github.com will understand, and convert to a link?

2 Answers 2

65

Yup - GitHub will pick up references to SHAs and users/repos patterns using the GitHub Flavored Markdown

Specifically about linking to commits:

A bit of the GitHub spice

In addition to the changes in the previous section, certain references are auto-linked:

  • SHA: be6a8cc1c1ecfe9489fb51e4869af15a13fc2cd2
  • User@SHA ref: mojombo@be6a8cc1c1ecfe9489fb51e4869af15a13fc2cd2
  • User/Project@SHA: mojombo/god@be6a8cc1c1ecfe9489fb51e4869af15a13fc2cd2
  • #Num: #1
  • User/#Num: mojombo#1
  • User/Project#Num: mojombo/god#1
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks! This is very helpful. I wonder if I'd get away with using an abbreviated SHA and still have it converted to a link..
Not sure - never tested it, but you could (if you're the only one using the remote, otherwise it gets a bit dangerous), push an abbreviated hash, then amend that commit if it doesn't work to the full one and push it again.
@Joel: It works ;-)
Thanks @desert69 for testing that
@Joel you still have to remember that it's an abbreviated SHA. If your repo gets big enough (kernel-like big, I know), your abbreviated SHA length could increase, leading you to ambiguities. So, you can use an abbreviated SHA, but if it's not that much effort, I think it's allways better to use the full SHA. Github auto-trims it for you.
|
19

Guys at git answers the question this way:

If you want to reference a previous commit in the history of a stable branch, use the format "abbreviated hash (subject, date)", like this:

Commit f86a374 (pack-bitmap.c: fix a memleak, 2015-03-30)
noticed that ...

The "Copy commit summary" command of gitk can be used to obtain this format (with the subject enclosed in a pair of double-quotes), or this invocation of git show:

git show -s --pretty=reference <commit>

or, on an older version of Git without support for --pretty=reference:

git show -s --date=short --pretty='format:%h (%s, %ad)' <commit>
Addition Feb 2024

I ended-up adding an alias ref = show -s --pretty=reference. It helps a lot.

2 Comments

Note that this is referring to a convention for contributing to git itself, rather than being a convention for git repositories in general.
It may be referring to how to commit to git itself, but it is a generally excellent set of guidelines for how to write a commit message. The git show -s --pretty=reference <commit> is a lovely command. Using this reference makes it much easier to understand what the commit is referring to without needing to click through to see the full commit details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.