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

Re: bug - Inconsistency of git status and branch, ./git/branches and ./git/HEAD

From
Chris Torek <chris.torek@gmail.com>
Date
Sep 30, 2023, 11:28 UTC
Message-ID
<CAPx1GvcjvctrQ-w1MekBs7S+R_7=KhCLUQ915-JAnxEq=BE3iQ@mail.gmail.com>
In-Reply-To
<CAAcHogVu+jrLdtEZi+Jx9d9gM5BvUF_nRVKEc4BsWCxCanL9Uw@mail.gmail.com>
On Sat, Sep 30, 2023 at 4:06 AM Irina Gulina <igulina@redhat.com> wrote:
> ... So the question is why 'git
> status' says "On branch master", but 'git branches' don't?
Although this seems weird, it is in fact normal.

This situation in Git -- where you are on a branch that does not exist -- is called an "orphan branch" or an "unborn branch" (which term is used depends on which bit of documentation you read).

The unborn / orphan current branch becomes "born" (begins to exist) once you make a commit while on it. If you move to a different branch, the unborn branch never comes into existence.

A new, empty repository cannot have any branches, because a branch is defined as "the latest commit that is on the branch" (this definition is unsatisfyingly circular, but that too is what it is). With no commits in the repository, no branches can exist. Yet you must be "on" some branch. Hence the existence of unborn branches, which are born when you make a new commit. That first commit creates the first branch name as well as the first commit.

Creating a new commit while on an unborn branch causes the new commit to have no parent commit. This, too, is normal and natural in a new empty repository, but it holds true for the situation in which you create a new unborn / orphan branch using `git checkout --orphan` or `git switch --orphan` while in a non-empty repository. You don't normally want to do this, but it's sometimes useful for some special oddball cases.

Chris
Previous: Junio C HamanoNext: Chris Torek
Message 4 of 5 in “bug - Inconsistency of git status and branch, ./git/branches and ./git/HEAD”
  1. Irina GulinaSep 30, 2023
  2. Irina GulinaSep 30, 2023
  3. Junio C HamanoSep 30, 2023
  4. Chris TorekSep 30, 2023
  5. Chris TorekSep 30, 2023

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.