node package manager
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »



A Node.js module for interacting with git repositories.


$ npm install gitjs


var git = require('gitjs'); ( repoPath[, autoCreate = false], callback )

Opens a git repository so git commands can be run. If the autoCreate parameter is given and is truthy, then the open function will init a new repository if one is not already in the given directory.'/path/to/repo', function(err, repo) {
    // ... 

git.init ( repoPath, callback )

Intializes a new git repo in the given directory.

git.init('/path/to/project', function(err, repo) {
    // ... 

Repo::run ( cmd[, args], callback )

Runs a git command in the repository. If an args array is given, it will be used to populate ? placeholders in the command. As an example, on the command line you would create a new branch like so:

$ git branch foo

The same could be done with gitjs like this:'branch foo', function(err, stdout, stderr) {
    // ... 

Also, you could make the branch name variable using the args parameter like this:

function createBranch(repo, branchName) {'branch ?', [branchName], function(err, stdout, stderr) {
        // ... 

Repo::add ( <String|Array> files, callback )

Stages files to be committed.

// All of the following are acceptable formats for files 
repo.add('*', function(err, stdout, stderr) {
    // ... 
repo.add('.', ...);
repo.add('foo.txt bar.txt', ...);
repo.add([ 'baz.txt', 'foo.html', 'bar.css' ], ...);

Repo::commit ( message, callback )

Commits the repository changes.

repo.commit('Changed foo.txt', function(err, stdout, stderr) {
    // ... 

Repo::commitAll ( message, callback )

Commits the repository changes with a git commit -a.

Repo::cloneTo ( target, callback )

Does a local clone of the repository to the given target path.

repo.cloneTo('/path/to/clone', function(err, stdout, stderr) {
    // ... 

Repo::clean ( dirs, callback )

Runs a git clean. If the dirs parameter is truthy, a -d flag will be used.

Repo::listBranches ([ giveObjs = false,] callback )

Gets an array of branch names for the repository. If the giveObjs parameter is given and is truthy, objects about the branches will be given instead of just names.

repo.listBranches(function(err, branches) {
    console.log(branches[0]);  // "master" 
repo.listBranches(true, function(err, branches) {
    console.log(branches[0]);  // { name: "master", isCurrent: true } 

Repo::currentBranch ( callback )

Gets the name of the current branch.

repo.currentBranch(function(err, branch) {
    console.log(branch);  // "master" 

Repo::listRemotes ( callback )

Gets the names of all remotes for the repository.

repo.listRemotes(function(err, remotes) {
    console.log(remotes[0]);  // "origin" 

Repo::remoteExists ( remote, callback)

Determines if a given remote exists.

repo.remoteExists('origin', function(err, exists) {
    console.log(exists);  // true 

Repo::createBranch ( name, callback )

Creates a new branch.

repo.createBranch('dev', function(err, stdout, stderr) {
    // ... 

Repo::deleteBranch ( name[, force = false], callback )

Deletes a branch from the repository. If the force parameter is given and is truthy, the -f flag will be given, forcing the branch to be deleted, even if it has unmerged changes.

Repo::checkout ( name, callback )

Checks out the given branch.

repo.checkout('master', function(err, stdout, stderr) {
    // ... 

Repo::status ( callback )

Runs a git status --porcelain -z command and parses the results into an array of files and the changes. Each value in the array is an object with the file name and the x and y change flags as defined in the git man pages.

    x: 'M', y: ' ',
    file: 'foo.txt'

Repo::push ( callback )

Pushing commits to git

repo.push(function(err, stdout, stderr) {
    // ...