Skip to main content

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

repo
string
required
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
options
CloneOptions
default:"{}"
Configuration options for cloning the repository

Returns

result
Promise<string>
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:
import { git } from 'use-git'

await git.clone('[email protected]:user/repo.git')

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

Build docs developers (and LLMs) love