Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asanchezr/fd8f0fec142ff053c9d5332598a86a2b to your computer and use it in GitHub Desktop.
Save asanchezr/fd8f0fec142ff053c9d5332598a86a2b to your computer and use it in GitHub Desktop.
GitHub - Import upstream branch into fork

Problem

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

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. 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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment