Add a new branch from upstream repository

Summary

I’ve forked Marlin to adjust the configuration and persist it in my own repository. Now Marlin released version 2.1.

Naturally, I wanted to use 2.1 as well. But the new branch was not available on my fork :-(

  • I have branch 2.0.x in my repo and cloned locally
  • I want branch 2.1.x from the Marlin repository cloned locally and pushed to my fork

Add new branch from original repository

Get the status quo

Let’s see what we have in a local clone of my repo:

> git branch -a
* 2.0.x
  remotes/origin/1.0.x
  remotes/origin/1.1.x
  remotes/origin/2.0.x
  remotes/origin/HEAD -> origin/2.0.x
  remotes/origin/Marlin_RTOS
  remotes/origin/bugfix-1.1.x
  remotes/origin/bugfix-2.0.x

OK. I am on 2.0.x and am lacking 2.1.x, which I want. You can also see, that I am missing the upstream link on this clone. The only branches we see, are from my fork (origin).

Add Marlin repository as upstream

My local clone needs to know about the upstream repository. See How to set Git Upstream For a Respository and a Branch for more details.

> git remote add upstream https://github.com/MarlinFirmware/Marlin.git

Locally, the upstream branches are not available after adding the upstream.

> git branch -a
* 2.0.x
  remotes/origin/2.0.x
  remotes/origin/HEAD -> origin/2.0.x
  remotes/origin/bugfix-2.0.x

So let’s update the branches. See When does Git refresh the list of remote branches? for more information.

> git remote update upstream --prune
Fetching upstream
remote: Enumerating objects: 8711, done.
remote: Counting objects: 100% (2390/2390), done.
remote: Compressing objects: 100% (62/62), done.
remote: Total 8711 (delta 2345), reused 2356 (delta 2328), pack-reused 6321
Receiving objects: 100% (8711/8711), 4.74 MiB | 4.50 MiB/s, done.
Resolving deltas: 100% (6509/6509), completed with 785 local objects.
From https://github.com/MarlinFirmware/Marlin
 * [new branch]            1.0.x        -> upstream/1.0.x
 * [new branch]            1.1.x        -> upstream/1.1.x
 * [new branch]            2.0.x        -> upstream/2.0.x
 * [new branch]            2.1.x        -> upstream/2.1.x
 * [new branch]            bugfix-1.1.x -> upstream/bugfix-1.1.x
 * [new branch]            bugfix-2.0.x -> upstream/bugfix-2.0.x
 * [new branch]            bugfix-2.1.x -> upstream/bugfix-2.1.x
 * [new tag]               2.0.9.2      -> 2.0.9.2
 * [new tag]               2.0.9.3      -> 2.0.9.3
 * [new tag]               2.0.9.4      -> 2.0.9.4
 * [new tag]               2.1          -> 2.1

Cool. Now the new branch is available locally.

Select the new branch

Locally the branch is now available, but not selected. Switching the local branch is done by:

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

Add branch to origin

Now that the new branch 2.1.x is available locally, we can just push it to our fork. This makes it available in our repository and it still knows where it comes from ;-)

> git push -u origin 2.1.x
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for '2.1.x' on GitHub by visiting:
remote:      https://github.com/ReneHezser/Marlin/pull/new/2.1.x
remote:
To https://github.com/ReneHezser/Marlin.git
 * [new branch]            2.1.x -> 2.1.x
branch '2.1.x' set up to track 'origin/2.1.x'.

forked branch

Summary

In order to add a new branch from a fored repository, you need to:

  1. Add the originally forked repository as upstream
  2. Update upstream branches in local repository
  3. Push the branch to your repository