First: Fork A Repo
At some point you may find yourself wanting to contribute to someone else’s project, or would like to use someone’s project as the starting point for your own. This is known as “forking.” For this tutorial, we’ll be using the Spoon-Knife project.
- Fork the “Spoon-Knife ” repo
To fork this project, click the “Fork” button.
Next: Set Up Your Local Repo
You’ve successfully forked the Spoon-Knife repo, but so far it only exists on GitHub. To be able to work on the project, you will need to clone it to your local machine.
- Clone the “Spoon-Knife” project
Run the following code:1$ git clone email@example.com:<em>username</em>/Spoon-Knife.gitClones your copy of the repo into the current directory in terminal
- Configure remotes
When a repo is cloned, it has a default remote called
originthat points to your fork on GitHub, not the original repo it was forked from. To keep track of the original repo, you need to add another remote named
More about remotes
A remote is a repo stored on another computer, in this case on GitHub’s server. It is standard practice (and also the default in some cases) to give the name
originto the remote that points to your main offsite repo (for example, your GitHub repo).
Git supports multiple remotes. This is commonly used when forking a repo.123$ cd Spoon-KnifeChanges the active directory in the prompt to the newly cloned "Spoon-Knife" directory$ git remote add upstream git://github.com/octocat/Spoon-Knife.gitAssigns the original repo to a remote called "upstream"$ git fetch upstreamPulls in changes not present in your local repository, without modifying your files
Then: More Things You Can Do
You’ve successfully forked a repo, but get a load of these other cool things you can do:
- Push commitsOnce you’ve made some commits to a forked repo and want to push it to your forked project, you do it the same way you would with a regular repo:
More about commits
Think of a commit as a snapshot of your project —code, files, everything — at a particular point in time. More accurately, after your first commit, each subsequent commit is only a snapshot of your changes. For code files, this means it only takes a snapshot of the lines of code that have changed. For everything else like music or image files, it saves a new copy of the file.1$ git push origin masterPushes commits to your remote repo stored on GitHub
- Pull in upstream changesIf the original repo you forked your project from gets updated, you can add those updates to your fork by running the following code:
12$ git fetch upstreamFetches any new changes from the original repo$ git merge upstream/masterMerges any changes fetched into your working files
What is the difference between fetch and pull?
There are two ways to get commits from a remote repo or branch:
pull. While they might seem similar at first, there are distinct differences you should consider.
Pull1$ git pull upstreamPulls commits from 'upstream' and adds them to the local repo
When you use
pull, Git tries to automatically do your work for you. It is context sensitive, so Git will merge any pulled commits into the branch you are currently working in. One thing to keep in mind is that
pullautomatically merges the commits without letting you review them first. If you don’t closely manage your branches you may run into frequent conflicts.
Fetch/Merge12$ git fetch upstreamFetches any new commits from the original repo$ git merge upstream/masterMerges any commits fetched into your working files
fetch, Git gathers any commits from the target branch that do not exist in your current branch and stores them in your local repo. However, it does not merge them with your current branch. This is particularly useful if you need to keep your repo up to date but are working on something that might break if you update your files. To integrate the commits into your master branch, you use
merge. This combines the specified branches and prompts you if there are any conflicts.
- Work with branchesBranching allows you to build new features or test out ideas without putting your main project at risk. A Git branch is a small file that references the commit it was spawned from. This makes Git branches very small and easy to work with.
How do I use branches?
Branches are pretty easy to work with and will save you a lot of headaches, especially when working with multiple people. To create a branch and begin working in it, use the following script:12$ git branch <em>mybranch</em>Creates a new branch called "mybranch"$ git checkout <em>mybranch</em>Makes "mybranch" the active branch
Alternatively, you can use the shortcut:1$ git checkout -b <em>mybranch</em>Creates a new branch called "mybranch" and makes it the active branch
To switch between branches, use
checkout.12$ git checkout masterMakes "master" the active branch$ git checkout <em>mybranch</em>Makes "mybranch" the active branch
Once you’re finished working on your branch and are ready to combine it back into the
merge.123$ git checkout masterMakes "master" the active branch$ git merge <em>mybranch</em>Merges the commits from "mybranch" into "master"$ git branch -d <em>mybranch</em>Deletes the "mybranch" branch
- Pull requestsIf you are hoping to contribute back to the original fork, you can send the original author a pull request.
- Unwatch the main repoWhen you fork a particularly popular repo, you may find yourself with a lot of unwanted updates about it. To unsubscribe from updates to the main repo, click the “Unwatch” button on the main repo.
- Delete your forkAt some point you may decide that you want to delete your fork. To delete a fork, just follow the same steps as you would to delete a regular repo.
More info at http://help.github.com/fork-a-repo/