This guide looks at the nuances of git fork
and git clone
, providing clear examples to illustrate their differences and applications.
Git clone
The git clone
command is used to create a local copy of a remote repository. This command duplicates the repository, including all its files, history, and branches, onto your local machine. Cloning is the first step most developers take when they want to work on an existing project or start a new one based on another project.
How to clone a repository
To clone a repository, you would use the following command in your terminal:
git clone <repository-url>
For example:
git clone https://github.com/username/project.git
This command creates a directory named project
in your current directory, initializing it as a Git repository and pulling in all the data from the remote repository.
Git fork
"Forking" is not a command within the Git software itself but a feature offered by hosting services like GitHub, GitLab, and Bitbucket. When you fork a repository, you create a copy of the original repository (the "upstream" repository) on the server side, under your own account.
This allows you to make changes independently of the original repository. Forking is commonly used to propose changes to someone else's project or to use someone else's project as a starting point for your own idea.
How forking works on GitHub
On GitHub, you can fork a repository by navigating to the repository's page and clicking the "Fork" button. This creates a copy of the repository under your GitHub account.
Git fork vs. clone
- Ownership and Location: Forking creates a new repository under your account on the hosting service, allowing you to work independently of the original project. Cloning, on the other hand, creates a local copy of a repository on your machine. You can push changes back to the remote repository if you have permissions.
- Contributing back: Forking is a two-step process for contributing to someone else's project. First you fork the repository to your account, then clone it to your local machine. After making changes locally, you can push them to your fork on GitHub and submit a pull request to the original repository. Pull requests are requests sent to the maintainers of the original repository, asking them to review and potentially merge your changes into their project. In the pull request, you can specify the changes you've made, why you've made them, and any other relevant information or discussion points. On the other hand if you have direct write access to a repository and don’t need to keep a separate copy under your account, cloning alone should be sufficient.
Fork vs. branch vs. clone
While a fork is a server-side copy of an entire repository under your account, and a clone is a local copy of a repository, a branch is a divergent line of development within the same repository. Branches are lightweight and used to isolate development work without affecting other branches in the repository.
Fork vs. mirror
A mirrored repository is a read-only copy of another repository that syncs with the original, mirroring all changes. Unlike a fork, a mirror does not allow for independent development but serves as a real-time backup or public mirror of a private repository.
Choosing between forking and cloning
- Fork when you want to contribute to a project you don’t have write access to, or you want to start a new project based on an existing project but work independently.
- Clone when you have write access to a repository and want to collaborate directly on the project, or you want a local copy of the project to work offline or make personal changes.
For more reading on forking and how it differs from cloning, see the official GitHub documentation.