Forking a Repo on GitHub

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.

  1. Fork the “Spoon-Knife ” repo

    To fork this project, click the “Fork” button.

    Click “Fork”

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.

  1. Clone the “Spoon-Knife” project

    Run the following code:

  2. Configure remotes

    When a repo is cloned, it has a default remote called origin that 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 upstream:

    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 origin to 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.

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.

  • 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:

    What is the difference between fetch and pull?

    There are two ways to get commits from a remote repo or branch: fetch and pull. While they might seem similar at first, there are distinct differences you should consider.

    Pull

    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 pull automatically merges the commits without letting you review them first. If you don’t closely manage your branches you may run into frequent conflicts.

    Fetch/Merge

    When you 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:

    Alternatively, you can use the shortcut:

    To switch between branches, use checkout.

    Once you’re finished working on your branch and are ready to combine it back into the master branch, use merge.

  • 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.

    Click “Unwatch”

  • 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/

Share on FacebookShare on Google+Email this to someoneShare on RedditShare on LinkedInShare on TumblrTweet about this on TwitterShare on StumbleUpon

Leave a Reply

Your email address will not be published.