Roman Roman - 3 months ago 30
Git Question

How to see and go to local branches in git?

I have just created a brand-new git repository:

git init

By executing

git status

I have determined that I am in the master branch. The first line of the output was:

On branch master

As a first step I wanted to create a branch and go in there. I have learned that one can do these two steps just by one command:

git checkout -b aaa

I this way I have created a branch called "aaa" and went there. I could confirm it with "git status" (it tells me "On branch aaa"). Now I want to go back to the "master" branch. So, I execute:

git checkout master

As a result I get:

error: pathspec 'master' did not match any file(s) known to git.

So, how do I go to another (existing) branch in git? Moreover, I do not even know what branches exist. How can I see a list of existing branches?

Answer Source

You had no commit in master branch so, master does not exist actually.

Create & checkout local master branch:

$ git checkout -b master

You can see branch list(s):

$ git branch       # see local branch(es)
$ git branch -r    # see remote branch(es)
$ git branch -a    # see all local & remote branch(es) 

Do changes, git add -A, git commit -m 'message'. So, now this commit actually point to the master branch.

N.B. By git init command default branch is master (it's not real branch, just default git convention). Then without doing any commit you checked out to aaa branch. So, master is vanished cause no branch exists without any commit history.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download