How To Unstage All Files In Git?
Sometimes, a developer may stage a lot of files, by error, in Git. Unstaging each file one by one would be a lengthy task. Luckily, the developers that work on Git have thought of this situation. Git offers an easy command to unstage all files.
To unstage all files in Git, use the git reset command like so:
bashgit reset
This article will go through different scenarios when unstaging files in Git.
Let's get to it 😎.
How to unstage a single file?
To unstage a single file in Git, you can either:
1. Use the git reset command like so:
bashgit reset index.js
2. Use the git restore command like so:
bashgit restore --staged index.js
Both commands will remove the file from the staging area.
Those commands do not affect the local changes. The changed file will stay in your working area (use the git status command to verify).
Note: Be careful not to use the git restore command without the staged option. Using this command without the staged option removes the file from the working area.
Note: Those commands can also unstage directories (if you type the directory's path).
The git reset command resets the HEAD to a specified state.
The git restore command restores working tree files.
How to unstage all files?
To unstage all files, use the git reset command like so:
bashgit reset
This will unstage all files and leave all of those files in the working area.
Alternatively, you can use the git restore command like so:
bashgit restore --staged .
This command has the same behavior as the git reset command.
How to remove unstaged changes?
To remove unstaged changes, use the git checkout command.
If you want to remove all unstaged changes, use:
bashgit checkout .
Or remove one unstaged changed file like so:
bashgit checkout index.js
Final thoughts
As you can see, Git offers two easy-to-use commands to unstage files, git reset and git restore.
The git restore is less known because it is newer but works as well as the other command.
Here are other Git tutorials for your enjoyment: