The git clone
copies an existing Git repository into a new directory on your local machine. Whether you're cloning a repository from GitHub, another remote server, or a local repo, git clone
sets up a version-controlled workspace for you to start working on the project.
This guide provides detailed instructions on how to use git clone
, covering various scenarios and commands.
In order to clone a repository you can either use SSH or HTTPS. For further detail see this guide on the differences between cloning with SSH and HTTPS.
Basic git clone usage
To clone a repository:
Open a terminal (or Git Bash on Windows).
Run the command:
git clone <repository-url>
Replace
<repository-url>
with the URL of the Git repository you wish to clone. This URL can be found on the repository's page on GitHub or any Git hosting service.
Cloning to a specific directory
You can also specify a specific directory on your local machine, that the repository will be cloned into:
Specify the directory name at the end of the
git clone
command:git clone <repository-url> <directory>
Replace
<directory>
with your desired directory name or file path. If the directory does not exist, Git will create it for you.
Cloning a specific branch
To clone a specific branch from a repository:
Use the
b
option withgit clone
:git clone -b <branch-name> <repository-url>
Replace
<branch-name>
with the name of the branch you want to clone.
Cloning with SSH
To clone using SSH (assuming you have set up SSH keys for your Git account):
Use the SSH URL of the repository:
git clone git@github.com:user/repository.git
This method is secure and allows you to clone repositories without entering your username and password, provided your SSH key is added to your Git account.
For more information on cloning with SSH see this walkthrough on SSH Git cloning.
Advanced cloning options
Shallow clone: To clone with limited commit history (reducing size and time), use the
-depth <depth>
option to specify the number of recent commits you want in your clone.git clone --depth 1 <repository-url>
Cloning quietly: To suppress the output of the clone command, use the
--quiet
option.
Git clone examples
Basic Repository Clone:
git clone <https://github.com/my-username/my-repo.git>
Cloning into a Specific Folder:
git clone <https://github.com/my-username/my-repo.git> ~/workspace/my-repo-folder
Cloning a specific branch:
git clone -b my-branch <https://github.com/my-username/my-repo.git>
Shallow clone of a repository:
git clone --depth 1 <https://github.com/my-username/my-repo.git>
Cloning from GitHub on different operating systems
Windows: Use Git Bash or the Windows Command Prompt with the same
git clone
command. Ensure Git is installed on your system.Linux/Ubuntu: Open the terminal and use the
git clone
command. Git might need to be installed using your distribution’s package manager.Mac: Open the Terminal app and use the
git clone
command. Git comes pre-installed on most Macs, or it can be installed via Xcode Command Line Tools. For further detail see this guide on installing Git on Mac.
Troubleshooting common issues
Not cloning all files: Ensure you have access to all branches and files. Some files might be ignored by
.gitignore
or stored in LFS, Git’s solution for storing large files not compatible with the traditional Git storage schema.Permission denied: When cloning via SSH, ensure your public key is added to your Git account. For HTTPS, check if your access token or password is correct.
For further reading on cloning with Git, see the official Git documentation.