How to specify the latest repository commit in go.mod? Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why you might need a "live" commit

\n

Have you ever written Go code, added a library via go get, and then discovered that its author fixed a critical bug but hasn't released a new tag (release) yet? Or maybe you're developing a library yourself and want to test it in your main project before publishing a version?

\n

Go modules have a cool feature: you can specify in go.mod not just a version number (v1.2.3), but a specific commit from a Git repository. This allows you to work with the very latest code, even if it hasn't been wrapped in a release tag yet.

\n

This article is a translation and adaptation of one of the most popular questions on Stack Overflow in the Go world. We'll break down how to do this correctly, what pitfalls exist, and how not to break your build.

\n\n

What is go.mod and why are commits there?

\n

go.mod is a file located in the root of your Go project. It describes all the dependencies (modules) your application needs. Typically, lines look like this:

\n
require (\n    github.com/someuser/somelib v1.2.3\n)
\n

But what if the feature you need isn't in v1.2.3, but only exists in the latest commit on the main branch? In Go, this is solved with pseudo-versions.

\n\n

What is a pseudo-version?

\n

This is a special string that replaces a regular version number. It consists of:

\n
    \n
  • A base version (if there is a tag from which it branched)
  • \n
  • The commit timestamp (in yyyymmddhhmmss format)
  • \n
  • The commit hash (first 12 characters)
  • \n
\n

Example: v0.0.0-20250320120000-abc123def456

\n

Don't be afraid of this long string — it's almost always generated by the go tool itself.

\n\n

Method #1: Using go get with the @latest flag

\n

This is the simplest and safest method. You simply tell Go: "Get the latest version from the repository."

\n

Step 1: Open a terminal in the root of your project.

\n

Step 2: Run the command:

\n
go get github.com/someuser/somelib@latest
\n

After this, Go will do the following:

\n
    \n
  • Download the latest commit from the default branch (usually main or master).
  • \n
  • Generate a pseudo-version.
  • \n
  • Automatically update go.mod and go.sum.
  • \n
\n

Result in go.mod:

\n
require (\n    github.com/someuser/somelib v0.0.0-20250320120000-abc123def456\n)
\n

Done! You've just pinned your project to the very latest commit.

\n\n

If you need to specify a specific commit hash

\n

If you know the exact hash (e.g., abc123def456789), you can substitute it directly:

\n
go get github.com/someuser/somelib@abc123def456789
\n

Go will convert it into a pseudo-version itself.

\n\n

Method #2: Manual editing of go.mod

\n

Sometimes you need to edit the file manually (for example, if you don't have terminal access or you're writing a CI script).

\n

Step 1: Open go.mod in any editor.

\n

Step 2: Find the line with the required dependency and replace the version with latest or a specific hash.

\n
require (\n    github.com/someuser/somelib latest\n)
\n

Step 3: Save the file and run in the terminal:

\n
go mod tidy
\n

The go mod tidy command will fetch missing dependencies, remove unnecessary ones, and replace the word latest with the actual pseudo-version.

\n

Important: If you simply write a hash manually (e.g., v0.0.0-abc123def456), Go may throw an error. It's always better to run go mod tidy after manual edits.

Blogs

Book Recommendations