Skip to content

cmd/go: avoid misleading git fatal message in a repo with no commits - #81258

Open
harjothkhara wants to merge 1 commit into
golang:masterfrom
harjothkhara:oss-find/go-2026-08-31
Open

cmd/go: avoid misleading git fatal message in a repo with no commits#81258
harjothkhara wants to merge 1 commit into
golang:masterfrom
harjothkhara:oss-find/go-2026-08-31

Conversation

@harjothkhara

@harjothkhara harjothkhara commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

In a git repository with no commits, "go build -v" prints git's
misleading message

fatal: your current branch 'main' does not have any commits yet

and a repository with no commits and a clean status fails to build
with "error obtaining VCS status".

Pass --ignore-missing and name HEAD explicitly so that git succeeds
with no output when there are no commits, and skip stamping the
revision and commit time in that case.

The abandoned CL 399828 by Filippo Rossi fixed this with
"git rev-list -n 1 --all", which can report a commit other than the
one checked out when the repository has more than one branch.
"log --ignore-missing HEAD" keeps the stamped revision unchanged for
repositories with commits.

Fixes #52263

In a git repository with no commits yet, the "git log" command that
stamps VCS information fails and prints

    fatal: your current branch 'main' does not have any commits yet

to the user's console during builds with -v, even though the build
succeeds. If the repository is also clean (for example, all files are
ignored), the build instead fails with "error obtaining VCS status".

Pass --ignore-missing and name HEAD explicitly, so that git succeeds
with no output when there are no commits, and skip stamping the
revision and commit time in that case. The flag is supported since at
least Git 1.7.6. An empty repository now builds without the message,
stamping only vcs and vcs.modified.

CL 399828 by Filippo Rossi fixed this with "git rev-list -n 1 --all".
That walks all refs, so in a repository with more than one branch it
can report a commit other than the one checked out. Using
"log --ignore-missing HEAD" keeps the stamped revision the same as
today for repositories that have commits. The test is based on the one
in CL 399828, updated for the review comments there.

Fixes golang#52263
@gopherbot

Copy link
Copy Markdown
Contributor

This PR (HEAD: 1273393) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/825284.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cmd/go: misleading fatal: log message when working in a git repo with no commits

2 participants