Git checkout another branch

128,289

Solution 1

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.

Solution 2

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
Share:
128,289

Related videos on Youtube

NoBugs
Author by

NoBugs

Updated on October 07, 2020

Comments

  • NoBugs
    NoBugs over 3 years

    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?

    • poke
      poke almost 12 years
      Does git checkout anotherbranch produce any output?
    • NoBugs
      NoBugs almost 12 years
      It shows no output, no error.
  • NoBugs
    NoBugs almost 12 years
    So why does git checkout branchname never switch to this? Branch exists on github, I just want to merge master changes to it.
  • knittl
    knittl almost 12 years
    @NoBugs: git checkout -b remotemaster mygithub/master should create a new branch off mygithub/master and switch to it.
  • NoBugs
    NoBugs almost 12 years
    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.
  • knittl
    knittl almost 12 years
    @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.
  • NoBugs
    NoBugs almost 12 years
    Creating a new branch helped, unfortunately it was unable to push back to the original branch. error: src refspec otherbranch does not match any.
  • knittl
    knittl almost 12 years
    @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)
  • ahoffer
    ahoffer over 5 years
    This will create a new branch. Reading the question, I do not think the poster wants to create a new branch.