LinuxCommandLibrary

git-reset-file

Restore tracked file's content from index

TLDR

Reset a file to HEAD

$ git reset-file [path/to/file]
copy

Reset a file to a specific commit
$ git reset-file [path/to/file] [commit_hash]
copy

SYNOPSIS

git reset-file [options] [--] <file>...

PARAMETERS

--hard
    Reset both index and working tree to specified commit (destructive)

--soft
    Reset index only, keep working tree changes as staged

--mixed
    Reset index, leave working tree changes unstaged (default)

<commit>
    Reset file to version from given commit (default: HEAD)

--patch
    Interactively select hunks to reset

DESCRIPTION

git-reset-file is a non-standard utility sometimes used to reset a tracked file in a Git repository back to the state in the index (staging area) or a specified commit, discarding local modifications.

This command targets individual files without affecting the entire working tree or commit history, making it ideal for undoing changes to specific files after accidental edits. It permanently deletes uncommitted changes, so use with caution.

Typically implemented as an alias or script equivalent to git checkout HEAD -- <file> (resets working tree to HEAD) or git reset HEAD -- <file> (unstages file from index). In Git 2.23+, git restore is the modern replacement: git restore --source=HEAD -- <file> for working tree, or git restore --staged <file> for index.

Workflow example: Edit a file, realize mistake, run git reset-file file.txt to revert. It does not create new commits; changes are simply overwritten.

Common in custom Git wrappers or IDE integrations for quick file recovery, but relying on core Git commands ensures portability across versions and environments.

CAVEATS

Non-standard command: git-reset-file does not exist in core Git. Use git checkout HEAD -- <file>, git reset HEAD -- <file>, or git restore instead. Uncommitted changes are lost irreversibly; always git stash first if needed.
Works only on tracked files.

COMMON USAGE

git reset-file README.md
Discard changes to README.md in working tree.

git reset-file --staged src/main.c
Unstage main.c (if alias supports).

SAFER ALTERNATIVE

Prefer: git restore --source=HEAD -- README.md
Modern, explicit, non-destructive preview with git diff first.

HISTORY

File-specific reset functionality core to Git since v1.6.0 (2007) via git reset <paths> and git checkout -- <paths>. Dedicated git restore added in Git 2.23 (2019) for clarity. Custom git-reset-file appears in third-party tools/extensions for user convenience.

SEE ALSO

Copied to clipboard