Forking a repository

Forking is a common concept for a developer; if you have already used a GNU-Linux based distribution, you know that there are some forefathers, such as Debian, and some derived distro, such as Ubuntu, also commonly called forks of the original distribution.

In GitHub things are similar. At some point, you find an interesting open-source project you want to modify slight to perfectly fit your needs; at the same time, you want to benefit from bug fixes and new features from the original project, keeping your work in touch. The right thing to do in this situation is to fork the project. But first, remember: fork is not a Git term, but GitHub terminology.

When you fork on GitHub, what you get is essentially a server-side clone of the repository on your GitHub account; if you clone your forked repository locally, in the remote list, you will find an origin that points to your account repository, while the original repository will assume the upstream alias (you have to add it manually anyway):

To better understand this feature, go to your GitHub account and try to fork a common GitHub repository called Spoon-Knife, made by GitHub mascot user octocat; so:

  1. Log in to your GitHub account
  2. Look for spoon-knife using the search textbox located up on the left of the page:
  3. Click on the first result, octocat/Spoon-Knife repository

 

  1. Fork the repository using the Fork button at the right of the page:
  1. After a funny photocopy animation, you will get a brand-new Spoon-Knife repository in your GitHub account:

Now, you can clone that repository locally, as we did before:

[1] ~
$ git clone https://github.com/fsantacroce/Spoon-Knife.git
Cloning into 'Spoon-Knife'...
remote: Counting objects: 19, done.
remote: Total 19 (delta 0), reused 0 (delta 0), pack-reused 19
Unpacking objects: 100% (19/19), done.

[2] ~
$ cd Spoon-Knife

[3] ~/Spoon-Knife (master)
$ git remote -v
origin  https://github.com/fsantacroce/Spoon-Knife.git (fetch)
origin  https://github.com/fsantacroce/Spoon-Knife.git (push)

  

As you can see, the upstream remote is not present, you have to add it manually; to add it, use the git remote add command:

[4] ~/Spoon-Knife (master)
$ git remote add upstream https://github.com/octocat/Spoon-Knife.git

[5] ~/Spoon-Knife (master)
$ git remote -v
origin https://github.com/fsantacroce/Spoon-Knife.git (fetch)
origin https://github.com/fsantacroce/Spoon-Knife.git (push)
upstream https://github.com/octocat/Spoon-Knife.git (fetch)
upstream https://github.com/octocat/Spoon-Knife.git (push)
  

Now, you can keep your local repository in sync with the changes in your remote, the origin, and you can also get those ones coming from the upstream remote, the original repository you forked. At this point. you are probably wondering how to deal with two different remotes; well, it is easy: simply pull from the upstream remote and merge those modifications in your local repository, then push them into your origin remote in a bundle with your changes. If someone else clones your repository, he or she will receive your work merged with the work done by someone else on the original repository.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.166.98