GitHub – Syncing Forked Repositories

Like it or not, GitHub is the de fato home for most open source projects today, despite lacking in features compared to other Git repository storage options like BitBucket. This means you miss out on a simple yet essential feature like fork syncing. When you fork somebody else’s repository, BitBucket allows an options called fork syncing. When enabled all commits from the original (call this upstream) are automatically synced to your BitBucket repository (call this origin).

On GitHub, you will have to manually sync the repositories. But before that let discuss some terminology.

  • upstream : The repository where you forked your repository from. This is not your repository.
  • origin : This is your forked repository. It exists on GitHub.
  • local (repository) : This exists on your local system, i.e the one you cloned. This is where you make your changes.

Now that we have terminology out of the way, let’s get started.

 

Fork Syncing

  1. To simplify things, its best if you don’t commit your local changes (if any) just yet. This will save you the hassle of dealing with conflicts later. If you have local changes, run

    This will save your local changes, which can be recovered later. Your workspace should now be clean.
  2. If necessary run the command

    You can change ‘master’ to which ever branch you’re want.
  3. You know where you forked your repository from, but Git does not. So lets tell Git where the upstream is located.
  4. Now that we have defined the upstream, lets fetch all commits that the upstream has but you don’t.

    You now have the changes stored locally, but these are not a part of your local history. If you do a git log, you will see that nothing has changed. The commits you fetched have been stored elsewhere.
  5. At this point, you have two options – merge or rebase. Since there are no local changes (as the stash command was run) its safe to merge.

    ‘master’ here refers to the branch you want to get commits from. You can do this with any branch that exists in the upstream repo. The resulting merge will be a fast-forward merge. This is because you do not have local commits or change to cause a conflict.
  6. Now your local repo has the latest commits, but the origin (the one on GitHub) does not. This can be easily fixed by running

    This will update your origin. So both your local and origin now are in sync with the upstream repo.
  7. Finally if you had previously stashed any changes, you can undo it by running

Adarsh

Howdy! You're reading my blog. To know more about me click here

Leave a Reply

Your email address will not be published. Required fields are marked *