{"thread":{"id":"60283","subject":"bug - Inconsistency of git status and branch, ./git/branches and ./git/HEAD","startedAt":"2023-09-30T06:53:24Z","lastAt":"2023-09-30T17:25:54Z","messageCount":5,"participants":["Irina Gulina","Chris Torek","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"482465","messageId":"CAAcHogVu+jrLdtEZi+Jx9d9gM5BvUF_nRVKEc4BsWCxCanL9Uw@mail.gmail.com","threadId":"60283","inReplyTo":null,"subject":"bug - Inconsistency of git status and branch, ./git/branches and ./git/HEAD","fromName":"Irina Gulina","fromEmail":"igulina@redhat.com","sentAt":"2023-09-30T06:51:09Z","receivedAt":"2023-09-30T06:53:24Z","isPatch":false,"sender":{"key":"igulina@redhat.com","avatar":null},"body":"Thank you for filling out a Git bug report!\nPlease answer the following questions to help us understand your issue.\n\nWhat did you do before the bug happened? (Steps to reproduce your issue)\n1. create an empty directory in CLI: mkdir test_repo\n2. convert it to a git directory: cd test_repo & git init\n3. 'git status' will say you are \"On branch master\"\n4. 'git branch', 'git branch --all', or 'git branch --list' - nothing\nwill say the master branch exists. And per man pages definition the\nbranch command displays existing branches. So the question is why 'git\nstatus' says \"On branch master\", but 'git branches' don't?\n5. check on 'ls -la ./git/branches' - it's empty\n6. check on 'ls -la ./git/refs/heads' - it's empty\n7. check on 'cat ./git/HEAD' - it shows 'ref: refs/heads/master' -\nwhy? if we didn't create any commit, and no branch exists?\n\nWhat did you expect to happen? (Expected behavior)\ngit status should not say \"On branch master\", cat ./git/HEAD should be\nempty.  Or the master branch should be created in ./git/branches and\nthe 'git branch' command should display it.\n\nWhat happened instead? (Actual behavior)\nInconsistency between 'git status', 'git branch', ./git/branches and ./git/HEAD\n\nWhat's different between what you expected and what actually happened?\n\nAnything else you want to add:\n\nPlease review the rest of the bug report below.\nYou can delete any lines you don't wish to share.\n\n\n[System Info]\ngit version:\ngit version 2.41.0\ncpu: x86_64\nno commit associated with this build\nsizeof-long: 8\nsizeof-size_t: 8\nshell-path: /bin/sh\nuname: Linux 6.4.12-200.fc38.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Aug 23\n17:46:49 UTC 2023 x86_64\ncompiler info: gnuc: 13.1\nlibc info: glibc: 2.37\n$SHELL (typically, interactive shell): /bin/bash\n\n\n[Enabled Hooks]\n\n"},{"id":"482466","messageId":"CAAcHogUEtcsFcDYp16g=154CMPh_a5fmY6ABEfp9zeqEJBNb6w@mail.gmail.com","threadId":"60283","inReplyTo":"CAAcHogVu+jrLdtEZi+Jx9d9gM5BvUF_nRVKEc4BsWCxCanL9Uw@mail.gmail.com","subject":"Re: bug - Inconsistency of git status and branch, ./git/branches and ./git/HEAD","fromName":"Irina Gulina","fromEmail":"igulina@redhat.com","sentAt":"2023-09-30T07:03:22Z","receivedAt":"2023-09-30T07:05:40Z","isPatch":false,"sender":{"key":"igulina@redhat.com","avatar":null},"body":"apologies for a typo: all paths written with ./git, meant to be .git\n\n\nOn Sat, Sep 30, 2023 at 8:51 AM Irina Gulina <igulina@redhat.com> wrote:\n>\n> Thank you for filling out a Git bug report!\n> Please answer the following questions to help us understand your issue.\n>\n> What did you do before the bug happened? (Steps to reproduce your issue)\n> 1. create an empty directory in CLI: mkdir test_repo\n> 2. convert it to a git directory: cd test_repo & git init\n> 3. 'git status' will say you are \"On branch master\"\n> 4. 'git branch', 'git branch --all', or 'git branch --list' - nothing\n> will say the master branch exists. And per man pages definition the\n> branch command displays existing branches. So the question is why 'git\n> status' says \"On branch master\", but 'git branches' don't?\n> 5. check on 'ls -la ./git/branches' - it's empty\n> 6. check on 'ls -la ./git/refs/heads' - it's empty\n> 7. check on 'cat ./git/HEAD' - it shows 'ref: refs/heads/master' -\n> why? if we didn't create any commit, and no branch exists?\n>\n> What did you expect to happen? (Expected behavior)\n> git status should not say \"On branch master\", cat ./git/HEAD should be\n> empty.  Or the master branch should be created in ./git/branches and\n> the 'git branch' command should display it.\n>\n> What happened instead? (Actual behavior)\n> Inconsistency between 'git status', 'git branch', ./git/branches and ./git/HEAD\n>\n> What's different between what you expected and what actually happened?\n>\n> Anything else you want to add:\n>\n> Please review the rest of the bug report below.\n> You can delete any lines you don't wish to share.\n>\n>\n> [System Info]\n> git version:\n> git version 2.41.0\n> cpu: x86_64\n> no commit associated with this build\n> sizeof-long: 8\n> sizeof-size_t: 8\n> shell-path: /bin/sh\n> uname: Linux 6.4.12-200.fc38.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Aug 23\n> 17:46:49 UTC 2023 x86_64\n> compiler info: gnuc: 13.1\n> libc info: glibc: 2.37\n> $SHELL (typically, interactive shell): /bin/bash\n>\n>\n> [Enabled Hooks]\n\n"},{"id":"482467","messageId":"CAPx1GvcjvctrQ-w1MekBs7S+R_7=KhCLUQ915-JAnxEq=BE3iQ@mail.gmail.com","threadId":"60283","inReplyTo":"CAAcHogVu+jrLdtEZi+Jx9d9gM5BvUF_nRVKEc4BsWCxCanL9Uw@mail.gmail.com","subject":"Re: bug - Inconsistency of git status and branch, ./git/branches and ./git/HEAD","fromName":"Chris Torek","fromEmail":"chris.torek@gmail.com","sentAt":"2023-09-30T11:28:47Z","receivedAt":"2023-09-30T11:29:09Z","isPatch":false,"sender":{"key":"chris.torek@gmail.com","avatar":"https://avatars.githubusercontent.com/u/16826774?v=4"},"body":"On Sat, Sep 30, 2023 at 4:06 AM Irina Gulina <igulina@redhat.com> wrote:\n> ... So the question is why 'git\n> status' says \"On branch master\", but 'git branches' don't?\n\nAlthough this seems weird, it is in fact normal.\n\nThis situation in Git -- where you are on a branch that does not exist --\nis called an \"orphan branch\" or an \"unborn branch\" (which term is used\ndepends on which bit of documentation you read).\n\nThe unborn / orphan current branch becomes \"born\" (begins to exist)\nonce you make a commit while on it. If you move to a different branch,\nthe unborn branch never comes into existence.\n\nA new, empty repository cannot have any branches, because a branch\nis defined as \"the latest commit that is on the branch\" (this definition is\nunsatisfyingly circular, but that too is what it is). With no commits in the\nrepository, no branches can exist. Yet you must be \"on\" some branch.\nHence the existence of unborn branches, which are born when you\nmake a new commit. That first commit creates the first branch name\nas well as the first commit.\n\nCreating a new commit while on an unborn branch causes the new\ncommit to have no parent commit. This, too, is normal and natural\nin a new empty repository, but it holds true for the situation in which\nyou create a new unborn / orphan branch using `git checkout --orphan`\nor `git switch --orphan` while in a non-empty repository. You don't\nnormally want to do this, but it's sometimes useful for some special\noddball cases.\n\nChris\n"},{"id":"482468","messageId":"CAPx1GvdNbHFZdDtWh-s6dN52oxV6L2Ww7CV+SAefenkekiOy+A@mail.gmail.com","threadId":"60283","inReplyTo":"CAPx1GvcjvctrQ-w1MekBs7S+R_7=KhCLUQ915-JAnxEq=BE3iQ@mail.gmail.com","subject":"Re: bug - Inconsistency of git status and branch, ./git/branches and ./git/HEAD","fromName":"Chris Torek","fromEmail":"chris.torek@gmail.com","sentAt":"2023-09-30T11:32:14Z","receivedAt":"2023-09-30T11:32:32Z","isPatch":false,"sender":{"key":"chris.torek@gmail.com","avatar":"https://avatars.githubusercontent.com/u/16826774?v=4"},"body":"I should note a slight correction here:\n\nOn Sat, Sep 30, 2023 at 4:28 AM Chris Torek <chris.torek@gmail.com> wrote:\n> A new, empty repository cannot have any branches, because a branch\n> is defined as \"the latest commit that is on the branch\" (this definition is\n> unsatisfyingly circular, but that too is what it is).\n\nThis is just one of multiple definitions of \"branch\" in Git. A branch is also\na *set* of commits, and/or a *name*. The fact that the word \"branch\" has\nso many meanings in Git makes it almost meaningless: it's like being at\na party where everyone is named Chris. Yet somehow we humans still\nmanage to extract meaning from a sentence like \"Hey Chris, Chris said\nto tell you that Chris can't get Chris's car started, so Chris and Chris\nwon't be here.\"\n\nChris\n"},{"id":"482471","messageId":"xmqqcyxzfu8z.fsf@gitster.g","threadId":"60283","inReplyTo":"CAAcHogUEtcsFcDYp16g=154CMPh_a5fmY6ABEfp9zeqEJBNb6w@mail.gmail.com","subject":"Re: bug - Inconsistency of git status and branch, ./git/branches and ./git/HEAD","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2023-09-30T17:25:48Z","receivedAt":"2023-09-30T17:25:54Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Irina Gulina <igulina@redhat.com> writes:\n\n>> What did you do before the bug happened? (Steps to reproduce your issue)\n>> 1. create an empty directory in CLI: mkdir test_repo\n>> 2. convert it to a git directory: cd test_repo & git init\n>> 3. 'git status' will say you are \"On branch master\"\n>> 4. 'git branch', 'git branch --all', or 'git branch --list' - nothing\n>> will say the master branch exists. And per man pages definition the\n>> branch command displays existing branches. So the question is why 'git\n>> status' says \"On branch master\", but 'git branches' don't?\n\nChris gave a good answer to this most relevant question; let me\nfill in one gap with others.\n\n>> 5. check on 'ls -la ./git/branches' - it's empty\n\nThe directory is where you would configure how individual local\nbranches you have would behave, and it is expected to be empty if\nyou did not do anything special.\n\n>> 6. check on 'ls -la ./git/refs/heads' - it's empty\n\nYou can never expect the contents of .git/refs/*  hierarchy to\ncorrespond to any state of the refs.  If you have a history behind\nyour local branch 'master', i.e., after running something like\n\n    $ git init empty && cd empty && git commit --allow-empty -m init\n\nyou can \"pack\" the references, i.e.,\n\n    $ git pack-refs --all\n\nand all the necessary data for Git to know what your branches are\nand what commits they point at will be removed from .git/refs/* and\nstored in different place in .git/ hierarchy.  So \"there is no file\nat .git/refs/heads/master\" does not mean \"there is no master branch\",\neven when we are not talking about a branch in the unborn state.\n\n>> 7. check on 'cat ./git/HEAD' - it shows 'ref: refs/heads/master' -\n>> why? if we didn't create any commit, and no branch exists?\n\nThe same comment applies to the above.  If you want to know the\nstate of HEAD, there are commands to do so (\"git branch --list\" being\none of them).  Do not expect that .git/HEAD would stay forever be\nthe implementation detail of what the HEAD ref points at.\n"}]}