12

Sometime ago I forked a git repo. Since then there has been several new commits to the master branch. https://help.github.com/articles/syncing-a-fork/ explains how to sync the upstream master branch with the local master branch and then push those commits to my fork.

However it does not mention how to sync new upstream branches. When I originally forked the project on github it looks like it brought over all of the then current branches.

I've seen explanations on how to sync one branch at a time. Is there any way to sync all of the branches new and old all at once?

I haven't made any commits to my fork. Thus I am looking for a fast forward sync.

Edit: Another way to say this:

I want

git ls-remote origin

to match

git ls-remote upstream

This would include branches and tags.

Gabriel
  • 1,679
  • 3
  • 16
  • 37
  • What are you trying to accomplish? You can have more than one remote repository when working with git, not sure you'd need to have every branch in your fork as well as the original repo. – Devon Bessemer May 07 '18 at 18:13
  • An archive of the upstream repo. Including all branches. – Gabriel May 07 '18 at 18:21

2 Answers2

11

However it does not mention how to sync new upstream branches

All you need to do is:

git fetch upstream

It supposes you have a remote repository referenced as upstream.
If not:

git remote add upstream <upstream_url>

That will fetch or update all the upstream branches and store them in your local repo, in the upstream namespace.

You can see those branches with:

git branch -avv

it it doesn't let me push all of the upstream branches to origin.

For that, you need to make a local branch based on any upstream branch you want to push to origin:

git branch abranch upstream/abranch
gut push -u origin abranch 

See more at "git - create local branch from existing remote branch".

To do so for all branches, see "Pull all branches from remote through it's mirror bare remote".
Or, better: "Track all remote git branches as local branches".
Simply use "upstream" instead of origin.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • This will fetch the `upstream` refspecs but it doesn't let me push all of the upstream branches to `origin`. – Gabriel May 08 '18 at 18:26
  • Is there any way to do this for all of the upstream branches all at once instead one at a time? – Gabriel May 08 '18 at 18:30
  • @Gabriel Is there anything missing to this answer? – VonC May 08 '18 at 20:12
  • 1
    Also, if you want to push new tags that you retrieved in the `git fetch upstream`, then execute `git push --tags`. – DavidH Oct 23 '20 at 19:04
  • @DavidH I agree. I also mention `--follow-tags` in https://stackoverflow.com/a/3745250/6309 and https://stackoverflow.com/a/4886153/6309. – VonC Oct 23 '20 at 19:06
  • When I run `git fetch upstream` it produces "fatal: 'upstream' does not appear to be a git repository". But adding it explicitly, as in `git remote add upstream ` fixes this. – sh37211 May 30 '22 at 20:44
  • 1
    @sh37211 Good point. I have edited the answer to make that clearer. – VonC May 30 '22 at 20:54
2

Do the following:

  1. List all your remote repositories to confirm if you have an upstream (parent/original) repository there or not:

    git remote -v

  2. If you don't see the original remote repository, add it by typing the command below. This is normally called 'upstream' unlike 'origin' which would be your forked remote repository.

    git remote add upstream <path of the original remote respository>

  3. Verify it's added as remote called 'upstream' and confirm it listed for both 'fetch' and 'push' operations.

    git remote -v

  4. Fetch the newly (after your fork) created branches locally:

    git fetch upstream

  5. List all the branches to confirm that you can see newly created branches

    git branch -a

Now you can locally check out the branch to work on it as needed.

Suyash
  • 190
  • 1
  • 10