Skip to main content
These functions provide simplified interfaces for common restore scenarios. All variants are built on top of the core restore() function.

restoreAll

function restoreAll(): Promise<string>
Restore all working tree files from the index. Effect: Discards all unstaged changes in the working tree, restoring files to their last staged state. Example:
await git.restoreAll()
Source: src/lib/restore/restoreAll.ts:16

restoreAllFromHead

function restoreAllFromHead(): Promise<string>
Restore all files in both the index and working tree from HEAD. Effect: Discards all staged and unstaged changes. This is equivalent to a hard reset but uses the restore command. Example:
await git.restoreAllFromHead()
Equivalent to:
await git.restore(".", {
  "--source": "HEAD",
  flags: ["--staged", "--worktree"],
})
Source: src/lib/restore/restoreAllFromHead.ts:15

restoreAllStaged

function restoreAllStaged(): Promise<string>
Restore all staged files back to HEAD, keeping working tree changes. Effect: Unstages all files while preserving your local modifications. This is useful when you want to unstage everything but keep your work. Example:
await git.restoreAllStaged()
Equivalent to:
await git.restore(".", { flags: ["--staged"] })
Source: src/lib/restore/restoreAllStaged.ts:13

restoreBoth

function restoreBoth(
  paths: string | string[],
  source?: string
): Promise<string>
Restore files in both the index and working tree from a given source.

Parameters

paths
string | string[]
required
The file path(s) to restore.
source
string
default:"HEAD"
The Git tree-ish to restore from.Examples:
  • "HEAD" (default)
  • "main" - Branch name
  • "HEAD~1" - Previous commit
  • "abc123" - Commit SHA

Returns

A promise that resolves with the command output.

Examples

Restore from HEAD:
await git.restoreBoth("file.txt")
Restore multiple files from a commit:
await git.restoreBoth(["file1.txt", "file2.txt"], "abc123")
Source: src/lib/restore/restoreBoth.ts:17

restoreFrom

function restoreFrom(
  source: string,
  paths: string | string[]
): Promise<string>
Restore a file from a specific commit, branch, or tag. Note: The parameter order is reversed from restoreBoth() - source comes first.

Parameters

source
string
required
The commit, branch, or tag to restore from.
paths
string | string[]
required
The file path(s) to restore.

Returns

A promise that resolves with the command output.

Example

await git.restoreFrom("HEAD~1", "src/index.ts")
Source: src/lib/restore/restoreFrom.ts:13

restoreFromHead

function restoreFromHead(
  paths: string | string[]
): Promise<string>
Restore files from HEAD, discarding both staged and unstaged changes. Effect: Completely resets the specified files to their HEAD state, removing all local modifications.

Parameters

paths
string | string[]
required
The file path(s) to restore.

Returns

A promise that resolves with the command output.

Example

await git.restoreFromHead("file.txt")
Equivalent to:
await git.restore("file.txt", {
  flags: ["--staged", "--worktree"],
  "--source": "HEAD",
})
Source: src/lib/restore/restoreFromHead.ts:13

restoreStaged

function restoreStaged(
  paths: string | string[]
): Promise<string>
Restore files in the index (staging area) to match HEAD. Effect: Unstages the specified files while keeping working tree changes intact. This is the equivalent of git reset HEAD <file> in older Git versions.

Parameters

paths
string | string[]
required
The file path(s) to unstage.

Returns

A promise that resolves with the command output.

Example

await git.restoreStaged("file.txt")
Equivalent to:
await git.restore("file.txt", { flags: ["--staged"] })
Source: src/lib/restore/restoreStaged.ts:13

restoreWorktree

function restoreWorktree(
  paths: string | string[]
): Promise<string>
Restore files in the working tree from the index. Effect: Discards unstaged changes to the specified files, restoring them to their staged state. This is the default behavior of restore() but made explicit.

Parameters

paths
string | string[]
required
The file path(s) to restore.

Returns

A promise that resolves with the command output.

Example

await git.restoreWorktree("file.txt")
Equivalent to:
await git.restore("file.txt", { flags: ["--worktree"] })
Source: src/lib/restore/restoreWorktree.ts:13

Comparison Table

FunctionAffects IndexAffects WorktreeRequires Source
restoreAll()NoYes (all files)No
restoreAllFromHead()YesYes (all files)No (uses HEAD)
restoreAllStaged()YesNoNo
restoreBoth()YesYesOptional
restoreFrom()NoYesRequired
restoreFromHead()YesYesNo (uses HEAD)
restoreStaged()YesNoNo
restoreWorktree()NoYesNo

Common Scenarios

Unstage a file

// Keep working tree changes, just unstage
await git.restoreStaged("file.txt")

Discard local changes

// Discard unstaged changes only
await git.restoreWorktree("file.txt")

// Discard both staged and unstaged changes
await git.restoreFromHead("file.txt")

Restore from a different commit

// Restore to working tree only
await git.restoreFrom("HEAD~2", "file.txt")

// Restore to both index and working tree
await git.restoreBoth("file.txt", "HEAD~2")

Unstage everything

await git.restoreAllStaged()

Reset all changes

// Discard all uncommitted changes (staged + unstaged)
await git.restoreAllFromHead()
  • restore - Core restore function with full options

Build docs developers (and LLMs) love