Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 1047 of the free egghead.io lessons, plus get JavaScript content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

How to Fork and Clone a GitHub Repository

2:41 JavaScript lesson by

You cannot push code to repositories that you don’t own or have sufficient permission to. So instead, you make your own copy of the repository by “forking” it. You are then free to make any changes you wish to your repository.


egghead.io comment guidelines

Avatar
egghead.io

You cannot push code to repositories that you don’t own or have sufficient permission to. So instead, you make your own copy of the repository by “forking” it. You are then free to make any changes you wish to your repository.

Because GitHub won't let you push code to repositories that you don't own or have sufficient permission to, you need to make a copy of the repository by forking it, and then you can make all your changes to that repository.

Once you have all the changes you want in your repository you can create a [inaudible 0:14] request to the project, and the maintainer can merge your changes into the main project. To do this we'll click on the fork button, and this will open up a dialog where we can click on our user to fork the repository to our users' repositories.

With that finished, we now have our own copy of the repository, this is forked from the egghead.io GitHub organization, but it's a part of our user, so we can make all changes to it that we want.

Now we need to take this SSH URL, and we'll copy it to our clipboard. Then we'll open our terminal, or if you're on a windows machine use GitBash, and git clone this repository into a directory where we want to edit the project.

Let's make sure that we're cloned the right project. If we LSAL, then we're going to see that we have a git, a gitignore, all of the files that this project has itself, so we're on the right track.

One of the challenges with forking a repository is keeping your copy up-to-date with the original repository, or the upstream repository. We're going to add the original repository as a git remote, so we can easily fetch and pull from it.

To do this we're going to need the SSH URL for the original repository. We'll go back to the main project here, we'll make a copy of the original SSH URL, and go into our terminal again, and we'll execute the command git remote add upstream and then paste the SSH URL that we just copied.

Now that we have the remote added, we need to tell our local git to go get information about that upstream remote, so we'll execute the command git fetch upstream. That will give us information about that remote.

It has a branch called master, and that is being pointed to upstream/master. Right now our local copy of the master branch is pointing to our personal remote repository. Our repository is called origin, we want our local copy of master to point upstream, so whenever we pull changes into master it will get the latest changes from the upstream repository.

To do this, we'll run the command git branch --set-upstream-to=upstream/master master. Now our branch, called master, is tracking the upstream master. When there are updates we like to get from the upstream repository we can get them by simply pulling on that branch.

We're now ready to set up our environment and the project for making our change.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?