1

Here is my scenario: I have a Forked Repo (say F) form original repo on GitHub (say O).

My local copy of Forked repo is L.

As I am working with L, I have 1 commit locally which is not yet pushed to F and some untracked changes for issue which I am working currently.

While this is happening, the original repo O gets updated. Now I will have to sync F and L with O.

I followed the steps as given here, i.e. these to be more precise:

# Add the remote, call it "upstream":
git remote add upstream https://github.com/whoever/whatever.git

# Fetch all the branches of that remote into remote-tracking branches,
# such as upstream/master:
git fetch upstream

# Make sure that you're on your master branch:
git checkout master

# Rewrite your master branch so that any commits of yours that
# aren't already in upstream/master are replayed on top of that
# other branch:
git rebase upstream/master

After doing this, I don't see any change in F on GitHub. There were 4 branches in O, but after following above steps, I don't see any of those new branches in F.

Then I thought that perhaps I need to push changes to F, so I did a git push origin master. My commits are visible in F as usual, but I don't see any of the new branches in F. So perhaps F did not get synchronized with O.

So what am I missing and how do I do that?

Community
  • 1
  • 1
rahulserver
  • 10,411
  • 24
  • 90
  • 164

1 Answers1

1

Only the branches you push to the repo are visible in F. Since you only pushed the master branch, only that will be visible in F.

If you want to see all of the branches from O in F, you have to checkout each branch and then push it to F.

Example for a branch called develop:

git checkout develop
git push origin develop

Do this for all of the branches from O that you want to see in F.

If you run git branch -a locally, it will show all of the local and remote branches. You will notice that the branches from O are only shown in the form remotes/upstream/develop, and there's no local branch for that. The git fetch command that you ran earlier has pulled in all of the remote branches, but has not created local branches for them.

The above commands first create a local branch from the upstream branch and then push them to your own remote.

One more thing: Only use rebase if you have not pushed your branch to your remote. Rebasing more than once and pushing in-between will change the history of your branch, and you will have to force push your changes. As long as you're the only one using your repo, that's not a problem, but there will be issues when other people have already pulled your changes. Rebasing and force-pushing your changes after that will cause problems for the others using your repo.

Instead of rebasing, you could merge in this case. I use this alias for updating my fork with an upstream remote:

alias gmu='git fetch origin -v; git fetch upstream -v; git merge upstream/master'
nwinkler
  • 52,665
  • 21
  • 154
  • 168
  • Hey, thanks for the answer. But shouldn't the command git fetch upstream bring in all the remote branches to L and thus the push should push the branch to F? – rahulserver Jul 10 '15 at 08:46
  • When you push, you only push the current branch (or the one you specify in the push command). It will not create local branches for you automatically. You still have to create the local branch first and then push it. – nwinkler Jul 10 '15 at 08:51