How Do I Remove a Single File From the Staging Area (Undo Git Add)?

Better Stack Team
Updated on August 12, 2024

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:

  1. Check the Status: Use git status to see the current state of the working directory and the staging area.

     
    git status
    

    The file example.txt should now appear under "Changes not staged for commit" instead of "Changes to be committed".

  2. Check the Staging Area: To further verify, you can use git diff --cached to 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

  1. Add a File to Staging Area:

     
    git add example.txt
    
  2. Unstage the File:

     
    git reset example.txt
    
  3. Verify:

     
    git status
    

    The file example.txt should 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.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github