threads / bug / 57951

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

Subject: BUG: git log on bare repository fails if the default branch is not called master

## tl;dr

5 messages between Jun 3, 2022 and Jun 4, 2022.

replies: 4people: 4as markdown or json

Ian Molton· Jun 3, 2022, 14:22 UTC · lore
Hi,
Please CC me in replies, I'm not on the list.

I've noticed that git log assumes the default branch is master, even if it doesn't exist...

My latest repo has it set to "mainline" and all was well until I pushed it to a bare repo on my server.

Expectation:
If I run
git log
in the bare repo, it should log the default (or only!) branch.
Reality:

git log # fatal: your current branch 'master' does not have any commits yet

-Ian
stefan.naewe@atlas-elektronik.com· Jun 3, 2022, 14:40 UTC · re: Ian Molton · lore

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

Show 30 quoted lines
> -----Ursprüngliche Nachricht-----
> Von: Ian Molton <git-ian@mnementh.co.uk>
> Gesendet: Freitag, 3. Juni 2022 16:22
> An: git@vger.kernel.org
> Cc: Ian Molton <git-ian@mnementh.co.uk>
> Betreff: BUG: git log on bare repository fails if the default branch is not called
> master
> 
> Hi,
> 
> Please CC me in replies, I'm not on the list.
> 
> I've noticed that git log assumes the default branch is master, even if it doesn't
> exist...
> 
> My latest repo has it set to "mainline" and all was well until I pushed it to a bare
> repo on my server.
> 
> Expectation:
> 
> If I run
> 
> git log
> 
> in the bare repo, it should log the default (or only!) branch.
> 
> Reality:
> 
> git log
> # fatal: your current branch 'master' does not have any commits yet
I bet the HEAD file in your bare repository points to refs/heads/master...
Stefan
Ian Molton· Jun 3, 2022, 18:06 UTC · re: stefan.naewe@atlas-elektronik.com · lore

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

On 03/06/2022 15:40, stefan.naewe@atlas-elektronik.com wrote:
> I bet the HEAD file in your bare repository points to refs/heads/master...

Possibly, but why? the only thiong that ever happened to was that I pushed my code into it, from a repo that used mainline (not master).

Why does a bare repo even have a HEAD file? (genuinely curious). It seems like it'd be best to create it when it's first pushed to? then it'd match whatever is used in the pushing repo...

-Ian
Junio C Hamano· Jun 3, 2022, 18:56 UTC · re: Ian Molton · lore

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

Ian Molton <git-ian@mnementh.co.uk> writes:
> Why does a bare repo even have a HEAD file? (genuinely curious). It
> seems like it'd be best to create it when it's first pushed to? then
> it'd match whatever is used in the pushing repo...

There indeed are things that have no reason to exist in a bare repository, but HEAD is not among them.

It should have HEAD to indicate what the owner of the repository considers the primary branch to follow the project hosted there. Anybody cloning from that bare repository will learn the name of what HEAD points at and that branch gets checked out when "clone" finishes its job.

Chris Torek· Jun 4, 2022, 00:50 UTC · re: Ian Molton · lore

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

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

← back to recent threads