git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: AW: git log on bare repository fails if the default branch is not called master

From
Chris Torek <chris.torek@gmail.com>
Date
Jun 4, 2022, 00:50 UTC
Message-ID
<CAPx1GvcxtvowiLkmihZQX+8-=+5c-zJT3M7OmO_BxO4qqqAMyA@mail.gmail.com>
In-Reply-To
<9df6c37e-4c43-7697-0562-c49d4fa5b38f@mnementh.co.uk>
On Fri, Jun 3, 2022 at 2:49 PM Ian Molton <git-ian@mnementh.co.uk> wrote:
> Why does a bare repo even have a HEAD file?

All repositories (bare or not) *must* have a `HEAD` setting. This contains the name of the current branch, or, if the repository is in detached-HEAD mode, the hash ID of the current commit.

In a new repository (bare or not), there are no commits, so there cannot be a *current commit*. A branch name is also constrained to contain a valid hash ID, and there are no valid hash IDs, so there are no branch names. Nonetheless you are still "on" some branch: it's just an *unborn* (or "orphan") branch.

In this mode, making a commit causes the branch name to spring into existence. So if your new empty repository is "on" `master`, the next `git commit` you run in that repository will create the branch `master`. As it's a bare repository, you probably will never run `git commit` there.

As Junio noted, the current branch is the branch name that a Git repository "recommends" to another Git that is performing a `git clone` operation. This makes it the "default branch". Normally we'd use `git switch` or `git checkout` to change the *current* branch and thus change the default branch, but in a bare repository, we cannot do this (at least not without supplying a temporary working tree), so you must either use `git symbolic-ref` or resort to direct writing to `.git/HEAD` to change the default branch in the bare repo.

The new `init.defaultBranch` configuration setting controls the name of the nonexistent branch you're on in a newly created repository. Given your setup you may wish to configure yours to be `main`.

Last, note that you can re-create this "we are now on a branch that does not exist" status at any time you like (in an ordinary non-bare repository) using `git switch` or `git checkout` with the `--orphan` argument. (Watch out, there's a subtle difference between these two commands: use `git status` to observe the index state before committing.)

Chris
Previous: Junio C Hamano
Message 5 of 5 in “BUG: git log on bare repository fails if the default branch is not called master”
  1. Ian MoltonJun 3, 2022
  2. AW: git log on bare repository fails if the default branch is not called masterstefan.naewe@atlas-elektronik.com, Jun 3, 2022
  3. Ian MoltonJun 3, 2022
  4. Junio C HamanoJun 3, 2022
  5. Chris TorekJun 4, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.