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?

2
  • Does git checkout anotherbranch produce any output?
    – poke
    Aug 2, 2012 at 18:44
  • It shows no output, no error.
    – NoBugs
    Aug 2, 2012 at 18:46

3 Answers 3

42

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.

6
  • 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 2, 2012 at 18:51
  • @NoBugs: git checkout -b remotemaster mygithub/master should create a new branch off mygithub/master and switch to it.
    – knittl
    Aug 2, 2012 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 2, 2012 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 2, 2012 at 19:09
  • 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 2, 2012 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
-7

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

git checkout -b BRANCH_NAME

1
  • 5
    This will create a new branch. Reading the question, I do not think the poster wants to create a new branch.
    – ahoffer
    Oct 6, 2018 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.