226

I have a fork (origin) from a project (upstream) on github. Now the upstream project has added a new branch, I want to import into my fork. How do I do that?

I tried checking out the remote and creating a branch on top of that, but that configures the branch the way that git push is trying to push to the upstream:

git checkout upstream/branch
git checkout -b branch

edit

Maybe that wasn't clear, but I want to add the branch to my local repository, so I can push it to origin (my fork) via git push. Because upstream repositories are usually read-only and you fork it to contribute.

So I basically want to checkout a non-existent branch on origin whose contents will be pulled in from upstream.

simleo
  • 2,775
  • 22
  • 23
poke
  • 369,085
  • 72
  • 557
  • 602
  • Does this answer your question? [How do I check out a remote Git branch?](https://stackoverflow.com/questions/1783405/how-do-i-check-out-a-remote-git-branch) – Michael Freidgeim Sep 01 '21 at 20:11
  • @MichaelFreidgeim My question was basically asking about the `-u` option, when dealing with *multiple* remotes. The accepted answer covers in this question covers that just fine. – poke Sep 02 '21 at 22:28
  • Sorry for the automatically created unintentional question - the comment should be read as [“Possible duplicate”](https://meta.stackexchange.com/questions/339563/the-auto-comment-does-this-answer-your-question-generated-when-voting-to-clos) – Michael Freidgeim Sep 03 '21 at 01:35

7 Answers7

345
  1. Make sure you've pulled the new upstream branch into your local repo:

    • First, ensure your working tree is clean (commit/stash/revert any changes)
    • Then, git fetch upstream to retrieve the new upstream branch
  2. Create and switch to a local version of the new upstream branch (newbranch):

    • git checkout -b newbranch upstream/newbranch
  3. When you're ready to push the new branch to origin:

    • git push -u origin newbranch

The -u switch sets up tracking to the specified remote (in this example, origin)

urschrei
  • 25,123
  • 12
  • 43
  • 84
  • 6
    I believe `git fetch upstream` is a better option at the first step, since `git pull upstream` requires more actions to be done after `git remote add ...` for the `upstream`. – Alexander Pavlov Sep 18 '12 at 10:13
  • git pull upstream returns: `You asked to pull from the remote 'upstream', but did not specify a branch. Because this is not the default configured remote for your current branch, you must specify a branch on the command line.` Adding the branch name at the end of the command causes a merge between the branch in the upstream with the current local branch instead of allowing the creation of a new local branch. Any ideas? – mMontu Sep 06 '13 at 12:14
  • 1
    Replacing `git pull upstream` with `git fetch upstream` solved the problem, and the following steps worked. – mMontu Sep 06 '13 at 16:30
  • I get: fatal: Cannot update paths and switch to branch 'upstream' at the same time. Did you intend to checkout 'upstream/master' which can not be resolved as commit? – sureshvv Apr 12 '15 at 07:40
  • 2
    @sureshvv That's probably because you need to have a remote reference to the upstream repository named **upstream** before doing any of that. In case you don't, that's how you add one: **git remote add upstream your_git_upstream_repository_url.git**. [Read this](http://stackoverflow.com/a/9257901/4905310) if you need clarification on that matter. – Gabriel Rainha Nov 25 '15 at 12:56
  • @sureshvv Or you made a typo in the branch name under upstream/ - the branch `upstream/foo` wasn't found. – MatsLindh Mar 25 '16 at 10:59
  • The `git checkout -b branchname upstream/branchname` doesn't work if I have a `/` in the branch name. For example, if the upstream has a branch named `feature/xyz` and I run `git checkout -b feature/xyz upstream/feature/xyz`, I get an error: `fatal: A branch named 'feature/logger' already exists.` Why is this? It works with branch names that don't have `/` in the name. – Nxt3 Nov 09 '17 at 20:44
  • Worth noting is this guide, for when you need to _update_ the upstream branch into a fork: https://help.github.com/en/articles/merging-an-upstream-repository-into-your-fork – f055 Apr 05 '19 at 08:17
11

I had trouble with this too, and google took me here. The solutions didn't work however. My problem was that when i added my upstream, it set up my git config to only fetch master, rather than all branches. e.g. It looked like this

[remote "somebody"]
        url = git@github.com:somebodys/repo.git
        fetch = +refs/heads/master:refs/remotes/upstream/master

Editing .git/config as follows fixed my problem

[remote "somebody"]
        url = git@github.com:somebodys/repo.git
        fetch = +refs/heads/*:refs/remotes/upstream/*
Ian Will
  • 1,042
  • 11
  • 14
9

I would use

git checkout -b <new_branch> upstream/<new_branch>
Svenito
  • 510
  • 4
  • 6
  • That's actually what I tried even before what I have explained in the question; it leads to the same results. – poke Dec 10 '10 at 15:43
8

From the UI:

In your fork go to Branches, click 'New Branch'. There you would select the source - either your fork or upstream. Select upstream and select the branch that you want to 'import'. Create the branch with the same name. Done.

Venkatesh MC
  • 111
  • 1
  • 3
2

The following steps worked well for me (assuming the upstream branch name is branch):

$ git fetch upstream
$ git checkout branch
$ git push origin
Eneko Alonso
  • 18,884
  • 9
  • 62
  • 84
  • 2
    I get git fetch upstream fatal: 'upstream' does not appear to be a git repository fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. – ThinkDigital Sep 17 '18 at 14:55
  • @ThinkDigital I got the same error, so git fetch simply works! Also, +1 for the comment in parentheses! – Mahsan Nourani Mar 07 '21 at 02:16
  • After I try git checkout on the new upstream branch, `git status` reveals that i'm still on my old branch and nothing happened. – sh37211 Jun 04 '22 at 18:05
2

I had a slightly more complicated scenario where I already had an upstream defined in my fork (from the canonical repo) but needed to checkout a branch from a different fork. To get that done, the process is slightly different. Here's the config I ended up with:

[remote "origin"]
url = git@github.com:<your_user/org>/<repo>.git
fetch = +refs/heads/*:refs/remotes/origin/*

[branch "master"]
remote = origin
merge = refs/heads/master
rebase = true

[remote "upstream"]
url = git@github.com:<upstream_user>/<repo>.git
fetch = +refs/heads/*:refs/remotes/upstream/*

[remote "other_user"]
url = git@github.com:<other_user>/<repo>.git
fetch = +refs/heads/*:refs/remotes/<other_user>/*

Now you can checkout a branch from <other_user> fork as well.

git fetch <other_user> <branch>
git checkout -b <branch> <other_user>/<branch>

That will give you a local branch which is derived from the <other_user> fork.

To push that local branch I had to be specific with my push command.

git push origin <branch>
SherylHohman
  • 16,580
  • 17
  • 88
  • 94
okor
  • 666
  • 8
  • 13
  • 1
    That’s pretty much the same as the accepted answer, just that the remote you are fetching from is not called “upstream”. – poke Sep 12 '19 at 23:43
0

--track?

git branch --track branch upstream/branch
troelskn
  • 115,121
  • 27
  • 131
  • 155