A lightweight interface for running git commands in any node.js application.
Use your favourite package manager:
Requires git to be installed and that it can be called using the command git.
Include into your JavaScript app using common js:
// require the library, main export is a function
const simpleGit = require('simple-git');
simpleGit().clean(simpleGit.CleanOptions.FORCE);
// or use named properties
const { simpleGit, CleanOptions } = require('simple-git');
simpleGit().clean(CleanOptions.FORCE);
Include into your JavaScript app as an ES Module:
import { simpleGit, CleanOptions } from 'simple-git';
simpleGit().clean(CleanOptions.FORCE);
Include in a TypeScript app using the bundled type definitions:
import { simpleGit, SimpleGit, CleanOptions } from 'simple-git';
const git: SimpleGit = simpleGit().clean(CleanOptions.FORCE);
Configure each simple-git instance with a properties object passed to the main simpleGit function:
import { simpleGit, SimpleGit, SimpleGitOptions } from 'simple-git';
const options: Partial<SimpleGitOptions> = {
baseDir: process.cwd(),
binary: 'git',
maxConcurrentProcesses: 6,
trimmed: false,
};
// when setting all options in a single object
const git: SimpleGit = simpleGit(options);
// or split out the baseDir, supported for backward compatibility
const git: SimpleGit = simpleGit('/some/path', { binary: 'git' });
The first argument can be either a string (representing the working directory for git commands to run in),
SimpleGitOptions object or undefined, the second parameter is an optional SimpleGitOptions object.
All configuration properties are optional, the default values are shown in the example above.
To prefix the commands run by simple-git with custom configuration not saved in the git config (ie: using the
-c command) supply a config option to the instance builder:
// configure the instance with a custom configuration property
const git: SimpleGit = simpleGit('/some/path', { config: ['http.proxy=someproxy'] });
// any command executed will be prefixed with this config
// runs: git -c http.proxy=someproxy pull
await git.pull();
AbortController
Terminate pending and future tasks in a simple-git instance (requires node >= 16).
Custom Binary
Customise the git binary simple-git uses when spawning git child processes.
Completion Detection
Customise how simple-git detects the end of a git process.
Error Detection
Customise the detection of errors from the underlying git process.
Progress Events
Receive progress events as git works through long-running processes.
Spawned Process Ownership
Configure the system uid / gid to use for spawned git processes.
Timeout
Automatically kill the wrapped git process after a rolling timeout.
Unsafe
Selectively opt out of simple-git safety precautions - for advanced users and use cases.
Each task in the API returns the simpleGit instance for chaining together multiple tasks, and each
step in the chain is also a Promise that can be await ed in an async function or returned in a
Promise chain.
const git = simpleGit();
// chain together tasks to await final result
await git.init().addRemote('origin', '...remote.git');
// or await each step individually
await git.init();
await git.addRemote('origin', '...remote.git');
To catch errors in async code, either wrap the whole chain in a try/catch:
const git = simpleGit();
try {
await git.init();
await git.addRemote(name, repoUrl);
} catch (e) {
/* handle all errors here */
}
or catch individual steps to permit the main chain to carry on executing rather than
jumping to the final catch on the first error:
const git = simpleGit();
try {
await git.init().catch(ignoreError);
await git.addRemote(name, repoUrl);
} catch (e) {
/* handle all errors here */
}
function ignoreError() {}
In addition to returning a promise, each method can also be called with a trailing callback argument to handle the result of the task.
const git = simpleGit();
git.init(onInit).addRemote('origin', 'git@github.com:steveukx/git-js.git', onRemoteAdd);
function onInit(err, initResult) {}
function onRemoteAdd(err, addRemoteResult) {}
If any of the steps in the chain result in an error, all pending steps will be cancelled, see the parallel tasks section for more information on how to run tasks in parallel rather than in series .
Whether using a trailing callback or a Promise, tasks either return the raw string or Buffer response from the
git binary, or where possible a parsed interpretation of the response.
For type details of the response for each of the tasks, please see the TypeScript definitions.
From v3 of simple-git you can now import as an ES module, Common JS module or as TypeScript with bundled type
definitions. Upgrading from v2 will be seamless for any application not relying on APIs that were marked as deprecated
in v2 (deprecation notices were logged to stdout as console.warn in v2).
| API | What it does |
|---|---|
.add([fileA, ...], handlerFn) |
adds one or more files to be under source control |
.addAnnotatedTag(tagName, tagMessage, handlerFn) |
adds an annotated tag to the head of the current branch |
.addTag(name, handlerFn) |
adds a lightweight tag to the head of the current branch |
.catFile(options, [handlerFn]) |
generate cat-file detail, options should be an array of strings as supported arguments to the cat-file command |
.checkIgnore([filepath, ...], handlerFn) |
checks if filepath excluded by .gitignore rules |
.commit(message, handlerFn) |
commits changes in the current working directory with the supplied message where the message can be either a single string or array of strings to be passed as separate arguments (the git command line interface converts these to be separated by double line breaks) |
.commit(message, [fileA, ...], options, handlerFn) |
commits changes on the named files with the supplied message, when supplied, the optional options object can contain any other parameters to pass to the commit command, setting the value of the property to be a string will add name=value to the command string, setting any other type of value will result in just the key from the object being passed (ie: just name), an example of setting the author is below |
.customBinary(gitPath) |
sets the command to use to reference git, allows for using a git binary not available on the path environment variable docs |
.env(name, value) |
Set environment variables to be passed to the spawned child processes, see usage in detail below. |
.exec(handlerFn) |
calls a simple function in the current step |
.fetch([options, ] handlerFn) |
update the local working copy database with changes from the default remote repo and branch, when supplied the options argument can be a standard options object either an array of string commands as supported by the git fetch. |
.fetch(remote, branch, handlerFn) |
update the local working copy database with changes from a remote repo |
.fetch(handlerFn) |
update the local working copy database with changes from the default remote repo and branch |
.outputHandler(handlerFn) |
attaches a handler that will be called with the name of the command being run and the stdout and stderr readable streams created by the child process running that command, see [examples](https://github.com/steveukx/git-js/blob/mai |
$ claude mcp add git-js \
-- python -m otcore.mcp_server <graph>