In this tutorial, you will learn about how to use the git clone command to clone an existing repository from a remote.
Cloning a repository means that you will create a copy of an existing repository into a new repository.
When you clone a repository, be it a local or a remote repository, you clone all the files and also all the Git history.
Here, we will focus on cloning remote repositories locally on your computer.
Remote repositories are Git repositories that are hosted in an online hosting service such as Github, Gitlab or Bitbucket.
If you are using Windows, you can read the more detailed steps on this article: Clone a Git Repository on Windows.
What Does it Mean to Clone a Repository?
When you clone a repository, you create a copy on your computer of the files within the repository, along with its entire history, branches, and tags.
Cloning a git repository to a local folder takes an existing remote, makes a full copy out of it on your computer and sync both locations. The copy is made at a point in time and includes all the files and folders, and all their versions for the project.
When cloning a repository, Git knows where the original repository was by storing a remote tag in the new repository’s configuration. You can view the remote using the git remote command.
When you clone the repository, Git will automatically name the remote
How to Git Clone a Local Repository
You can clone a remote or a local repository using the git clone command.
To clone a local repository:
- Open your Terminal
- Navigate to the location where you want the local repository to be copied, using the cd command
- Use the git clone command followed by the path to the project directory
- You can also give it a name using
git clone /path/to new_repo
Clone a Remote Repository on Your Local Computer
You can use the
git clone <URL> command to clone a remote repository.
The <URL> shows the location of the repository that you want to clone.
The git clone command looks something like this:
git clone https://github.com/user/repo.git
How to Clone a Repository From Github
You can male a copy of an existing remote repository on your local computer by cloning it. Here are the steps to do so.
- On Github.com, go to the repository
- On top of the files, click on <> Code
- Copy the URL of the repository
There are three ways that you can clone your repository:
Clone the repository using HTTPS under “HTTPS”.
Clone the repository using an SSH key under “SSH”
Clone a repository using GitHub CLI under “GitHub CLI”
View the section later to know if you should choose HTTPS or SSH.
- Open Terminal and Move to the Repo where to Clone the Remote
- Add the git clone command
- Press ENTER
How to Clone a Repository From Gitlab
The steps to clone a repository from Gitlab are similar.
You can also find the clone URL straight from the Gitlab repository.
How to Clone a Repository From Bitbucket
Again, similar technique to clone a git repository from Bitbucket.
- Open Bitbucket and go to the repository you want to clone.
- Go on the left hand side of the screen, select the clone button (second)
- Click on “Clone” on the right-hand side of the page.
- Copy the URL
- Open your terminal (or command prompt)
- cd into the directory where you want to clone the repository.
- Type the “git clone” command with the URL that you want to clone.
Git Clone with Username and Password
To provide a username and password when running the “git clone email@example.com” command use the
firstname.lastname@example.org format in the cloning URL.
$ git clone https://email@example.com/username/repository.git
It is not necessary to add the password in the command as Git with prompts you for the password. It is more secure this way.
Otherwise, adding the password to the clone URL this way will store the password in the Bash history file.
$ git clone https://username:firstname.lastname@example.org/username/repository.git
Git Clone Branch
To clone a specific git branch use the
git clone -b flag (short for
$ git clone -b <branch> <repository URL>
Here is an example of cloning a branch without switching branches on the remote repository.
$ git clone -b branch-name email@example.com:user/repository.git
Should You Use HTTPS or SSH For Git?
When connecting to a Git remote, you can use either the SSH or HTTPS protocol.
Using HTTPS or SSH to connect to remote Git repositories like Gitlab and Github depends on the level of security that your project requires.
- HTTPS is easier to set up
- SSH is more secure as it offers a stronger encryption and authentication. Transferred data is encrypted when using SSH. It is also faster.
Git Clone HTTPS
The HTTPs clone command looks like this:
git clone https://github.com/user/repo.git
Git Clone SSH
While the SSH command you connect using a password-protected SSH key using this pattern:
Multiple Ways to Copy a Repository
There are many ways that you can use to create a copy of a Git Repository: cloning, forking, mirroring, archiving and exporting.
- Cloning: Create a full copy of the repository on your local machine, including all of its commit history, branches, and tags. Use cloning when working on an existing repository.
- Forking: Create a copy of the repository on the Git hosting service instead of your local machine. Use Forking when you want to contribute to an open-source project that you don’t have write access.
- Mirroring: Create a read-only copy of a Git repository. Use mirroring to create backups.
- Archiving: Create a compressed copy of a Git repository. Use archiving to create backups or to share to non-git users.
- Exporting: Create a copy of a Git repository in a non-Git format. Use exporting to share to non-git users.
- Cloning a specific branch: Create a copy of a single branch of a Git repository. Clone a branch using the “git clone -b <branch_name> <URL>” command.
Difference Between Cloning and Forking a Repository
Cloning and forking are two ways to copy a Git repository, but have different purposes.
To clone a repository means that you create a copy of the full repository on your computer. When cloning, you download all the files, folders, along with the commit history, the branches, on your local computer. When you clone a repository, you are able to make, commit and push changes made to your local copy.
To fork a repository means that you create a copy of the repository on a Git hosting service (e.g. Github, Gitlab, Bitbucket). When forking, you create a new repository (identical to the original). When you fork a repository, you have full control over it and are able to make, commit and push changes made to the fork. You can’t however commit to the original repository without a merge request.
Debug Cloning Errors
Error: Repository not found
The Error: Repository not found error in git can happen when the repository does not exist or when you don’t have:
- The right repository URL. Check for a typo or the wrong casing when cloning
- Enough permissions to access the repository (e.g. private repository)
- SHH Key connected to your personal Github account
Fatal: Git Clone Authentication Failed for URL
The fatal Authentication failed issue faced when cloning a git repository can be caused by the following things:
- User logged out due to multiple incorrect login attempts,
- two-factor authentication is turned on
- Windows messed up with credentials stored in the credentials manager
fatal: Authentication failed for <URL>
Debugging Fatal: Git Clone Authentication Failed
- Got to Gitlab/Github/Bitbucket website and log in manually so that you can complete the CAPTCHA.
- Generate access token on Github, go to Applications and generate token (steps)
- In Windows, Search for
Credential Manager. Select
Windows managerand select your Gitlab/Github/Bitbucket credentials and modify them.
Other Git Methods and Github Posts
SEO Strategist at Tripadvisor, ex- Seek (Melbourne, Australia). Specialized in technical SEO. Writer in Python, Information Retrieval, SEO and machine learning. Guest author at SearchEngineJournal, SearchEngineLand and OnCrawl.