AI Hints
Home›Git & GitHub Tutorial›Undoing Changes
Git & GitHub · Lesson 25 of 144

Unstaging a File

Git restore --staged.

⏱ 2 min read▶ Video at 26:10🎓 Free
Watch this lesson in the Git & GitHub courseWatch on YouTube ↗Full Git & GitHub course

Sometimes you git add a file by accident. You don't want to lose your edits. You just want to take the file out of the staging box.

Unstaging a File
From the video: Unstaging a File
  • You staged something by accident
  • Keep the edits
  • Just take it out of the box

Taking it back out

We changed two files, and added both with git add dot. But the style change isn't ready yet. Status shows both are staged. Now git restore, dash dash staged, and the style file. Status again. The page is still staged, but the style file is back to modified, and not staged. And our edit is still there in the file.

git
git add .
git status --short
M  index.html
M  style.css
git restore --staged style.css
git status --short
M  index.html
 M style.css
cat style.css
body { font-family: sans-serif; }
p { color: gray; }

Two kinds of restore

So there are two restores. With dash dash staged, it takes the file out of staging, and keeps your edits. Without it, it throws your edits away. Git status even reminds you of both commands, every time you run it.

git restore --staged

  • Unstages the file
  • Your edits stay

git restore

  • Discards the edits
  • Back to last commit

Summary

git restore dash dash staged unstages a file but keeps your work. Next, fixing a commit you just made.

Key takeaways
  • git restore --staged <file>
  • Edits are kept
  • Status shows the hints
Next up: Fixing the Last Commit — git commit --amend