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.
-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.
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 .
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With