Objective
- To reverse the git add operation for specific files, moving them from the staging area back to the working directory
- not modifying the content of the files themselves
Goal
Guideline
When you stage a file using git add
, it moves into the staging area (index), ready to be committed. If you change your mind and want to unstage it (without losing your changes), you have two common options:
git restore --staged <file>
git reset <file>
Here I will explain of the first one git restore
, because it is part of the newer, task-specific Git commands. This command is more intuitive and descriptive, focusing only on the act of “restoring” the index state. It avoids the broader and potentially confusing implications of git reset
, which can also affect commit history when used in other forms.
To unstage a specific file
git restore --staged <file_name>
To unstage multiple specific files
git restore --staged <file_name1> <file_name2> ...
- List all the files you want to unstage, separated by spaces.
To confirm the result after running
git status
💻 custom alias setup for git unstage
Git lets you define shortcuts (aliases) for longer commands via the [alias]
section of .gitconfig
.
Setup Syntax
[alias]
<shortcut> = <actual git command>
Alias Setup
The following setup defines a Git subcommand alias called git unstage
[alias]
unstage = "restore --staged"
Then, git unstage <file>
works the same as git restore --staged <file_name>