Git

How to clone a git repository

The git repository can be created locally and published later in the GitHub account. The repository can be created remotely, and the copy of the remote repository can be stored locally. Git clone is a very useful git command to copy or clone a particular existing repository that can be local or remote. When the git clone command is used to clone a remote repository from the GitHub account, it copies the full content of the target repository with all versions of files and folders. Suppose the git user needs to make the correction on the repository content, add or remove more than one file, and push the larger commits. In that case, it is better to make a clone of the required GitHub repository the modification. The repository can be pushed to the remote server later after modification. Git clone command can be used to clone any local repository also. The ways of cloning a remote and local repository have shown in this tutorial.

Prerequisites:

Install GitHub Desktop

GitHub Desktop helps the git user to perform the git-related tasks graphically. You can easily download the latest installer of this application for Ubuntu from github.com. You have to install and configure this application after download to use it. You can also check the tutorial for installing GitHub Desktop on Ubuntu to know the installation process properly.

Create a GitHub account

You will require a GitHub account to check the commands used in this tutorial for the remote repository.

Create a local and remote repository

You have to create a local repository and a remote repository to check the commands used for cloning.

Clone a remote repository:

Open the terminal and run the following `cd` command to go to the folder, git-repositories where the new local repository will be created. `git clone` command will make a copy of the remote repository named Django to the local folder. You have to provide your username and password of the GitHub account after running the `git clone` command. `ls` command will display the content of the current folder.

$ cd git-repositories

$ git clone https://github.com/fyc21/Django

$ ls

The following output shows that the Django folder is copied from the remote server to the local folder.

Run the following commands to check the content of the Django folder.

$ cd Django

$ ls


The output shows that the Django folder has three folders, Models, Templates, and Views.

The content of the remote repository can be checked by opening the remote repository from github.com. https://github.com/fyc21/Django

The following output shows that the remote repository contains the same content that is downloaded in the local repository.

Clone a remote repository with a different name:

The clone command for copying the remote repository to a local repository with that same name has been shown in the previous example. The use of the clone command to create a local repository from a remote repository with a different name has shown in this part of this tutorial. Run the following command to create a local repository with the named django-tutorials by cloning the remote repository named Django.

$ git clone https://github.com/fyc21/Django django-tutorials

The following output shows that a local repository named django-tutorials is created from the remote repository named Django.

Clone a remote repository using GitHub Desktop:

Open the GitHub Desktop and click on the option, “Clone a repository from the Internet…” to select the remote repository you want to copy in the local system.

The following dialog box will appear to select the remote repository. The list of published remote repositories is displaying here. Select the repository named bash, choose the local path where the repository will be stored in the local drive, and click on the Clone button to copy the repository.

The following content will be displayed after completing the cloning process. This repository contains only one file copied in the local repository, and the last commit message is “Bash tutorials“.

The remote repository has been copied properly or not into the local repository can be checked by executing the following code from the terminal. The first `ls` command will show the list of files and folder of the current location. `cd` command will change the directory location to the folder, bash that is the local repository folder copied from the remote repository. The first `ls` command will show the list of files and folder of the local repository, bash.

$ ls

$ cd bash

$ ls

The following output shows that the remote repository is copied properly in the defined folder of the local drive, and the folder contains the file named bash-basic-tutorial.sh of the remote repository.

Clone a local repository:

The `git clone` command can create a new local repository by copying an existing local repository. The first `ls` command will display the list of files and folders before executing the clone command. The clone command will create a new repository named bash-tutorial by copying the local repository named bash. The last `ls` command will display the list of files and folders after executing the clone command.

$ ls

$ git clone bash bash-tutorial

$ ls

The following output shows that the new local repository has been created after executing the above commands. Here, bash-tutorial is the newly created local repository.

Conclusion:

Different uses of the clone command have been shown in this tutorial by using multiple demo git repositories. The ways of cloning remote repository have been explained here by using terminal and GitHub Desktop. The way of cloning the local repository has been shown by executing the git command from the terminal. I hope that the readers will be able to properly clone any local or remote repository after reading this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.