Referencing other commits in commit messages

Last week, I asked whether you should include issue IDs in commit messages.

Another thing I like to reference in a commit message is the commit ID (or SHA) of a related commit.

For example, when I run git log in my website repository, I see commits like this:

commit 0c91825c16217d0fe7eff4ea100a67550051c4a9
Author: Oliver Davies <[email protected]>
Date:   Sat May 11 15:32:07 2024 +0200

    Create a cached talk counter

    Create a cached version of the talk counter service that returns a
    cached result of the talk count for that day.

    This uses the Decorator design pattern to decorate the existing
    `TalkCounter` service and works as they both implement the same
    `TalkCounterInterface`.

The sha for this commit is 0c91825c16217d0fe7eff4ea100a67550051c4a9.

If I was to make another commit that was related to this one, I can include this commit sha in my new commit message.

I also don't need to include the entire thing - only enough for it to be unique (usually five or six characters).

Once pushed, the commit IDs should never change, so this will be a permanent reference to the first commit.

Helpfully, websites like GitHub, GitLab and Bitbucket will identify it as a commit sha and make it clickable so you can easily navigate to the referenced commit.

- Oliver

Was this interesting?

Sign up here and get more like this delivered straight to your inbox every day.

About me

Picture of Oliver

I'm an Acquia-certified Drupal Triple Expert with 17 years of experience, an open-source software maintainer and Drupal core contributor, public speaker, live streamer, and host of the Beyond Blocks podcast.