54

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.

Fish Monitor
  • 4,155
  • 3
  • 32
  • 53

4 Answers4

93

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
Community
  • 1
  • 1
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • Does not work: `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?`. – Fish Monitor Jun 16 '15 at 08:54
  • @fossilet Can you check if `git fetch upstream master:branch_name` works for you? Check http://stackoverflow.com/a/1614996/1860929 – Anshul Goyal Jun 17 '15 at 16:48
  • @fossilet Not sure why. Had tried googling this earlier, but couldn;t figure out. Might be a bug with the git version – Anshul Goyal Jun 18 '15 at 08:16
  • How do I push the branch I made tracking upstream back to origin? – tnrich Feb 25 '20 at 16:34
  • I got `fatal: 'upstream/develop' is not a commit and a branch 'develop' cannot be created from it` – Michael Fulton Nov 23 '21 at 17:06
9

If you just added the remote, you'll need to fetch it so that Git knows which branches are available:

git fetch upstream master

After this you can do

git checkout upstream/master

without any issues.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • 1
    Would this incorporate changes from upstream/master to origin/master? This isn't wanted. – Fish Monitor Jun 16 '15 at 08:40
  • 2
    Sorry, I meant `fetch` not `pull`. Thanks. – nneonneo Jun 16 '15 at 08:41
  • After the fetch? That's surprising, since I definitely just tried this on my `git` and it works fine...can you post what steps you're using? – nneonneo Jun 16 '15 at 08:56
  • 6
    Yes, you can checkout `upstream/master`, but you will be in 'detached head' state. If you want it to behave like you expect a branch to behave, you have to make it a tracking branch like @mu wrote. – xtofl Sep 26 '18 at 06:43
2

In more concise way (I'm using git 2.28), you can say

git fetch upstream

and then

git checkout -b <branch_name> --guess

where the --guess flag checks if a branch corresponding to <branch_name> exists on any of the remotes and tracks the corresponding remote (docs here).

charlie80
  • 806
  • 1
  • 7
  • 17
-1

Please follow the below steps,

Step 01: add new remote using below add command

git remote add testRemote https://repourl.git

Step 02: set the url as remote

git remote set-url testRemote https://repourl.git

Step 03: to pull the branches from new repo

git pull

Step 04: switch to new branch

git checkout branchFromNewremote

Then you can create new branch from the current branch

  • I'm not sure this will work: can you really add a new remote also called `origin`? does `git pull` without an argument really pull from the new remote? And in your `git checkout` command, OP was getting an error doing `git checkout upstream/master`, can you explain how you avoid that? – joanis Oct 20 '21 at 18:26
  • your are right it wont work if we have already remote name as origin. Sorry there is a typo just corrected. Since we are setting new remote with "git remote set-url" git pull will try to pull all the branches from new remote and will help to switch to new branch from testRemote. – Vasanthakumar Jagannathan Oct 21 '21 at 05:43
  • Thanks for fixing the typo. For the `git pull`, I'm still not convinced: in my experience, it pulls only from the default remote, which is going to be origin if you're one a branch tracking something origin when you run it. I just tested your instructions and `git pull` answered "Already up to date." You really need `git fetch testRemote` instead, like in the accepted answer. – joanis Oct 21 '21 at 12:44
  • To be honest, and sorry if I sound mean, but I'm not sure what you're trying to add to the accepted answer. I appreciate your efforts, but it's important to test solutions you propose here, and make sure new answers to old questions add something novel, or simpler, or better in some way that you can explain in your answer. – joanis Oct 21 '21 at 12:48
  • once you add new remote you must switch to new remote using set-url, this will point to your new remote. Post that if you want to checkout new branch under current remote pls do a git pull to fetch all the branches from current remote. You cant see new branches under current remote until you do a git pull. I have tested above scenario before I post, it worked for me :) – Vasanthakumar Jagannathan Oct 21 '21 at 14:01