Overview
The clone() function clones a Git repository into a new directory. This creates a complete copy of the repository, including all history, branches, and tags.
Syntax
function clone ( repo : string , options ?: CloneOptions ) : Promise < string >
Parameters
The repository URL to clone. Supports HTTP(S), SSH, and Git protocols. Examples:
https://github.com/user/repo.git
[email protected] :user/repo.git
git://github.com/user/repo.git
Configuration options for cloning the repository The name of the directory to clone into. If not specified, Git uses the repository name.
Array of Git flags to modify cloning behavior Available flags:
"--local" - Optimize for local cloning
"--no-hardlinks" - Copy files instead of hardlinking
"--shared" - Share objects with source repository
"--dissociate" - Borrow objects temporarily
"--quiet" - Suppress progress output
"--progress" - Show progress output
"--no-checkout" - Skip checkout of HEAD
"--reject-shallow" - Fail if source is shallow
"--no-reject-shallow" - Allow shallow source
"--bare" - Create a bare repository
"--sparse" - Initialize sparse checkout
"--also-filter-submodules" - Apply filter to submodules
"--mirror" - Set up mirror of source repository
"--no-tags" - Don’t clone any tags
"--single-branch" - Clone only one branch
"--no-single-branch" - Clone all branches
Use partial clone feature to filter objects. Example: "blob:none" to omit blobs.
Use a custom name for the remote instead of the default origin.
Clone a specific branch instead of the default branch.
Checkout a specific revision (commit, tag, or branch) after cloning.
Specify the path to git-upload-pack on the remote.
Create a shallow clone with history truncated to the specified number of commits.
Returns
A promise that resolves with the output message from Git after successfully cloning the repository.
Usage Examples
Basic Clone
Clone a repository using HTTPS:
import { git } from 'use-git'
await git . clone ( 'https://github.com/user/repo.git' )
// Clones into ./repo directory
Clone into Custom Directory
Specify a custom directory name:
import { git } from 'use-git'
await git . clone ( 'https://github.com/user/repo.git' , {
dir: 'my-project'
})
// Clones into ./my-project directory
Clone Specific Branch
Clone only a specific branch:
import { git } from 'use-git'
await git . clone ( 'https://github.com/user/repo.git' , {
'--branch' : 'develop' ,
flags: [ '--single-branch' ]
})
Shallow Clone
Create a shallow clone with limited history:
import { git } from 'use-git'
await git . clone ( 'https://github.com/user/repo.git' , {
'--depth' : 1
})
// Only clones the latest commit
Shallow clones are faster and use less disk space, making them ideal for CI/CD environments.
Clone with SSH
Clone using SSH protocol:
Bare Clone
Create a bare repository clone:
import { git } from 'use-git'
await git . clone ( 'https://github.com/user/repo.git' , {
flags: [ '--bare' ],
dir: 'repo.git'
})
Quiet Clone
Clone without progress output:
import { git } from 'use-git'
await git . clone ( 'https://github.com/user/repo.git' , {
flags: [ '--quiet' ]
})
Partial Clone
Use partial clone to reduce download size:
import { git } from 'use-git'
await git . clone ( 'https://github.com/user/repo.git' , {
'--filter' : 'blob:none'
})
// Omits blob objects initially
Clone with Custom Remote Name
Use a custom name for the remote:
import { git } from 'use-git'
await git . clone ( 'https://github.com/user/repo.git' , {
'--origin' : 'upstream'
})
Mirror Clone
Create a mirror of the repository:
import { git } from 'use-git'
await git . clone ( 'https://github.com/user/repo.git' , {
flags: [ '--mirror' ],
dir: 'repo-mirror.git'
})
Multiple Options
Combine multiple options:
import { git } from 'use-git'
await git . clone ( 'https://github.com/user/repo.git' , {
dir: 'my-project' ,
'--branch' : 'main' ,
'--depth' : 1 ,
flags: [ '--single-branch' , '--quiet' ]
})
Error Handling
Handle clone errors gracefully:
import { git } from 'use-git'
try {
await git . clone ( 'https://github.com/user/repo.git' )
console . log ( 'Repository cloned successfully' )
} catch ( error ) {
console . error ( 'Clone failed:' , error . message )
}
init - Initialize a new repository
info - Check Git installation and version information
Git Documentation
For more information about git clone, see the official Git documentation .
Source Code
Location: src/lib/clone/clone.ts:9