How Do I Remove a Single File From the Staging Area (Undo Git Add)?
If you've added a file to the staging area with git add but want to remove it from the staging area without affecting your working directory (i.e., undo the git add), you can use the git reset command. Here’s how to do it:
Removing a Single File from the Staging Area
Using git reset for a Specific File
To unstage a single file:
git reset <file>
For example, if you added example.txt to the staging area and want to unstage it:
git reset example.txt
This command will remove example.txt from the staging area, but it will not delete or modify the file in your working directory. The changes you made to example.txt will still be present, but it will no longer be included in the next commit.
Verifying the Change
To confirm that the file has been removed from the staging area:
Check the Status: Use
git statusto see the current state of the working directory and the staging area.git statusThe file
example.txtshould now appear under "Changes not staged for commit" instead of "Changes to be committed".Check the Staging Area: To further verify, you can use
git diff --cachedto show the differences between the staged changes and the last commit. The file should no longer appear in this output.git diff --cached
Example Scenario
Add a File to Staging Area:
git add example.txtUnstage the File:
git reset example.txtVerify:
git statusThe file
example.txtshould now be listed under "Changes not staged for commit".
Summary
To remove a single file from the staging area without affecting the working directory:
- Use
git reset <file>to unstage the file.
This approach allows you to selectively manage which changes are staged for the next commit and helps keep your commit history clean and precise.