Fork A Repo
Copy a repo to create a new, unique project from its contents.
If you’ve found yourself on this page, we’re assuming you’re brand new to Git and GitHub. This guide will walk you through the basics and explain a little bit about how everything works along the way.
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.
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:
$ git clone email@example.com:username/Spoon-Knife.gitClones your copy of the repo into the current directory in terminal
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
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.
$ 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:
Once 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:
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.
$ git push origin masterPushes commits to your remote repo stored on GitHub
Pull in upstream changes
If the original repo you forked your project from gets updated, you can add those updates to your fork by running the following code:
$ git fetch upstreamFetches any new changes from the original repo $ git merge upstream/masterMerges any changes fetched into your working files
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.
$ 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.
$ 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 branches
Branching 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.
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:
$ git branch mybranchCreates a new branch called "mybranch" $ git checkout mybranchMakes "mybranch" the active branch
Alternatively, you can use the shortcut:
$ git checkout -b mybranchCreates a new branch called "mybranch" and makes it the active branch
To switch between branches, use
$ git checkout masterMakes "master" the active branch $ git checkout mybranchMakes "mybranch" the active branch
Once you’re finished working on your branch and are ready to combine it back into the
$ git checkout masterMakes "master" the active branch $ git merge mybranchMerges the commits from "mybranch" into "master" $ git branch -d mybranchDeletes the "mybranch" branch
If you are hoping to contribute back to the original fork, you can send the original author a pull request.
Unwatch the main repo
When 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 fork
At 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.
You have now forked a repo. What do you want to do next?