Skip to main content

Overview

The createGit() function creates a new Git instance with custom configuration options. This is the primary way to initialize the useGit library and set up your working directory and debug settings.

Syntax

function createGit(options?: CreateGit): typeof git

Parameters

options
CreateGit
default:"{}"
Configuration options for the Git instance

Returns

git
GitInstance
Returns a new Git instance with all available Git methods. The instance includes all methods from the library including init, clone, commit, branch, and more.

Usage Examples

Basic Usage

Create a Git instance with default settings:
import { createGit } from 'use-git'

const git = createGit()

// Use the git instance
await git.init()

Custom Working Directory

Specify a custom working directory:
import { createGit } from 'use-git'

const git = createGit({ 
  cwd: '/path/to/my/project' 
})

await git.status()

Debug Mode

Enable debug mode to see detailed Git command output:
import { createGit } from 'use-git'

const git = createGit({ 
  debug: true 
})

// All Git commands will now show debug output
await git.clone('https://github.com/user/repo.git')

Multiple Instances

Create multiple Git instances for different directories:
import { createGit } from 'use-git'

const projectA = createGit({ cwd: './project-a' })
const projectB = createGit({ cwd: './project-b' })

// Work with different repositories independently
await projectA.status()
await projectB.status()

Default Export

The library also provides a default Git instance for convenience:
import git from 'use-git'
// or
import { git } from 'use-git'

// Use immediately without configuration
await git.init()
await git.status()
The default git export is equivalent to calling createGit() with no options.
The default git instance uses the current working directory (.) and has debug mode disabled.

Source Code

Location: src/index.ts:9

Build docs developers (and LLMs) love