You staged a file before it was ready, but you want to keep editing it. The operation you need changes the index while leaving the working copy in place.
What you will build
Use a disposable repository to see the difference between the index and working tree when git restore --staged runs.
Before you start
Git 2.23 or later and a terminal. The commands create a new practice directory.
1. Create unstage.sh
mkdir git-unstage-practice
cd git-unstage-practice
git init
git config user.name "Tutorial Example"
git config user.email "[email protected]"
printf 'first version\n' > notes.txt
git add notes.txt
git commit -m "Add notes"
printf 'second line\n' >> notes.txt
git add notes.txt
git status --short
git restore --staged -- notes.txt
git status --short
git diff -- notes.txt
2. Run the example
bash unstage.shExpected result
Before restore, short status shows M in the index column. Afterwards it shows M in the working-tree column, and git diff still displays the added line.
How it works
Git tracks your last commit, the index used to prepare the next commit, and the working tree you edit. The --staged option selects the index as the restore destination.
After unstaging, inspect both git diff and git diff --cached. The first shows working-tree changes relative to the index; the second shows what would enter the next commit. In this fixture the cached diff should be empty.
The double dash separates options from path names. Get used to including it when a command accepts paths, especially when file names might begin with a dash.
This example commits an initial version so HEAD exists. A brand-new repository with no commits is a different case. Run the exercise in its own directory and read status before applying a command to an important project.
Test the edge cases
- Run cat notes.txt and confirm the second line remains.
- Confirm git diff --cached produces no changes.
- Stage notes.txt again and verify it reappears in the index column.
Keep learning
Browse related tutorials. Reference: official documentation.
Reviewed 7 November 2026. AI-dependent examples require your own local model; generated text varies.