The git clone command creates a copy of an existing Git repository. This repository can be hosted on platforms like GitHub, GitLab, or Bitbucket, or it can reside on a local or remote server. Cloning a repository involves copying all the repository’s data to your local machine, including the entire history and all branches.
Syntax:
git clone <repository-link>
For Example: If you want to clone the Bootstrap repository, you would run.
git clone https://github.com/twbs/bootstrap
What is Git Clone?In the above image, It’s a Bootstrap repository and you want to clone it in your local system. For this, you have to use the link to clone this repository on your computer. First, you have to go inside the folder in which you want to clone the repository, for this use the cd command
What is Git Clone?After that, Clone the repository using git clone <repo-link>
git clone https://github.com/twbs/bootstrap
What is Git Clone?The cloning process will begin. You’ll see progress messages indicating how much of the repository has been cloned (e.g., 14%, 50%, 100%).

Once the cloning process is complete, you can navigate into the new directory (e.g., cd bootstrap) to start working on the project locally.

If you need to stop the cloning process before it finishes, you can press Ctrl + C to cancel the operation.

By cloning a repository, you get a complete copy of the project, including all of its branches and commit history, allowing you to work offline and push your changes back once you are ready.
Git Clone With Username And Password
If you are cloning a repository using HTTPS, you may need to authenticate with your GitHub (or other Git hosting services) username and password. You can pass the credentials directly in the URL like this:
git clone https://github.com/username/repository
In the place of "Username" and "Password" give your credentials.
Important: For security reasons, it is not recommended to include your password in the URL. Instead, you should use a credential manager or access tokens for more secure authentication.
Cloning a Specific Branch
By default, git clone clones the main branch (often named main or master) of a repository. However, if you want to clone a specific branch of the repository, you can use the -b option.
Syntax:
git clone -b <branch-name> <repository-url>
This command clones the specified branch into your local repository instead of the default branch.
Repo-to-Repo Collaboration
Cloning repositories is a key part of collaborating on projects with other developers. When you clone a repository, you create a local copy where you can make changes. Afterward, you can push your changes back to the remote repository for others to review and merge.
To clone the remote repository use the following command.
git clone <remote_repository_url>
This creates a working copy of the project, allowing you to modify it, and then push the updates back to the repository.
Git Clone -Branch
The git clone --branch command is used to clone a specific branch from a remote Git repository to your local machine. By default, when you clone a repository, it clones the main branch (usually called main or master). However, if you want to clone a different branch, you can specify the branch name in the command.
git clone --branch <branch_name> <remote_repository_url>
- <branch_name>: This is the name of the branch you want to clone from the remote repository.
- <repository_url>: This is the URL of the remote repository (either HTTPS or SSH).
For example, if you want to clone the feature-xyz branch from a repository, you would run the following command:
git clone --branch feature-xyz https://github.com/username/repository
Git Clone - Mirror vs Git Clone - Bare
Git supports two specialized cloning methods: mirror cloning and bare cloning.
1. Git Clone - Mirror:
- A mirror clone creates a complete, exact copy of a remote repository, including all refs, branches, and configuration.
- Use Case: Typically used for creating full backups of repositories.
git clone --mirror <repository-url>
2. Git Clone - Bare:
- A bare clone only contains the repository’s version control data (e.g., .git folder) and doesn't include the working directory (the actual files). It’s commonly used for shared repositories.
- Use Case: Often used for creating a central repository for collaboration or setting up a server repository.
git clone --bare <repository-url>
Git Clone -Mirror vs. Git Clone -Bare
Git Clone -Mirror | Git Clone -Bare |
|---|
Git Clone -Mirror create an complete copy of the git remote repository or complete mirror of the git repository. | Git Clone -Bare will only copies the file which are required for the keeping the track of that repository. |
Git Clone -Mirror it will have the all the copies of the files which are exactly present in the remote git repository. | Git Clone -Bare will only keeps the files which are necessary to keep the track. |
Mostly used for the backup purposes | Mostly used for the continuous integration. |
It can also be used for the offline development purpose. | It can also be used for the working on the same repository on multiple locations. |
Git URLs for Cloning
The code which is stored in the git remote repositories are need to be accessed or to be cloned in to the local repositories for that git URL's will acts as an git web address.
Types of Git URL's
1. HTTPS URL: Commonly used for cloning and pushing code.
Example: https://github.com/username/repository
2. SSH URL: More secure and used for authentication without entering a username or password.
Example: [email protected]:username/repository.git
3. Git Protocol URL: Less commonly used but works similarly to HTTP.
Example: git://github.com/username/repository.git
4. Local File System: Clone from a local directory.
Example: /path/to/repository
5. Relative File System: For local repository paths relative to the current directory.
6. Git Bundle: A packaged archive of a Git repository.
Cloning a Repository into a Specific Local Folder
You can specify the local directory where you want the repository to be cloned. Use the following command to clone a repository into a specific folder:
Syntax
git clone <repository-url> <local-folder-name>
Replace the URL with the repository URL you want to clone, and specify the name of the folder where you want to copy the repository in place of <local_folder>.
For example, to clone a repository into a folder named my-project, use:
git clone https://github.com/username/repository my-project
What does the git clone command primarily do?
-
Creates a new empty Git repository
-
Copies only the latest commit from a remote repository
-
Downloads the entire repository including history, branches, and files
-
Creates a mirror of your local directory
Explanation:
git clone creates a full copy of an existing repository, including:
- complete commit history
- all branches
- remote configuration
This allows you to work offline and push changes later.
Which command is used to clone a specific branch instead of the default branch?
-
git clone -remote <branch>
-
git checkout clone <branch>
-
git clone --branch <branch_name> <repo_url>
-
git clone /branch <branch_name>
Explanation:
To clone a specific branch, you use:
This fetches only the chosen branch instead of the standard main/master.
What is the main difference between git clone --mirror and git clone --bare?
-
Bare clone includes all branches but mirror does not
-
Mirror clone includes all refs and configuration; bare clone only includes repository metadata
-
Bare clone is for backup; mirror clone is for collaboration
-
They behave exactly the same
Explanation:
- Mirror clone → full exact copy including all refs + remote configuration (ideal for backups).
- Bare clone → only the metadata (
.git) without working files, used for central/shared repositories.
What happens if you press Ctrl + C during a cloning process?
-
The clone restarts automatically
-
The cloning process is canceled
-
It deletes the entire repository
-
Git switches to mirror mode
Explanation:
Pressing Ctrl + C terminates the cloning operation. Any partially cloned files remain, but the process stops immediately.
Which of the following is a secure way to authenticate when cloning via HTTPS?
-
Embedding your username and password directly in the URL
-
Using SSH keys or personal access tokens
-
Cloning without authentication
-
Using the git:// protocol
Explanation:
It is unsafe to put your password in a URL.
Instead, GitHub/GitLab recommend using SSH keys or personal access tokens for secure authentication.
Quiz Completed Successfully
Your Score : 2/5
Accuracy : 0%
Login to View Explanation
1/5
1/5
< Previous
Next >
Explore
Git Introduction
Git Installation and Setup
All Git Commands
Most Used Git Commands
Git Branch
Git Merge
Git Tools and Integration
Git Remote Repositories
Collaborating with Git
Advanced Git Commands