Git checkout another branch
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
Related videos on Youtube
NoBugs
Updated on October 07, 2020Comments
-
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. Runninggit checkout anotherbranch
works, butgit status
says I am still on branch 'master'. What am I doing wrong?-
poke almost 12 yearsDoes
git checkout anotherbranch
produce any output? -
NoBugs almost 12 yearsIt shows no output, no error.
-
-
NoBugs almost 12 yearsSo why does git checkout branchname never switch to this? Branch exists on github, I just want to merge master changes to it.
-
knittl almost 12 years@NoBugs:
git checkout -b remotemaster mygithub/master
should create a new branch offmygithub/master
and switch to it. -
NoBugs almost 12 years
git checkout -b otherbranch mygithub/otherbranch
works, butgit merge mygithub/master
wants to 'fast forward' and delete files from the non-master, that I want to keep. -
knittl almost 12 years@NoBugs: Do you want to merge
mygithub/master
intomaster
, or do you want to mergemaster
intomygithub/master
? A 'fast forward' is just a special case of a merge. -
NoBugs almost 12 yearsCreating a new branch helped, unfortunately it was unable to push back to the original branch.
error: src refspec otherbranch does not match any.
-
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'sgit push mygithub otherbranch:otherbranch
) -
ahoffer over 5 yearsThis will create a new branch. Reading the question, I do not think the poster wants to create a new branch.