Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: can't switch to new remote branch

I have an account on github and I use it from two different machines. On one, I created a new branch myNewBranch and switched to it. Then I did my modifications to my code, I committed and pushed to myNewBranch.

On the second machine, I can't figure out how to push to it.

$ git pull origin myNewBranch
From https://github.com/myUsername/myProject
 * branch            myNewBranch -> FETCH_HEAD
Already up-to-date.

[ I had already successfully pulled from it]

Then I try to switch to it, but I get an error:

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

What am I missing?

like image 518
Ricky Robinson Avatar asked May 30 '13 21:05

Ricky Robinson


People also ask

How do I force switch to another branch?

Force a Checkout You can pass the -f or --force option with the git checkout command to force Git to switch branches, even if you have un-staged changes (in other words, the index of the working tree differs from HEAD ). Basically, it can be used to throw away local changes.

How do I checkout to a new branch?

New Branches The git branch command can be used to create a new branch. When you want to start a new feature, you create a new branch off main using git branch new_branch . Once created you can then use git checkout new_branch to switch to that branch.

How do I pull a new branch from a remote?

If you have a single remote repository, then you can omit all arguments. just need to run git fetch , which will retrieve all branches and updates, and after that, run git checkout <branch> which will create a local copy of the branch because all branches are already loaded in your system.

Can I switch branch without commit?

when you switch to a branch without committing changes in the old branch, git tries to merge the changes to the files in the new branch. If merging is done without any conflict, swithing branches will be successful and you can see the changes in the new branch.


3 Answers

You need to fetch the data onto your local repository on machine 2 first:

$ git fetch origin
$ git checkout origin/myNewBranch
like image 156
m3rlin45 Avatar answered Oct 06 '22 08:10

m3rlin45


My guess on what happened there is a remote origin/myNewBranch, but not a local branch myNewBranch. What your command did was to fetch origin/myNewBranch to your current local branch. When you did the git checkout myNewBranch, the error happened because there was no local branch named myNewBranch. I suggest try git checkout -b myNewBranch origin/myNewBranch.

like image 30
cforbish Avatar answered Oct 06 '22 08:10

cforbish


Try doing git checkout origin/myNewBranch.

like image 30
Femaref Avatar answered Oct 06 '22 09:10

Femaref