32

I run:

 git checkout mygithub/master

but for some reason, running 'git status' shows "not currently on any branch". Running:

 git checkout master

and then git status, says that I'm now on branch master. Now I want to switch to another branch. Running git checkout anotherbranch works, but git status says I am still on branch 'master'. What am I doing wrong?

Martin Brown
  • 24,692
  • 14
  • 77
  • 122
NoBugs
  • 9,310
  • 13
  • 80
  • 146

3 Answers3

40

mygithub/master is a remote branch. To create a local branch based off of that remote branch, you have to use git checkout -b mymaster mygithub/master. Git tries to make this easy for you: if you write git checkout branchname, and branchname only exists in a remote, but not locally, Git will automatically set up a local branch with <remote>/branchname being its parent.

knittl
  • 246,190
  • 53
  • 318
  • 364
  • So why does git checkout branchname never switch to this? Branch exists on github, I just want to merge master changes to it. – NoBugs Aug 02 '12 at 18:51
  • @NoBugs: `git checkout -b remotemaster mygithub/master` should create a new branch off `mygithub/master` and switch to it. – knittl Aug 02 '12 at 18:54
  • `git checkout -b otherbranch mygithub/otherbranch` works, but `git merge mygithub/master` wants to 'fast forward' and delete files from the non-master, that I want to keep. – NoBugs Aug 02 '12 at 19:01
  • @NoBugs: Do you want to merge `mygithub/master` into `master`, or do you want to merge `master` into `mygithub/master`? A 'fast forward' is just a special case of a merge. – knittl Aug 02 '12 at 19:09
  • Creating a new branch helped, unfortunately it was unable to push back to the original branch. `error: src refspec otherbranch does not match any.` – NoBugs Aug 02 '12 at 20:03
  • 1
    @NoBugs: The branch must have an upstream branch configured or a remote branch with the same name. If this isn't the case, you can always be explicit about which branch to push: `git push origin localbranch:remotebranch` (I assume in your case that's `git push mygithub otherbranch:otherbranch`) – knittl Aug 02 '12 at 20:17
15

If you want to switch to another branch then run this command:

git checkout branch name

If you want to delete a branch then run this command:

git branch -D branch name

If you want to create a new branch then run this command:

git checkout -b branch
Aakash
  • 695
  • 3
  • 10
  • 25
Viru
  • 165
  • 1
  • 8
-7

If you want to checkout from master branch just run this command in your terminal

git checkout -b BRANCH_NAME

S.m.g. baquer
  • 369
  • 1
  • 5
  • 9
  • 5
    This will create a new branch. Reading the question, I do not think the poster wants to create a new branch. – ahoffer Oct 06 '18 at 19:43