Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Remove a File from git add (Unstage It)

· 4 min read · Home Tech

In this article
  1. Check What Is Staged Before You Unstage Anything
  2. Unstage the File with git restore
  3. Special Case: A File You Never Want Git to Track
  4. Symptoms, Causes and Fixes After Unstaging
  5. How to Tell It Worked, and What to Do Next

Run git restore --staged to unstage one file, or git restore --staged . to unstage everything, then your next commit will not include it. The old command git reset HEAD does the same thing.

Time 2 min · Steps 6 · Level Easy

The mistake people make here is running git reset --hard, which throws away real edits, when all they wanted was to pull a file back out of the staging area. git add only stages a copy of the file; unstaging it never touches the file on disk. The commands below keep your work exactly as it is.

Check What Is Staged Before You Unstage Anything

Before running any command, look at what Git thinks is staged. Open a terminal in the repository folder and run git status. You will see a block headed "Changes to be committed" — those are the files your next commit would include — and below it, "Changes not staged for commit" and "Untracked files". Only the first block is affected by unstaging.

For a fuller view, run git diff --staged. This shows the exact line-by-line changes that are staged, so you can confirm the file you are about to unstage really is the one you added by mistake.

Unstage the File with git restore

  1. Open the repository in a terminal. On Windows use Git Bash or the terminal built into your editor; on macOS or Linux use Terminal, then change into the project folder with cd.
  2. Run git status. Find the file under "Changes to be committed" and copy its path exactly as shown.
  3. Unstage one file with git restore --staged path/to/file. Replace the path with the one from the previous step. The file drops out of the staged list but every edit stays on disk.
  4. Unstage everything at once with git restore --staged . Run this from the repository root; the dot means "all staged paths".
  5. Unstage a folder with git restore --staged path/to/folder. Every file under that folder leaves the staging area in one go.
  6. Confirm with git status. The file should now appear under "Changes not staged for commit" (if it is tracked) or "Untracked files" (if it is new), and nothing is lost.

If your Git version predates git restore, the equivalent commands are git reset HEAD path/to/file and git reset HEAD for everything. Command names and output labels can differ slightly between Git versions, but these two forms have worked for many years.

Special Case: A File You Never Want Git to Track

If the file you unstaged is something like a log, a build output, or a file with secrets in it, unstaging alone is not enough — a future git add . will stage it again. Add its path to a file named .gitignore in the repository root, one path per line, and save it. After that, git add . skips anything matching those lines.

One important limit: .gitignore does not unstage files that are already tracked. If the file was committed in the past, unstage it first, then remove it from tracking with git rm --cached path/to/file and commit that change. The --cached flag is what keeps the copy on your disk; without it, Git deletes the working file too.

Symptoms, Causes and Fixes After Unstaging

SymptomCauseFix
File still shows under "Changes to be committed"You unstaged a different path than the one staged, or ran the command outside the repoRun git status, copy the exact path shown, and rerun git restore --staged with that path from the repository root
"unknown switch" or similar error on git restoreAn older Git version without the restore subcommandUse git reset HEAD path/to/file instead
Edits disappeared from the fileA hard reset was run at some point, not the unstaging commandsCheck git reflog for the previous commit state; if the file was ever committed, it can be recovered from there
File keeps coming back as staged after git add .The file is not in .gitignore, or it is already trackedAdd it to .gitignore; if tracked, also run git rm --cached path/to/file and commit
"fatal: not a git repository"The terminal is open in the wrong foldercd into the folder containing the .git directory, then retry

How to Tell It Worked, and What to Do Next

The clearest sign of success is git status showing the file outside the "Changes to be committed" block. If the file is one Git already tracks, it moves to "Changes not staged for commit" — the edits are safe and still there. If it is a brand-new file, it moves to "Untracked files". Either way, a plain git commit now commits only what remains staged.

If you meant to unstage only some edits inside a file rather than the whole file, use git restore --staged -p path/to/file. The -p (patch) flag walks you through the changes one hunk at a time and asks y or n for each, so you can keep some lines staged and pull others back. Answer n to unstage a hunk, y to leave it staged, and q to stop.

One habit prevents most of this: stage deliberately with named paths (git add index.html styles.css) instead of git add ., and glance at git status before every commit. The ten seconds spent checking is far cheaper than rewriting a commit that swept in the wrong files.

Official source: Git SCM Documentation