Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does "git status" show I'm on the master branch and "git branch" does not in a newly created repository?

Tags:

git

I'm trying to automate a process and issue the git branch command to find out what branch I am on. Everything is working fine except for a newly initialized repo where git branch returns nothing. Given I've done nothing with the repo, not even the initial commit, I can accept the answer. However, if I run a git status it tells me I'm on the master branch, as seen here:

$ mkdir todelete $ cd todelete $ git init Initialized empty Git repository in /u/u70021a/todelete/.git $ git status On branch master  No commits yet  nothing to commit (create/copy files and use "git add" to track) $ git branch $ 

Am I doing something wrong? Is there some setting I haven't set properly?

I also have a number of new people to Git and I can't explain to them why the command to show which branch they are on shows nothing, yet the status command does.

like image 211
GOVarney Avatar asked Jul 16 '19 06:07

GOVarney


People also ask

What is M flag in git branch?

-M is a flag (shortcut) for --move --force per the docs page on git branch . It renames the branch main (since the default branch name for repositories created using the command line is master , while those created in GitHub [starting in Oct.

Why does git status says up to date but not?

On branch master, Your branch is up to date with 'origin/master. As long as you haven't made any new commits that message is correct, unversioned/changed files that haven't been committed yet should be listed below it. If unversioned files don't show up, check if they might be hidden by a .

What does git status tell you?

The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven't, and which files aren't being tracked by Git. Status output does not show you any information regarding the committed project history.


2 Answers

I upvoted two other answers, but I think the way to think of this is simple: You can be on a branch that doesn't exist. That's normal in a new empty repository, too, because for a branch name to exist, that branch name must identify the hash ID of an existing, valid commit. A new empty repository has no commits, so no branch names are allowed to exist yet.

Nonetheless, you are, initially, on some branch. The branch you are on is the one whose name is stored in the special name HEAD. In a new, empty repository, Git stores the name master (more precisely, refs/heads/master—the full name of the branch) in HEAD, so you are on master, while master does not exist.

You can change which non-existent branch you are on using git checkout -b:

$ git init Initialized empty Git repository in [path] $ git checkout -b asdf Switched to a new branch 'asdf' $ git checkout -b hello Switched to a new branch 'hello' 

Whenever you are on a branch that does not exist, the next commit you make creates the branch. This is also how git checkout --orphan works.

like image 196
torek Avatar answered Oct 15 '22 14:10

torek


git branch shows nothing because there is no branch. But, as you can read in man git init:

This command creates an empty Git repository - basically a .git directory with subdirectories for objects, refs/heads, refs/tags, and template files. An initial HEAD file that references the HEAD of the master branch is also created.

I bolded the part I think is relevant - it looks like although there is no master branch yet, a reference to it already exists and that is why it is shown in git status. A proper branch will be created upon committing.

like image 35
Stanowczo Avatar answered Oct 15 '22 16:10

Stanowczo