clone Git Command Guide
The git clone command creates a copy of an existing Git repository, allowing developers to obtain a complete copy of the repository’s history, branches, tags, and files.
git clone Syntax:
Section titled “git clone Syntax:”git clone [<options>] [--] <repository> [<directory>]Options:
Section titled “Options:”| Option | Description |
|---|---|
| —bare | Create a bare repository |
| —mirror | Create a mirror of the source repository |
| —depth | Create a shallow clone with a history truncated to the specified number of commits |
| —single-branch | Clone only a single branch |
| —no-single-branch | Clone all branches (default) |
| —branch | Clone specific branch instead of HEAD |
| —origin | Use |
| —recursive | Initialize and clone all submodules |
| —recurse-submodules | Same as —recursive |
| —recursive-submodules | Initialize and clone all submodules recursively |
| —shallow-submodules | Clone submodules with —depth=1 |
| —no-shallow-submodules | Do not clone submodules with —depth=1 |
| —template= | Directory from which templates are used |
| —local | When cloning from local repository, do not use hardlinks |
| —shared | When cloning from local repository, use hardlinks |
| —archive= | Create archive of repository at HEAD in |
| —dissociate | Borrow objects from reference repositories |
| —separate-git-dir= | Place cloned .git directory in |
| —config | Set config inside the new repository |
| —quiet, -q | Operate quietly |
| —verbose, -v | Be verbose |
| —progress | Force progress reporting |
| —no-progress | Suppress progress reporting |
Parameters:
Section titled “Parameters:”| Parameter | Description |
|---|---|
| URL or path of the repository to clone | |
| Name of the target directory (optional, defaults to repository name) |
git clone Command Samples:
Section titled “git clone Command Samples:”Basic repository clone
Section titled “Basic repository clone”git clone https://github.com/user/repo.gitClones the repository into a directory named ‘repo’.
Clone to specific directory
Section titled “Clone to specific directory”git clone https://github.com/user/repo.git my-projectClones into directory named ‘my-project’.
Clone specific branch
Section titled “Clone specific branch”git clone -b feature-branch https://github.com/user/repo.gitClones only the ‘feature-branch’ branch.
Shallow clone for large repositories
Section titled “Shallow clone for large repositories”git clone --depth 1 https://github.com/user/repo.gitCreates a shallow clone with only the latest commit.
Clone with submodules
Section titled “Clone with submodules”git clone --recursive https://github.com/user/repo.gitClones the repository and initializes all submodules.
Clone over SSH
Section titled “Clone over SSH”git clone git@github.com:user/repo.gitUses SSH authentication for cloning.
Bare clone for server hosting
Section titled “Bare clone for server hosting”git clone --bare https://github.com/user/repo.git repo.gitCreates a bare repository without a working directory.
Mirror clone for backup
Section titled “Mirror clone for backup”git clone --mirror https://github.com/user/repo.gitCreates a complete mirror of the source repository.
Clone single branch
Section titled “Clone single branch”git clone --single-branch -b main https://github.com/user/repo.gitClones only the ‘main’ branch and sets it up for single branch tracking.
How do I clone a Git repository?
Section titled “How do I clone a Git repository?”To clone a Git repository, use:
git clone <repository-url>How can I clone a specific branch?
Section titled “How can I clone a specific branch?”To clone a specific branch, run:
git clone -b <branch-name> <repository-url>How do I create a shallow clone?
Section titled “How do I create a shallow clone?”To create a shallow clone, execute:
git clone --depth <number> <repository-url>How can I clone a repository with submodules?
Section titled “How can I clone a repository with submodules?”To clone a repository with submodules, use:
git clone --recursive <repository-url>How do I clone over SSH?
Section titled “How do I clone over SSH?”To clone over SSH, run:
git clone git@<host>:<user>/<repo>.gitApplications of the git clone command
Section titled “Applications of the git clone command”- Setting up local copies of remote repositories for development
- Creating backups of repositories as bare clones
- Forking repositories to start new projects
- Contributing to open source projects
- Setting up continuous integration environments
- Creating mirrors for distributed development teams