Using GitHub to Share with SparkFun

Contributors: SFUptownMaker
Favorited Favorite 12

Syncing with the Original Repository

Syncing is the process of getting changes from the original remote repository, the one you forked from in the first place, and bringing them into your local repository so that you can ensure that you're making changes on the most up-to-date version of the original. Since you don't have access to change the directory on the SparkFun GitHub page, like I do, you'll have to just follow along--without any changes to the original repository, a sync is meaningless.

I made a small change to one of the files in the repository, so now we've got a fairly common scenario: most of the repository matches, but I have a new file I want to send to SparkFun, and SparkFun has some changes that I need to include.

Adding the Remote Repository

Before you can pull it to your local repository, you need to add the original repository's information to your local repository. Here's how to do that:

Adding a remote

  • git remote add upstream - add a remote endpoint to grab code differences from.
  • git remote -v - you can see here that we now have two different remotes to interface: our personal copy and SparkFun's copy. This will allow us to keep our copy up-to-date with SparkFun's in much the same way as we do with our own remote repository.

Fetching a branch

  • git fetch upstream - fetch works like pull, except it creates a new branch for the incoming data rather than attempting to merge it immediately. This is a "safe" operation; you'll need to manually merge the incoming data.
  • git branch -va - the -va switch displays all local and remote branches. A branch is just what it sounds like--the change log branches off from the original, and changes to the branch are tracked independently of changes to other branches.

Merge and push

  • git merge upstream/master - merge attempts to reconcile differences between two branches, bringing the changes in the branch named (in this case "upstream/master") into the currently active branch (since we haven't changed it, "master"). For most simple changes, merge will complete automatically. If it doesn't, this website has information about integrating an external merge tool with git, which is probably a good idea, since git's internal merge tool leaves a lot to be desired.
  • git status - we're ahead of our remote again, since we pulled changes in from the master repository to our local repository. That means we need to...
  • git push - now our GitHub repository is up-to-date with the latest changes in the SparkFun master copy. But what about the changes we made? How do we get them to the SparkFun repository?

At this point, you have all the tools and knowledge you need to fork a repository, make and commit changes to its contents, keep it up-to-date with SparkFun's master copy, and push it to GitHub. Only one thing remains: submitting a pull request, to ask SparkFun to include your changes in our master repository.