Git is an open source distributed version control system, initially designed and developed by Linus Torvalds for Linux kernel development in 2005. For may years, centralized version control system (like CVS and Subversion) were very popular to Open Source developers community. At the same time, distributed version control systems become more and more popular. Github founded in February 2008, was one more reason why Git really took off. GitHub provides free hosting for Open Source projects with unlimited collaborators (as well as commercial plans for private repositories).
There are mainly two ways to collaborate using Github.
- the shared repository model
- the fork and pull model
When you create a repository in Github, even a public repository, only you have “push” access to it. To allow other people to “push” to this repository you need to add them as Collaborators (they need to have a Github account, even free). This is basically the “shared repository model”, which is a good solution for small teams, where members know and trust each other.
However, this solution cannot be applied to large teams, where everybody has the right to contribute (the so-called “social coding”). In this case, the “fork and pull model” is the suitable solution. Any Github member can “fork” a public repository. Forking the original repository, another repository is created (the forked repository). You can apply any modification to this one. Changes will not affect the original repository, until you submit a Pull request. In case your pull request will be accepted by original repository owner, your changes will be merged to original repository.
Since two years, BitBucket offers a similar service with Github. The difference is that BitBucket allows unlimited repositories for free (even private), but only for five collaborators. For more than five users, there are paid plans.
Here is a real example of “fork and pull model”. Even I have an active Github account, I will describe all the steps for someone who just starts with Git and Github. I will fork a public repository. Changes made to forked repository will be merged to original one after a pull request. Here is how.
You can easily setup Git in any operating system. Detailed instructions are available at http://git-scm.com/downloads
Getting started instructions: http://git-scm.com/book/en/Getting-Started-Git-Basics
An excellent interactive tutorial is available here: Try Git
Create a Github account
If you do not already have a Github account, create one for free. Just go to: https://github.com
In this step I will fork the public repository named “CodeHouse” at https://github.com/phpdevelopersindia/CodeHouse, just pressing the fork button:
After I forked this repo, this will appear to my Github profile as a forked repo: https://github.com/pontikis/CodeHouse
As I want to receive email notifications for changes in CodeHouse repository https://github.com/phpdevelopersindia/CodeHouse, I press the watch button to Watch the original repo:
Clone forked repository
In order to get forked repository contents to my hard disk, I have to clone the forked repo (this process will be performed once):
cd /srv/http/dev git clone firstname.lastname@example.org:pontikis/CodeHouse.git
This will create a directory CodeHouse.
With the following command:
git remote -v
I get the remote repositories of local repo:
origin email@example.com:pontikis/CodeHouse.git (fetch) origin firstname.lastname@example.org:pontikis/CodeHouse.git (push)
In order to be able to get original repository contents (not only the forked one), I add it as a remote repository, as follows:
git remote add upstream email@example.com:phpdevelopersindia/CodeHouse.git
(where upstream points to original repo)
Now, giving the command:
git remote -v
I get the following results:
origin firstname.lastname@example.org:pontikis/CodeHouse.git (fetch) origin email@example.com:pontikis/CodeHouse.git (push) upstream firstname.lastname@example.org:phpdevelopersindia/CodeHouse.git (fetch) upstream email@example.com:phpdevelopersindia/CodeHouse.git (push)
Update original repository
In order to update the contents of the original repository to my hard disk:
cd /srv/http/dev/CodeHouse git fetch upstream git merge upstream/master
cd /srv/http/dev/CodeHouse git push origin master
These changes will not affect the original repository, until I submit a Pull request:
From the forked repository https://github.com/pontikis/CodeHouse and using Github web interface, I select the “branch” contains my changes (in this example only a master branch exists) and I press the “Pull request” button:
Then I submit my request. I write a subject and a short description and press “Send” button.
Now, I am just waiting for acceptance of my request from CodeHouse repository owner.
Merge Pull requests
Original Repository https://github.com/phpdevelopersindia/CodeHouse owner can see pull requests as follows:
and can accept a request, as follows:
Finally, after pressing the “Merge” button, my changes are available to original repository.
Enjoy social coding!
We welcome your feedback. Leave us a comment.