Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkout branch on different remote

Tags:

git

branch

I have a repo that has another remote upstream besides origin. I can do git checkout origin/master, but when I run git checkout upstream/master, I get:

error: pathspec 'upstream/master' did not match any file(s) known to git. 

This does not work either:

$ git fetch upstream From https://github.com/getsentry/sentry  * branch            HEAD       -> FETCH_HEAD $ git co -b asdf --track upstream/master fatal: Cannot update paths and switch to branch 'asdf' at the same time. Did you intend to checkout 'upstream/master' which can not be resolved as commit? 

How to check out branches on upstream remote as I do on origin remote? My git version is 2.1.2.

like image 442
Fish Monitor Avatar asked Jun 16 '15 08:06

Fish Monitor


People also ask

Can you push a local branch to a different remote branch?

In order to push your branch to another remote branch, use the “git push” command and specify the remote name, the name of your local branch as the name of the remote branch.

Does git checkout create remote branch?

Actually, Git does not allow creating a (new, isolated) branch on a remote repository. Instead, you can push an existing local branch and thereby publish it on a remote repository.

How do I force checkout 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.


1 Answers

Just fetch the refs from the remote (this will fetch all branch, commit, refs etc for the upstream repo)

git fetch upstream 

After this, checkout the needed branch (this creates a local copy of the branch)

git checkout -b <branchname> --track upstream/<branchname> 

Now if you want to pull the changes in this branch in future, all you need to do is

git pull upstream <branchname> 

As mentioned here, try doing an explicit fetch on the branch name:

git fetch upstream master:branch_name 
like image 78
Anshul Goyal Avatar answered Sep 22 '22 01:09

Anshul Goyal