-1

I have just created a brand-new git repository:

git init

Now I want to see what branch I am in. So, I execute:

git branch

This command does not output anything (my expectation was that I will see only one branch "master" and I will be in there).

So, I guess there are no branches at all. So, I try to create a branch by:

git checkout -b aaa

As a result I see:

Switched to a new branch 'aaa'

But when I execute

git branch

I get no output again. So, how can I verify that I am in a particular branch?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Roman
  • 124,451
  • 167
  • 349
  • 456
  • 1
    Use `git status`. `git branch` for managing branches, not for checking your current branch. – bronislav Nov 24 '17 at 08:02
  • Until you make your first commit, some of the git commands doesn't produce the expected output. – Lasse V. Karlsen Nov 24 '17 at 08:02
  • But, as far as I remember, earlier, when I worked with another repository, "git branch" gave me a list of existing branches and "*" symbol indicated what branch I am in. – Roman Nov 24 '17 at 08:08
  • `git branch` or `git branch --list` *should* show you that: https://git-scm.com/docs/git-branch – jonrsharpe Nov 24 '17 at 08:17

3 Answers3

6

You could do a git status.

The first line will be: On branch aaa.

fr3dch3n
  • 409
  • 3
  • 11
2

fr3dch3n's answer is correct—running git status will tell you which branch you're on—but doesn't address why git branch shows you nothing at all. The reason is that you are in a special state.

You just created a new, empty repository—a repository with no commits at all—by doing your git init.

Now, the thing about branch names in Git is that they always—always—point to one specific commit. But you have no commits at all, so how can a branch name like master or aaa point to one?

The answer is that it can't, so it doesn't, and Git achieves that by not creating the branch yet. You are, quite simply, on a branch that doesn't exist.

This special state has to occur when the repository is new and completely empty of commits, but it also occurs if you use git checkout --orphan <newbranch>. This puts you on the new branch witihout first creating the branch, so that the new branch doesn't exist.

When a branch doesn't exist, yet you're "on" that branch in git status terms, and you run git commit, Git creates a new commit as usual,1 and at the end of the process, Git updates the current branch name so that it points to the new commit it just made. If the branch didn't exist before, now it does!

So, in a new, empty repository, the branch you're "on" doesn't exist, and switching to another new branch with git checkout -b puts you on the other new branch that continues not to exist. The branch that you were on, that didn't exist, simply vanishes into thin air: it was never there, it was just a phantom that you were "on", and now you're on a new and different phantom.

(The same thing happens with git checkout --orphan <newbranch> later: it puts you on this sort of ghostly non-existent branch, and if you then switch to a different new branch, you continue to be on a ghostly non-existent branch. Running git checkout master, or using the name of any actual existing branch, will switch you back to the existing branch, assuming the git checkout itself succeeds.)


1The git commit process works like this, although the precise order of various steps is somewhat changeable, and options like git commit --only <files> make bigger changes.

  1. Make sure it looks OK to commit (do any pre-commit testing required, including running a pre-commit hook).
  2. Turn the current index contents into at least one tree object, making a snapshot of all of the files ("blobs") that are in the index now. (This is the git write-tree command.)
  3. Gather together your name, email, and a timestamp as "author" and again as "committer" for this commit. Add on the commit log message.
  4. Figure out which commit(s) are the parent(s) for this commit.
  5. Make the new commit object (git commit-tree).
  6. Update the current branch name so that it points to the new commit.

It's step 6 that actually creates the branch, in this special "branch that is yet to be created" condition.

Note that in step 4, "figure out parents", Git notices this same "branch that is yet to be created" condition. The result of this condition is that there is no parent for the new commit: the new commit is a root commit. Normally, though, step 4 reads HEAD with the goal of find the hash ID of the current commit: HEAD contains the branch name, and the branch does exist and contains the current commit hash ID.

When committing the merge result after resolving conflicts from git merge, step 4 uses a trace file that git merge leaves behind, to determine the extra parent commit hash (normal merge) or hashes ("octopus" merge). The current or HEAD or @ commit is still, as usual, the first parent.

torek
  • 448,244
  • 59
  • 642
  • 775
0

Use git branch -a - It will show you all branch which are exist in local repo

and git status - show you current branch with updated file's

As you are beginner in Git, you should see this Git learning game It will definately help to increase your knowledge :)

Vrushali Raut
  • 1,130
  • 1
  • 10
  • 20