git restore --staged
- Version
- 2.51
- Since
- 2.23
- Review
- Draft
Summary#
git restore --staged updates the index for the paths you name. Use it to unstage an edit while keeping the edited file in your working tree.
Syntax#
git restore --staged -- path/to/file
Example#
(
set -eu
scratch=$(mktemp -d "${TMPDIR:-/tmp}/devlookup-git.XXXXXX")
trap 'rm -rf -- "$scratch"' EXIT
unset GIT_CONFIG_GLOBAL $(git rev-parse --local-env-vars)
export GIT_CONFIG_NOSYSTEM=1 GIT_TEMPLATE_DIR= LC_ALL=C
export HOME="$scratch" XDG_CONFIG_HOME="$scratch" GIT_ATTR_NOSYSTEM=1
mkdir "$scratch/work"
cd "$scratch/work"
git init -q -b trunk
printf 'original\n' > note.txt
git add note.txt
git -c user.name="Reference Example" -c user.email=example@example.invalid commit -qm "Add note"
printf 'edited\n' > note.txt
git add note.txt
git status --short
git restore --staged -- note.txt
git status --short
)
Expected output#
M note.txt
M note.txt
Common mistakes#
--staged selects the index and uses HEAD as the default source. Before the first commit, use git rm --cached -- path/to/file to unstage an unchanged file you just added. Adding --worktree also updates the file on disk and can overwrite edits. Use -- before paths that might be mistaken for options.
Version applicability#
Git 2.23 added git restore as a dedicated path restoration command. Git 2.27 made HEAD the default source when combining --staged and --worktree. Before Git 2.23, use git reset HEAD -- path/to/file to unstage a tracked edit.
The complete example requires Git 2.28 or later for git init -b.
Sources#
- Attribution
- DevLookup contributors
- License
- CC-BY-4.0