What is git-clone?
The git-clone npm package allows you to clone Git repositories programmatically. It provides a simple interface to clone repositories from various sources, making it useful for automation scripts, CI/CD pipelines, and other scenarios where you need to programmatically interact with Git repositories.
What are git-clone's main functionalities?
Clone a repository
This feature allows you to clone a Git repository from a given URL to a specified local directory. The callback function handles any errors that occur during the cloning process.
const gitClone = require('git-clone');
gitClone('https://github.com/user/repo.git', './local-repo', (err) => {
if (err) console.error('Failed to clone repository:', err);
else console.log('Repository cloned successfully');
});
Clone a specific branch
This feature allows you to clone a specific branch of a Git repository. By providing the branch name in the options object, you can ensure that only the desired branch is cloned.
const gitClone = require('git-clone');
gitClone('https://github.com/user/repo.git', './local-repo', { checkout: 'branch-name' }, (err) => {
if (err) console.error('Failed to clone repository:', err);
else console.log('Repository cloned successfully');
});
Clone with authentication
This feature allows you to clone a Git repository that requires authentication. By including the username and password in the repository URL, you can clone private repositories.
const gitClone = require('git-clone');
gitClone('https://username:password@github.com/user/repo.git', './local-repo', (err) => {
if (err) console.error('Failed to clone repository:', err);
else console.log('Repository cloned successfully');
});
Other packages similar to git-clone
simple-git
simple-git is a lightweight interface for running Git commands in any Node.js application. It provides a more comprehensive set of Git functionalities compared to git-clone, including commit, push, pull, and more. It is useful for more complex Git operations beyond just cloning.
nodegit
nodegit is a native Node.js binding to the libgit2 library, which provides a full suite of Git functionalities. It is more powerful and flexible than git-clone, allowing for detailed manipulation of Git repositories, including creating branches, merging, and more. It is suitable for advanced users who need fine-grained control over Git operations.
isomorphic-git
isomorphic-git is a pure JavaScript implementation of Git that works in both Node.js and browser environments. It offers a wide range of Git functionalities, including cloning, committing, pushing, and pulling. It is ideal for applications that need to run Git operations in both server and client environments.
git-clone
Clone a git repository via git
shell command.
Installation
Install:
$ npm install git-clone
To use the original callback-based API:
const clone = require('git-clone');
As of 0.2.0 there's a promised-based API for use with async
/await
:
const clone = require('git-clone/promise');
API
Common Options
git
: path to git
binary; default: git
(expected to be in your $PATH
)shallow
: when true
, clone with depth 1checkout
: revision/branch/tag to check out after cloneargs
: additional array of arguments to pass to git clone
Callback
clone(repo, targetPath, [options], cb)
Clone repo
to targetPath
, calling cb
on completion; any error that occurred will be passed as the first argument. If no error is passed the git clone
operation was successful.
Promise
async clone(repo, targetPath, [options])
Clone repo
to targetPath
, throwing an exception on failure.
Contributors
Copyright & License
© 2014-2021 Jason Frame & Contributors [ @jaz303 / jason@onehackoranother.com ]
Released under the ISC license.