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.
- 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.
- If necessary run the command
1git checkout master
You can change ‘master’ to which ever branch you’re want.
- You know where you forked your repository from, but Git does not. So lets tell Git where the upstream is located.
1git remote add upstream https://github.com/the_other_user/repo_name
- Now that we have defined the upstream, lets fetch all commits that the upstream has but you don’t.
1git fetch upstream
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.
- 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.
1git merge upstream/master
‘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.
- 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.
- Finally if you had previously stashed any changes, you can undo it by running
1git stash pop