Git

What is Cloning in Git?

GitHub is a platform where multiple developers work concurrently on the same development project. While working on large development projects, they work in a team. Sometimes, they may need to copy other team members’ repositories in their local system to test code or modify it. For this purpose, the cloning process can be utilized.

This article will describe:

What is Cloning in Git?

Cloning is a process in Git used to make or create a copy of the local or remote Git repositories in the local system. The modifications of the cloned repository cannot be combined with the original repository except by the repository’s collaborator or owner. Moreover, it allows users to contribute to an open-source project by recommending changes or bug fixes and sending pull requests to the original remote repository.

How to Clone the Local Repository?

To clone the local repository, first, navigate to the root directory. Then, display the root directory’s local repositories and choose the particular repository you want to clone. After that, utilize the “git clone <repo-name>/ <clone-repo-name>” command to make a clone of the specific local repository. Lastly, verify the newly created cloned repository.

Step 1: Redirect to Local Repository

First, utilize the below-listed command and switch to the root directory:

cd "C:\Git"

Step 2: View Repository Content

Then, list the content of the root repository using the following command:

ls

It can be seen that the root repository contains three local repositories. Now, choose the desired local repository which you want to clone:

Step 3: Clone Local Repository

Now, type out the “git clone” command along with the previously selected repository for cloning and specify the name for the new clone repository:

git clone Repo1/ CloneRepo1

Here, “Repo1” is our desired repository which we want to clone, and “CloneRepo1” is the name of our new clone repository:

Step 4: Verify Changes

To view the newly created cloned repository, list the content of the root directory:

ls

Here, it can be seen that the clone of our desired repository has been created successfully:

As you can see the content of the original “Repo1” repository and cloned “CloneRepo1” repository is the same:

How to Clone the Remote Repository?

To clone the remote repository in the local repository, first, redirect to the desired GitHub repository and copy its HTTP URL. Then, switch to the particular local repository and run the “git clone <remote-URL>” command.

Step 1: Copy HTTP URL of Remote Repository

First, redirect to the desired GitHub repository which needs to be cloned, and copy its HTTP URL:

Step 2: Switch to Local Repository

Then, redirect to the particular local repository:

cd "C:\Git\Repo2"

Step 3: Clone Remote Repository

Now, utilize the following command along with the remote repository’s URL to clone it:

git clone https://github.com/laibayounas/newRepo.git

Step 4: Verify Changes

List the content of the current repository to ensure that the remote repository has been cloned:

ls

In the below output, it can be seen that the “newRepo” remote repository has been cloned/copied successfully:

How to Clone the Remote Repository With Specific History?

Cloning the remote repository brings all the history of commits. However, users can clone the remote repository with the specific history. For this purpose, users must specify the number of commits using the “–depth” option. To do so, follow the given-provided steps.

Step 1: Clone Remote Repository

To clone or copy the certain remote repository with a specific history, write out the following command and specify the depth and remote repository’s HTTP URL:

git clone --depth 1 https://github.com/laibayounas/newRepo.git

Here, the “–depth” option is utilized to obtain the desired commits. The “depth 1” will retrieve the most recent commit of the remote repository only:

Step 2: Go to Remote Repository

Then, switch to the cloned Git repository:

cd newRepo

Step 3: Verify Changes

Finally, check the reference log to view the commit history of the cloned repository:

git reflog .

The below image shows the latest commit only, which means the remote repository has been cloned successfully with one commit:

That was all about cloning in Git.

Conclusion

Cloning is a procedure in Git used to make a copy of the particular Git repositories in the local system. Users can clone any local or remote Git repository. Moreover, the remote repository can also be cloned with the specific history. This article explained about cloning in Git.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.