Fork And Sync A Forked Git Repository With The Original

A fork is a remote, server-side copy of a git repository that you manage. A fork is not really conceptually part of git. It is a feature most git hosting services (such as GitHub, GitLab and Bit Bucket) provide. It is majorly used in open source development.

github_fork_button.jpg Fork Button on Github

If you want to contribute to an existing project to which you don’t have push access or commit rights, you can fork the project.

When you fork a repository, for example an open source project that is actively maintained; the original repository is likely to evolve as other users commit changes to it. These changes do not appear in your fork automatically; you need to sync the fork in order to pull newer commits that was made on the repository after you forked.

Lets say you have cloned a copy of a forked repository on your GitHub account.

$ git clone https://github.com/[YOUR_USERNAME]/[YOUR_FORK].git

Now let's check the remote details of your local forked repository.

$ git remote
origin
$ git remote -v
  origin  https://github.com/[YOUR_USERNAME]/[YOUR_FORK].git (fetch)
  origin  https://github.com/[YOUR_USERNAME]/[YOUR_FORK].git (push)

You see that you have just one remote repository (origin) which is your forked git repository on github https://github.com/[YOUR_USERNAME]/[YOUR_FORK].git.

Syncing The Forked Git Repository With The Original

Let's consider using a GitHub repository.

  • First, you need to set up the remote repository that will be synced with the fork (that is the original repository). You often give the remote repository a descriptive name upstream. Run $ git remote add upstream <ORIGINAL_REPOSITORY_URL>
$ git remote add upstream https://github.com/[ORIGINAL_OWNER]/[ORIGINAL_REPOSITORY].git

You can now see that remote upstream has been added.

$ git remote -v
  origin    https://github.com/[YOUR_USERNAME]/[YOUR_FORK].git (fetch)
  origin    https://github.com/[YOUR_USERNAME]/[YOUR_FORK].git (push)
  upstream  https://github.com/[ORIGINAL_OWNER]/[ORIGINAL_REPOSITORY].git (fetch)
  upstream  https://github.com/[ORIGINAL_OWNER]/[ORIGINAL_REPOSITORY].git (push)
  • You need to get your forked repository in sync with the original repository by fetching all the commits, branches and all metadata from the original repository.
$ git fetch upstream

And be sure to be on the master of your forked local repository.

$ git checkout master
  Switched to branch 'master'
  • Merge commits from the HEAD branch usually master of original repository to your local forked repository.
$ git merge upstream/master

NOTE You don't have to merge changes in all branches from upstream since you majorly want to work a branch.

  • Make your contribution - I typically advice you create a new branch for the feature you want to add. You would do this by branching out from the master branch.
$ git checkout -b [YOUR_NEW_BRANCH]

Don't commit directly to the master branch

Obviously, when done with your contributions, you commit and push changes to your remote forked repository (origin).

  • Finally, Head over to the original upstream remote repository (in this case GitHub) and submit a Pull Request (PR) to merge commits from the [NEW_BRANCH] on your forked repository.

See GitHub specific workflow below:

pull-request-start-review-button.png Pull Request Button on Github

compare-across-forks-link.png Compare Across Forks Link

choose-base-fork-and-branch.png Choose Original Repository And Branch

choose-head-fork-compare-branch.png Choose Head Fork Compare Branch

Visit Github Help Page for creating a PR from a forked repository for GitHub specific workflow.

No Comments Yet