Git Clone and Git Fork

Learn via video courses
Topics Covered

Overview

To change the content of a project, we first fork the remote repository using the fork button present on GitHub or using the git fork command. Now after forking it, you will copy the entire project on your local system. This copying of the project is termed cloning in Git. So, you can either use the Clone or download button and download the zip project on your local system or you can use the git clone command for the same. Git clone and Git fork commands are used to clone and fork the remote repository to our local system.

Pre-requisites

The prerequisites for learning the git clone and git fork commands can be a basic understanding of Version Control Systems, Branching, and Git. Let us discuss them briefly before learning about the git clone and git fork commands.

Version Control Systems

A version control system is a tool in software development that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc. There are two types of Version Control systems: Centralized Version Control Systems (CVCS) and Distributed Version Control Systems (DVCS).

Git

Git is a version control system that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc. Git is free and one of the most widely used version control systems. We can use Git through the command line as well as through its graphical user interface (GUI). The command line or terminal version of Git is known as Git Bash, on the other hand, the GUI version of Git is known as Git GUI. Git tracks a project's changes and saves a state known as commit. A commit is a snapshot of the file's current version (s). So, we track these commits and can revert to a certain commit if we want.

Branching

A branch is an independent line of development that is used to add certain features and fix bugs without hampering the main project. So, we can develop new features in parallel and when the development is completed, we can add the back to the main project. By default, all the GitHub repository has the master branch which can be used for production.

So, a new branch is a copy of the master branch which is created for bug fixes and for the addition of new features. After the bug is fixed or new features are added, we can merge the branch to the master branch. The git branch command enables us to perform parallel development. The command can create, rename, list, and delete branches.

GitHub

GitHub is a cloud-based central repository that can be used to host our code for team collaboration. It is a hosting service that is used to manage the git repository in the central server. GitHub is a free (for certain limits) and easy-to-use platform that enables teammates to work together on projects. GitHub tracks the changes made in the project workflow and we can also revert back a certain state of the project (as GitHub saves the history of the project). GitHub supports open-source development where several developers can collaborate and support each other in the development process.

Anyone can create an account on the GitHub platform to host their code, files, and documents. GitHub sells hosted private code repositories, and other collaborative business model plans to make money as an organization.

Introduction

To learn the git clone and git fork commands, let us take a scenario. Suppose a developer or a team of developers have developed a project and hosted the project on the GitHub platform so that all the users can use it and see the source code. Now you are a user of the product and when you were viewing the project, you found an issue that you can fix. So, how will you fix the issue? Will you message the developers? Mail the team? or Will you change the code and ask the project maintainers to merge the changes after viewing it?

The last option is the best choice. So, to change the content of a project, we first fork the remote repository using the fork button present on GitHub or using the git fork command. Now after forking it, you will copy the entire project on your local system. This copying of the project is termed cloning in Git. So, you can either use the Clone or download button and download the zip project on your local system or you can use the git clone command for the same. Git clone and Git fork commands are used to clone and fork the remote repository to our local system.

This process should be used when we are working with a remote repository and we want to change something on it. Now, what is this remote repository? Well, a repository that is hosted or stored on the remote server is known as a git remote repository. As we have compared a git repository with a file system similarly we can compare a git remote repository with the remote file system. A good example of a remote git repository can be GitHub. A remote repository helps us to help or host on a server which allows us to share our code and files easily with fellow developers. We have a remote server GitHub where we can host our local git repository. This will allow fellow developers to view our work. The other developers can also contribute to our projects as our git remote repository (public) is visible to them. Hence, it supports community development and helps each other without any deed.

The commands- Git clone, Git fork, Git pull, and Git push- are used frequently when we are working on the repository hosted on GitHub. Let us now learn how we can fork and clone a remote repository using the git clone, and git fork commands.

How to Clone GitHub Repository?

If we want to clone some other developer's work (which is stored in the form of the central repository) then we can use the sample command that is git clone repository to copy the whole repository into our local system. Suppose, we have developed a project and we have hosted the project on the remote repository (server) and the data on our local system got deleted due to some issue. Then we can easily clone our very own repository; in this way, our data will always be stored in a safe place and will not be affected by any loss from the local system.

To fetch the data from the remote repository using git clone, we can use the command:

In the above command git clone repository, we need to provide the address of the remote repository that we want to clone. If we are using GitHub, then there is a button called clone button, we can get the address of the repository by clicking on the button. After copying the address of the repository, we will run the command on the Git shell and Git will clone the central repository to our local system.

Let us take an example of a dummy repository present on GitHub and then try to clone it using:

  1. Button present on the GitHub: Let us look at the steps to clone the remote repository using the clone button.

    1. First we need to open the remote repository that we want to clone on GitHub.
    2. Click on the Code button on the right corner of the menu.
    3. Under the Code button, you can see the various options for cloning. So, you can clone the project using the GitHub desktop application, Git (that we will see in the next subsection), and you can download the zip file of the entire project. Please refer to the image provided below for more clarity.

    button-on-github

  2. Using Git Bash by providing the URL of the remote repository: Let us look at the steps to clone the remote repository using the Git Bash application.

    1. First we need to open the remote repository that we want to clone on GitHub.
    2. Click on the Code button on the right corner of the menu.
    3. Under the Code button, you can see the remote repository link, so you just need to copy the link and then open Git Bash in any folder of your choice (where we want to clone the data).
    4. Open Git Bash and then type the command: git clone <remote>. Just replace the <remote> with the copied URL of the project. Please refer to the image provided below for more clarity.

    type-command-git-clone-on-gitbash

How to Fork a Repository?

In the above section, we have discussed the Git clone command which is used to clone the remote repository to the local system, the Git fork command is quite similar to the Git clone command. We use the Git fork command to fetch the changes from the remote repository to the cloned local repository. We used the Git fork command in the development process in cases such as bug fixing, feature adding, etc. We first fork the repository, then we run the repository on our local system for testing. After the testing, we make changes such as bug fixing or any extra addition of features then we push our changes to the forked repository.

Let us take an example of a dummy repository present on GitHub and then try to fork it.

  1. Open the desired remote repository on GitHub and then click on the top-right corner of the page where you can see a button named Fork.
  2. After clicking on the fork button, you need to select the owner for the forked remote repository.
  3. We can even change the name of the forked remote repository, but by default, the name of the forked repository is the same as the parent repository. We can also add some description of the fork for our reference.
  4. We then need to choose whether we want to clone all the branches or we just want to clone the master branch. By default, GitHub forks the master branch only.
  5. Finally click on the Create Fork button.

Do the git fork and git clone commands look similar? Let us now see some of the differences between them. Let us look at some of the differences between git fork and git clone.

  • The git fork command creates an independent copy of our git repository, on the other hand, the git clone command creates a linked copy that will be used to synchronize with the target repository.
  • Git fork is performed on GitHub i.e. on the remote repository, on the other hand, the git clone command is performed on git only.
  • Clone is a process of cloning a repository, on the other hand, forking is a concept of the development phase.

Cloning Your Forked Repository

So far we have learned how to clone a remote repository and how to fork a remote repository on GitHub. Let us now learn how we can clone the forked repository.

  1. Open GitHub and then navigate to the forked repository. There you can see a Code button (similar to what you have seen in cloning a repository in the above section).
  2. Click on the Code button.
  3. Copy the URL of the forked remote repository. We have numerous options available such as using HTTPS, using an SSH key, and using GitHub CLI.
  4. After that open GitBash at any location of our choice and then clone the repository using the command: git clone <remote>.

Cloning Your Forked Repository

So far we have seen the git clone and git fork commands. Let us see the differences between these commands and the use case.

git clonegit fork
We clone the remote repository using the Git application.We use GitHub to fork any remote repository.
By cloning a repository, we are creating a copy of the original remote repository on our local system.By forking a repository, we are creating a copy of the original remote repository on our GitHub account.
Git clone is a process, not a concept.Git fork is a concept, not a clone.
If we have updated or changed the code of the cloned repository then we need to be a collaborator or a maintainer to merge the changes.Whenever we change or update the code of the forked repository, we first need to ask the maintainer of the project to merge our work. We cannot directly merge our code to the main repository.
For cloning a repository, we use the git clone command.For forking a repository, we can use the Fork button on GitHub.

Conclusion

  • Git is a version control system that tracks the changes in the code, documents, and other important information regarding a certain code base, etc.
  • Git clone and Git fork commands are used to clone and fork the remote repository to our local system.
  • If we want to clone some other developer's work (which is stored in the form of the central repository) then we can use the sample command that is git clone repository to copy the whole repository into our local system.
  • The Git fork command is quite similar to the Git clone command. We use the Git fork command to fetch the changes from the remote repository to the cloned local repository.
  • The git fork command creates an independent copy of our git repository, on the other hand, the git clone command creates a linked copy that will be used to synchronize with the target repository.
  • Git fork is performed on GitHub i.e. on the remote repository, on the other hand, the git clone command is performed on git only.
  • Clone is a process of cloning a repository, on the other hand, forking is a concept of the development phase.
  • We clone the remote repository using the Git application, on the other hand, we use GitHub to fork any remote repository.
  • By cloning a repository, we are creating a copy of the original remote repository on our local system but by forking a repository, we are creating a copy of the original remote repository on our GitHub account.
  • If we have updated or changed the code of the cloned repository then we need to be a collaborator or a maintainer to merge the changes.
  • Whenever we change or update the code of the forked repository, we first need to ask the maintainer of the project to merge our work. We cannot directly merge our code to the main repository.