What do you do when you have a custom fork of a repository, and the original version has some new update that you’d like to include in your fork without losing your customizations? It’s a pretty simple process if you just follow the steps below.
Add the original repo
First, you need to add the parent repo as a second remote for your local fork. You could name this remote after the original owner (ex: torspark), but I like to name it “parent” to keep things simple.
git remote add parent <url-to-parent-repo>
For example, I recently made a couple customizations to the middleman-spellcheck gem. This started as a pull request to fix a bug, but then I added a little extra functionality while I was digging into the code. Anyway, after my pull request was accepted, I wanted to update my fork to be based on the current version. Here’s what I typed into my terminal:
git remote add parent firstname.lastname@example.org:minivan/middleman-spellcheck.git
Verify that your remote was added by running:
Fetch the original repo
Next, you can fetch all the changes from the original repo by typing:
git fetch parent
This will give you a new branch called parent/master. If you’re looking for a specific branch on the parent repository, the following command with list all branches available, including both those from your fork and the parent repo.
git branch -a
Merge or Rebase
Finally, you just have to either merge or rebase your branch with the parent repo. If you prefer to merge, checkout your branch, and merge the branch you want from the parent repo.
git checkout master git merge parent/master
Or, if you’re a fan of rebasing code, you can run this instead:
git rebase parent/master
I was the only one using my fork at the time, so I chose to rebase in order to have a cleaner version history.
That’s all there is to it. The features I added have since been pulled into the original repo, so you can see that my fork matches the original repo (as of version 0.7.5). This process will make it easy for me to continue adding new feature branches to my fork while staying current with the original repository as more pull requests are accepted.
What repositories have you contributed to lately? I’d love to hear about it.