Frequently asked questions around Git and Version Control.
How to Unstage Files in Git
Git has a concept called "Staging Area" to determine which changes should go into the next commit. And it's important to understand that changes are NOT added to the Staging Area automatically: they have to be manually and explicitly added with the
git add command. Adding files to the Staging Area like this is often called "staging files for the next commit."
In this short article, we'll discuss how to do the opposite: how to unstage a file in Git, i.e. removing it from the Staging Area to prevent it from being included in the next commit.
The Git Cheat Sheet
No need to remember all those commands and parameters: get our popular "Git Cheat Sheet" - for free!
Using git restore to Unstage
git restore command is perfect when you have already added a file to the Staging Area and then changed your mind:
$ git restore --staged myFile.js
This will remove the file from the Staging Area, making sure that it will NOT be part of the next commit.
In case you also want to discard the local changes in this file, you can simply remove the
$ git restore index.html
This will discard any local modifications in this file and reset it to its last committed state. Please be vey careful with this command: discarding uncommitted local changes cannot be undone!
Unstaging files in Tower
In case you are using the Tower Git client, untaging a file is as simple as unchecking its "Status" checkbox in the Working Copy view:
Using git reset to Unstage
restore you can also use
git reset to unstage changes. If you're using a Git version older than 2.23, you will have to use
restore is quite a new feature in Git.
$ git reset myFile.js
git restore --staged, this makes sure the file is NOT included in our next commit. The local changes themselves are not affected by this command.
Unstaging All Files at Once
Sometimes, you might want to unstage all of the files you had already added to the Staging Area. In such a situation, you can use
git reset without further options:
$ git reset
The Staging Area will be emptied (but your local changes themselves are left untouched).
- Check out the chapter Working on Your Project in our free online book
- More frequently asked questions about Git & version control