3

This is not a duplicate of this, or anything else I could find on SO (and outside).

I created a github fork (let's say 'origin') from another repo (let's call it 'upstream'). Time passed and I want to have the upstream changes in origin. Today github has a nifty 'Fetch Upstream' button that updates branches that existed when I created my origin fork, and them alone.

The github docs list commands that are supposedly equivalent to the 'fetch upstream' UI:

$ git fetch upstream
...
$ git checkout main
...
$ git merge upstream/main

So the 'fetch upstream' UI doesn't try to update non-default branches? Let alone create new ones?

What is the right way to achieve this via command line? (I have origin and upstream setup as remotes, and did a fetch --all)

Is there a place where we can discuss this with the github devs themselves? I couldn't find the right repo (they have 400+). This looks to me like a worthwhile potential improvement.

Ofek Shilon
  • 14,734
  • 5
  • 67
  • 101
  • 1
    Would this work https://stackoverflow.com/a/12805604/1075282 ? – Renat Feb 14 '22 at 22:34
  • 1
    @Renat thank you, that thread has some useful info (especially the comments). It essentially says there is no out-of-the-box solution, right? The accepted answer says "fetch will not create local branches (which track remote branches), you have to do this manually" – Ofek Shilon Feb 14 '22 at 22:51

3 Answers3

2

If you have only one new branch to sync and you know its name, the really easy and quick way to do it is to create the branch manually in GitHub, then just hit 'sync fork' and it's done...

Olivier
  • 252
  • 2
  • 11
1

As you noted in a comment:

The accepted answer [to https://stackoverflow.com/q/6865302/1256452] says "fetch will not create local branches (which track remote branches), you have to do this manually"

which is true. However, there's no need to create branch names. You can run:

git fetch upstream

to create-or-update upstream/* names in your own repository locally (on your laptop for instance). Then, for each such name that you wish to create on your GitHub fork, you use the refspec refs/remotes/upstream/name:refs/heads/name to tell your own laptop Git software that the name-pair you wish to work with is upstream/name here—that's the remote-tracking name that git fetch upstream just created or updated—and your origin's branch name name.

These assembled refspecs go after git push origin or git push -f origin:

git push origin refs/remotes/upstream/foobranch:refs/heads/foobranch

which creates or updates branch foobranch in your GitHub fork (origin) based on the commits you got from the Git repository you're calling upstream.

Write yourself a small script to compute the right set of names based on whatever criteria apply to you, and use that script, and you're done. You can see those names with:

git fetch upstream

followed by:

git for-each-ref refs/remotes/upstream

(you'll need a lot more than just these two commands, but overall, writing such a script in sh/bash is not difficult).

torek
  • 448,244
  • 59
  • 642
  • 775
1

First, since Aug. 2022, the button is now called "Sync Fork":

Improved UI for syncing a fork

We updated the web UI to make keeping forks in sync with their upstream repositories more intuitive.
"Fetch upstream" has been renamed to "Sync fork", which better describes the button's behavior.

If the sync causes a conflict, the web UI prompts users to contribute their changes to the upstream, discard their changes, or resolve the conflict.

Image of sync fork button -- https://i0.wp.com/user-images.githubusercontent.com/90000203/186034529-e590fa61-481d-4bfd-859f-e0efa7272b4a.png?ssl=1

  • Read more about branches.
  • Read more about working with forks.

So the 'fetch upstream' UI doesn't try to update non-default branches? Let alone create new ones?

Second, this is an action done on GitHub side for the branch currently selected in your GitHub repository.
It applies to that one branch (be it the default one, or any other branch you happen to browse when you click the button.


The right way to replicate that locally is:

git fetch upstream
git switch aBranch
git merge upstream/aBranch

That would apply to any branch you are not working on, since working in a fork means, as a best practice, creating your own topic branch to isolate your development effort.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    This does not get any of the new branches that have been added to the original repo. It just updates the current branch. – benui Dec 18 '22 at 20:17