How to keep your forked repositories in sync with the source repository
This blog post aims to answer the following questions.
- How to contribute to Open Source Projects (Partially)?
- How to keep forked repositories in sync with the source repository ?
- How to pull the commits from source repository to the forked repository?
I often forked open source repositories found in github and also commit back to the repositories whenever I feel to. However, this may seem obvious who has been doing these kind of thing. But for someone who is completely new this is kind of mess and it’s hard to figure out the right way to do it at first. So, I do not claim that the way I show here is right. I am just showing how I do it my way and would like to share it with others.
I have forked repository from github repo. I already have it cloned in my local repository. I had forked it long ago. So, when I get back to the forked repo, I get a message saying that My repository is 3 commits behind.
Now, before I can contribute to the main repo I have to first sync up my repo with the forked one. To do that I have to bring in all the commits that are missing from my repo, those commits are as follows:
- Add PHP School to intro FAQ
- Add Snappy library
- Add packages to work with screenshot APIs
I get inside my forked repository i.e. https://github.com/samundra/php-faq
php-faq
is the folder that I have my forked repo on my local machine. So I use
$ cd php-faq
to get inside the folder. Inside the folder as you can see below “git remote -v” displays the “origin” as the remote.
To be able to pull in changes, I will add another remote which is my forked repository. I use
git remote add wwphp https://github.com/wwphp-fb/php-resources
Now when I do
$ git remote -v
it will display origin and wwphp under remote list.
Now, I fetch all the meta information from the newly added repo i.e. wwphp. I am only interested in the master branch and thus fetching information from that particular branch only.
git fetch wwphp master
Now, to see the commits that my repository is missing I use
$ git log --oneline origin/master..wwphp/master --left-right
It basically lists out all the commits that are in wwphp/master branch but no in origin/master (my forked repository).
Just to confirm:
Now, to pull in the commits that my repo is missing I will use
$ git merge wwphp/master
To confirm that we have successfully pulled in the commits we can see the “git log” output. The above output message “Your branch is ahead of ‘origin/master’ by 3 commits.” are shown because I haven’t pushed them to my forked repo on github. Once I push the currently made changes to my github repo then this message will stop showing up.
Finally push all the newly introduced commits to my forked repository.
Now, the message has changed from “3 commits behind” to “branch is even” which means that we have successfully pulled in all the commits from remote repo to our forked repositories.
Note:
Instead of “git fetch wwphp master” we can also use “git fetch https://github.com/wwphp-fb/php-resources/ master” similarly we can use “git pull https://github.com/wwphp-fb/php-resources/ master”. However, I would suggest you use the fetch and then see the commits that will come in and whether you would like to merge or not.
Let me know if you have any confusions on this. I will try to answer them.
Happy forking 🙂