Syncing Github Forks

26 Nov

GitHub_LogoGithub has become the place to manage source code—it’s free, robust, and commonly accessible. Github repositories are owned and access to change them must be enabled by the owner. Still, you can make changes to a Github repository; but a “pull request” must be sent to contributors of the repository and they can accept the change at their discretion. Keeping changes in sync when you don’t have direct change privileges may not be obvious.

One-time Github Changes

If you are making a one-time change to one file, you can make it through the Github website, editing the file and submitting the change to the owners as a “pull request”. This will create a branch off their repository, commit the change, and submit a “pull request” to the owners. You can check the branch out to sync those changes to your local directory. If they accept the change, they will merge the change into their main branch and you can continue updating local changes from the main branch.

  1. Go to the repository’s github page.
  2. Select, edit, and submit the sole file change.
  3. “Pull” the changes to sync the new branch with your working directory.

The Github site does not allow you to batch changes to more than one file into one “commit” change (each file changed is submitted into a separate commit). So, if you are making more than one change, then it is best to work off your own fork, make all your changes, then submit the change back to the original repository owner.

Owning and Managing Changes via a Fork

Alternatively, you can “fork” a copy of the repository to your own account. Then you can use standard git commands to make changes as necessary. Then, you can use the github site to send changes back to the parent repository as a “pull request”. This allows you to change and validate several changes at once before having the owners integrate those changes.

  1. Fork repository into your own Github account from the repository’s Github webpage.
  2. Commit and push changes to the fork as necessary.
  3. To provide changes back to the parent repository, use the fork’s “pull request” feature to send the changes back for consideration.

Synching Fork from Parent Repository

If you plan to make ongoing changes, it might be useful to request to become a contributor to the parent repository. Until that happens, you can continue to make changes on your own fork. As changes are made to the parent repository, it will become out of sync with your own. In order to keep them in sync, if you want to make ongoing changes to your own coy (infrequently). Unfortunately, Github doesn’t provide a feature to do this on their website, but the Github article, “Syncing a fork,” on the topic describes the following:

  1. Add the parent repository as a remote named “upstream”:
    git remote add upstream
  2. Get changes from parent repository and merge into local branch:
    git fetch upstream         ## Get any parent's changes
    git checkout master        ## Make sure we are at origin/master
    git rebase upstream/master ## Supplant the current state with that of the parent
    git push origin            ## Update repository
  3. Or, rather than the “rebase” command, you can merge the parent’s changes with your own. Later, you can submit your simultaneous changes as a pull request back to the parent repository.
    git merge upstream/master  ## Merge changes from parent
    git push origin            ## Update repository
I’ll update this with more images when I do this again and can validate the steps that I’ve documented from memory.

Tags: , , , ,