GIT and GitHub - Fork/Clone & Origin/Upstream
Note: this section on Fork is based on Fork A Repo.
A fork is a copy of a repository. Forking a repository allows us to freely experiment with changes without affecting the original project. In other words, a fork is a personal copy of a repository that we can use as we wish.
Basically, this is doing a cloning a GitHub repo at GitHub before cloning that fork locally to our machine.
The difference between clone and fork will become clear if we understand what the fork is.
Most commonly, forks are used to either propose changes to someone else's project or to use someone else's project as a starting point for our own.
Picture from What is the difference between origin and upstream in github
A great example of using forks to propose changes is for bug fixes. Rather than logging an issue for a bug we've found, we can:
- Fork the repository.
- Make the fix.
- Submit a pull request to the project owner.
If the project owner likes our work, they might pull our fix into the original repository!
At the heart of open source is the idea that by sharing code, we can make better, more reliable software. In fact, when we create a repository on GitHub, we have a choice of automatically including a license file, which determines how we want our project to be shared with others.
Note: when we are cloning a GitHub repo on our local workstation, we cannot contribute back to the upstream
repo unless we are explicitly declared as "contributor".
So when we clone a repo to our local workstation, we're not doing a fork
. It is just a clone.
This section is based on What is the difference between origin and upstream in github
- upstream generally refers to the original repo that we have forked
- origin is our fork: our own repo on GitHub, clone of the original repo of GitHub
According to GitHub page:
"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":
$ git remote add upstream git://github.com/user/repo.git
- We will use upstream to fetch from the original repo (in order to keep our local copy in sync with the project we want to contribute to).
- We will use origin to pull and push since we can contribute to our own repo.
- When we're doing
git push origin branchname
, we're pushing to the origin repository. There's no requirement to name the remote repository origin, and there can be multiple remote repositories. Remotes are simply an alias that store the url of repositories. We can see what url belongs to each remote by usinggit remote -v
. In the push command we can use remotes or we can simply use a url directly. - We will contribute back to the upstream repo by making a pull request.
Git/GitHub Tutorial
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization