Last update on .

What is the difference between a Fork and a Clone of a GitHub repository? Having never really understood the difference between forking a repository and cloning one, I decided to do some research and here is what I have determined.

The git command for copying a repository is git clone. There is no command in the git command reference for fork. Forking a repository only exists on GitHub or GitLab. A fork is a request for GitHub/GitLab to clone the project and register it under your username. This allows you to freely experiment with changes without affecting the original or upstream project. Once you have forked the repository, you can use git clone to copy it down to your local machine and make modifications.

So why would you fork a repo instead of clone it? By forking a repository on GitHub or GitLab, you still have a link to it. Any changes to the upstream repository can be added to your repository by using the fetch and merge commands.

There are a couple of reasons for doing this.

1) You may want to contribute to the project so you fork the repo and make your changes, then submit a pull request to the upstream project to incorporate your changes.

2) But most often, the case is that you simply want to build upon something that someone else has already done. Most of the time, a developer will fork an existing project and customize it to his or her preference, while able to capitalize on any updates that are made to the original, upstream project.

How does it work? Once you have forked a repository to your GitHub/GitLab username, you clone it to your local machine. After making modifications, when you get ready to commit and push any changes back up to your remote repo, the origin remote points to your forked repo.

If you wish to keep your version of the repo current and up-to-date with the upstream project, you need to add another remote (typically named "upstream") that links to the upstream repo so that you can occasionally fetch and merge any changes that have been made to the original project. This makes sure that any security updates or other important changes are being incorporated into your version.

Your list of remotes will look something like this depending on the service you use:

$ git remote -v
> origin    https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
> origin    https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
> upstream  https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch)
> upstream  https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)

References

This article from Bryan Pendleton gave the clearest explanation: Git Clone vs Fork

This answer on Stack Overflow is pretty solid and includes a great graphic (included below) that helps with understanding the flow: What is the difference between origin and upstream on GitHub?

And finally, here are some links to the instructions on GitHub on how to fork a repo and how to keep it in sync with the upstream repository. GitLab also has help for forking a project.

Git vs Fork

Additional Information about Entry

Comments

No comments yet.

Post your comment