'How to Update a Github fork, including new branches upstream?

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.



Solution 1:[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).

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 torek