Skip to main content

diffCommits

Show changes between two commits.

Function Signature

function diffCommits(
  from: string,
  to: string,
  paths?: readonly string[]
): Promise<string>

Parameters

from
string
required
Base commit reference.
to
string
required
Target commit reference.
paths
readonly string[]
Optional paths to limit the diff output.

Example

const changes = await git.diffCommits("HEAD~1", "HEAD")
const srcChanges = await git.diffCommits("main", "dev", ["src"])

diffFiles

Compare two files directly, even if they are outside a Git repository.

Function Signature

function diffFiles(
  fileA: string,
  fileB: string
): Promise<string>

Parameters

fileA
string
required
First file path.
fileB
string
required
Second file path.

Example

const diff = await git.diffFiles("config.json", "config.backup.json")

diffHead

Show changes between HEAD and the working tree.

Function Signature

function diffHead(
  paths?: readonly string[]
): Promise<string>

Parameters

paths
readonly string[]
Optional paths to limit the diff output.

Example

const changes = await git.diffHead()
const readmeChanges = await git.diffHead(["README.md"])

diffRange

Show changes for a commit range.

Function Signature

function diffRange(
  range: `${string}...${string}`,
  paths?: readonly string[]
): Promise<string>

Parameters

range
string
required
Commit range in A...B format (three-dot notation).
paths
readonly string[]
Optional paths to limit the diff output.

Example

const changes = await git.diffRange("main...dev")
const releaseChanges = await git.diffRange("v1.0.0...v1.1.0", ["src"])

diffStaged

Show changes staged in the index.

Function Signature

function diffStaged(
  paths?: readonly string[]
): Promise<string>

Parameters

paths
readonly string[]
Optional paths to limit the diff output.

Example

const staged = await git.diffStaged()
const stagedSrc = await git.diffStaged(["src/index.ts"])

diffWorkingTree

Show changes in the working tree compared to the index.

Function Signature

function diffWorkingTree(
  paths?: readonly string[]
): Promise<string>

Parameters

paths
readonly string[]
Optional paths to limit the diff output.

Example

const changes = await git.diffWorkingTree()
const srcChanges = await git.diffWorkingTree(["src"])

getChangedFiles

Get a list of files changed in the working tree.

Function Signature

function getChangedFiles(): Promise<string[]>

Returns

result
Promise<string[]>
Array of changed file paths.

Example

const files = await git.getChangedFiles()
console.log(files) // ["src/index.ts", "README.md"]

getStagedFiles

Get a list of files that are currently staged.

Function Signature

function getStagedFiles(): Promise<string[]>

Returns

result
Promise<string[]>
Array of staged file paths.

Example

const files = await git.getStagedFiles()
console.log(files) // ["src/index.ts"]

changedFileCount

Get the number of files changed in the working tree.

Function Signature

function changedFileCount(): Promise<number>

Returns

result
Promise<number>
Number of changed files.

Example

const count = await git.changedFileCount()
console.log(`${count} files changed`)

stagedFileCount

Get the number of staged files.

Function Signature

function stagedFileCount(): Promise<number>

Returns

result
Promise<number>
Total count of staged files.

Example

const count = await git.stagedFileCount()
console.log(`${count} files staged`)

hasDiff

Check whether the working tree contains unstaged changes.

Function Signature

function hasDiff(): Promise<boolean>

Returns

result
Promise<boolean>
true if unstaged changes exist, otherwise false.

Example

if (await git.hasDiff()) {
  console.log("Working tree has changes")
}

hasStagedDiff

Check whether the index contains staged changes.

Function Signature

function hasStagedDiff(): Promise<boolean>

Returns

result
Promise<boolean>
true if staged changes exist, otherwise false.

Example

if (await git.hasStagedDiff()) {
  console.log("There are staged changes")
}

hasBinaryChanges

Check whether the diff contains binary file changes.

Function Signature

function hasBinaryChanges(): Promise<boolean>

Returns

result
Promise<boolean>
true if binary changes are present, otherwise false.

Example

if (await git.hasBinaryChanges()) {
  console.log("Binary file changes present")
}

hasOnlyWhitespaceChanges

Check whether all working tree changes are whitespace-only.

Function Signature

function hasOnlyWhitespaceChanges(): Promise<boolean>

Returns

result
Promise<boolean>
true if all changes are whitespace-only, otherwise false.

Example

if (await git.hasOnlyWhitespaceChanges()) {
  console.log("Only formatting changes detected")
}

hasOnlyEOLChanges

Check whether all changes are end-of-line only (CRLF ↔ LF).

Function Signature

function hasOnlyEOLChanges(): Promise<boolean>

Returns

result
Promise<boolean>
true if all changes are EOL-only, otherwise false.

Example

if (await git.hasOnlyEOLChanges()) {
  console.log("Only EOL changes detected")
}

Build docs developers (and LLMs) love