22

I've forked a repo and all of my work goes into that fork (my origin) and I merge branches upstream with pull requests. Pretty standard.

But now there's a new branch in the upstream repo and I can't quite figure out how to get that new branch locally and then push it to my origin. Here is my situation.

$ git remote show origin
* remote origin
  Fetch URL: git@github.com:rackspace/jclouds.git
  Push  URL: git@github.com:rackspace/jclouds.git
  HEAD branch: master
  Remote branches:
    1.5.x                   tracked
    master                  tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

$ git remote show upstream
* remote upstream
  Fetch URL: https://github.com/jclouds/jclouds
  Push  URL: https://github.com/jclouds/jclouds
  HEAD branch: master
  Remote branch:
    master tracked
  Local ref configured for 'git push':
    master pushes to master (up to date)

I know that there is a 1.6.x branch in jclouds/jclouds and I want to get that branch locally and then push it to rackspace/jclouds. I've tried this command

$ git fetch upstream 1.6.x
From https://github.com/jclouds/jclouds
 * branch            1.6.x      -> FETCH_HEAD

And it looks like it's fetched the branch but I don't see it in git remote show or git branch -a so I'm unable to setup a local tracking branch.

What am I missing?

Everett Toews
  • 10,337
  • 10
  • 44
  • 45

1 Answers1

35

This should be enough

# I prefer fetching everything from upstream
git fetch upstream

# Then I track the new remote branch with a local branch
git checkout -b 1.6.x --track upstream/1.6.x
git push origin 1.6.x

If there are update issues like:

fatal: Cannot update paths and switch to branch '1.6.x' at the same time. 
Did you intend to checkout 'upstream/1.6.x' which can not be resolved as commit?"

And if this doesn't work either:

git checkout upstream/1.6.x -b 1.6.x

Then a simpler version is:

# let's create a new local branch first
git checkout -b 1.6.x
# then reset its starting point
git reset --hard upstream/1.6.x

What the OP Everett Toews has to do in his case was:

Ultimately I had to explicitly add the upstream branch with

git remote add --track 1.6.x upstream-1.6.x https://github.com/jclouds/jclouds 

and then:

git pull upstream-1.6.x 1.6.x
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I did the git fetch but when I try the git checkout I get the error "fatal: Cannot update paths and switch to branch '1.6.x' at the same time. Did you intend to checkout 'upstream/1.6.x' which can not be resolved as commit?" – Everett Toews Mar 31 '13 at 16:16
  • @EverettToews would a `git checkout upstream/1.6.x -b 1.6.x` work better? – VonC Mar 31 '13 at 17:04
  • @EverettToews if that doesn't work, I have added in the answer a 'reset' option for you to try. – VonC Mar 31 '13 at 17:08
  • The `git checkout upstream/1.6.x -b 1.6.x` gives me the same fatal error as above. Naturally for `git checkout 1.6.x` I get "error: pathspec '1.6.x' did not match any file(s) known to git." – Everett Toews Mar 31 '13 at 22:16
  • @EverettToews sorry, I forgot the `-b`: `git checkout -b 1.6.x`, then `git reset`. – VonC Mar 31 '13 at 22:19
  • That didn't work for me either. Same fatal error. Ultimately I had to explicitly add the upstream branch with `git remote add --track 1.6.x upstream-1.6.x https://github.com/jclouds/jclouds` and then `git pull upstream-1.6.x 1.6.x`. Thanks a lot for your help anyway. – Everett Toews Apr 05 '13 at 14:07
  • @EverettToews ok. I have included your version in the answer for more visibility. – VonC Apr 05 '13 at 14:09
  • I had to resort the last two commands (remote add and pull) to fix my problem. – kakyo Nov 07 '13 at 19:44