Tuesday, February 9, 2016

Quick Tip: Synch a GitHub Fork via the Command Line

When you fork someone’s repository on GitHub, you’ll want to update your fork with any changes made to the original. There are various ways to do this. In this quick tip, Shaumik describes how to update your fork via the command line. Another option is to update your fork via the GitHub web interface.

Git is a distributed version control system, which means that each copy of a repository is complete with its own history. GitHub, on the other hand, is an online collection of Git repositories. GitHub introduces the concept of forking, which involves making a copy of the main repository.

GitHub Workflow

GitHub workflow

To understand the concept of updating a fork, one must first know why this is necessary.

An organization can’t grant every potential contributor write access to its main repository, so the public can only view the central repository. A fork is a copy of this repository that a user can create. Users have read and write access to their own forks.

Normally, programming happens on a local machine (or a VM) instead of the GitHub interface directly, so a clone of the fork will normally be created.

Once a contributor has made a commit to a local copy, it then needs to be pushed to the fork on GitHub (which is possible due to the write access). Then, a pull request is created from the fork to the central repository.

Keeping Your Fork Up to Date

When the central repository is updated with someone else’s code (after the fork was created), these new commits do not magically appear on the fork. One must first download and merge these changes with the local repository, and then push it to the fork.

For legacy reasons, in our local repository, we name the central repository remote as upstream and the fork as origin.

Ideally, you should never make any commits directly to the master branch of your fork or the local repository. This branch must only be used for keeping the updated code from upstream. All changes must be made to new feature or bug branches, and pushed to the branches with the same name on the fork.

Hence, the following steps help in updating the fork with the latest commits from the central repository:

  • Pull from upstream’s master branch to local repository’s master
  • Push from local repository’s master to fork’s master

These steps assume that you have forked the repository and cloned the fork on your local machine.

For demonstration, we’ll be using the repository of e-Cidadania on GitHub.

Continue reading %Quick Tip: Synch a GitHub Fork via the Command Line%


by Shaumik Daityari via SitePoint

No comments:

Post a Comment