fork and clone are two frequent operations we encounter while dealing with Git. Both operations create a copy of the Git repository, but they’re used in different scenarios and have different goals.
In this tutorial, we’ll look at these two seemingly similar Git operations.
2. Git fork
Git fork creates a server-side copy of any existing public repository. When we want to contribute to any project, we can fork the existing project repository, which creates our own copy on the server. We can pull all the updates from the original repository on our personal server-side repository via git pull.
However, we have to raise pull requests if we want changes from our forked repository to be pushed to the public repository. The pull requests allow the maintainers to cherry-pick what changes they allow on the public repository. Additionally, this also provides a convenient way to collaborate on the changes.
To fork a repository, we can simply go to the public repository and click on the Fork button. This creates a server-side copy on our account:
Additionally, we can also fork a repository via the GitHub CLI Tools. Firstly, we’ve to install the GitHub CLI Tools. Then, let’s run the auth command from a terminal and follow the instructions to authenticate from the browser:
$ gh auth login --web
Finally, when we’re logged in, we can now run the fork command for the desired repository:
$ gh repo fork https://github.com/<your_repository_here>
Once the repository is forked, we can clone it on our local machine and start working on it.
We can use Git fork for open-source collaborative contributions or personal experimentation. We can also fork simply for having a personal backup of the public repository for reference.
3. Git clone
Git clone duplicates the contents of the existing Git repository. When we clone a repository, we copy the complete project on our local machine. This includes all the branches and commits history for the repository.
This allows us to work in offline mode, perform tests, and create new branches. Briefly, we make changes without impacting the original repository. However, for our changes to reflect in the original repository, we must push our changes back to the remote repository.
We can clone a repository by executing the following git command:
$ git clone https://github.com/<your_repository_here>
Git clone is helpful for offline access, individual work, or obtaining a copy of the codebase to use it further.
In this article, we saw how Git fork creates a repository copy on the server side. Git clone, on the other hand, creates a local copy of the repository that we may work upon, even in offline mode.