'Github: Import upstream branch into fork
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.
Solution 1:[1]
I would use
git checkout -b <new_branch> upstream/<new_branch>
Solution 2:[2]
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 = [email protected]:somebodys/repo.git
fetch = +refs/heads/master:refs/remotes/upstream/master
Editing .git/config as follows fixed my problem
[remote "somebody"]
url = [email protected]:somebodys/repo.git
fetch = +refs/heads/*:refs/remotes/upstream/*
Solution 3:[3]
The following steps worked well for me (assuming the upstream branch name is branch):
$ git fetch upstream
$ git checkout branch
$ git push origin
Solution 4:[4]
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 = [email protected]:<your_user/org>/<repo>.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
rebase = true
[remote "upstream"]
url = [email protected]:<upstream_user>/<repo>.git
fetch = +refs/heads/*:refs/remotes/upstream/*
[remote "other_user"]
url = [email protected]:<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>
Solution 5:[5]
--track?
git branch --track branch upstream/branch
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Svenito |
| Solution 2 | Ian Will |
| Solution 3 | Eneko Alonso |
| Solution 4 | SherylHohman |
| Solution 5 | troelskn |
