How to Fork and Clone a GitHub Repository

Kent C. Dodds
InstructorKent C. Dodds
Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 5 years ago

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.

[00:00] 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.

[00:11] Once you have all the changes you want in your repository you can create a [inaudible] 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.

[00:29] 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.

[00:40] 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.

[00:54] 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.

[01:06] 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.

[01:20] 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.

[01:42] 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.

[01:55] 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.

[02:18] 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.

[02:37] We're now ready to set up our environment and the project for making our change.

Oalbacha Albacha
Oalbacha Albacha
~ 6 years ago

What is padleft?

Nik Luparev
Nik Luparev
~ 4 years ago

Can you explain one more time what command " --set-upstream-to=upstream/master master" does? My guess is whenever you do "git pull origin master" it will pull the latest changes from upstream master and not from your copy

Markdown supported.
Become a member to join the discussionEnroll Today