How do you undo git add .
without losing the changes made on local?
I wanted to git add './file1.java './file2.java'
but instead did git add .
In short, in your particular case...
$ git restore --staged .
...would move all staged changes back to your Working Tree, without the risk of any loss of data; you could then re-add only the files you intended to add in the first place. See below illustration showcasing how restore
can be used to manipulate both the Staging Area and Working Tree.
Warning: performing restore
on changes made to your Working Tree will lead to loss of data.
Since Git 2.23 restore
is the preferred option for unstaging files, prior to this release reset
was the command to use, and it can still be used.
To unstage all files, the following reset
command could also be used:
$ git reset -- .
Just like the terminal shows in the illustration above, Git's CLI is very verbose and helpful in the way it communicates. Notice how git status
displays how changes can be moved in between the Staging Area and Working Tree.
Source: Above excerpt is taken from this full length post on the subject: How to undo changes in Git? (reset vs revert vs restore)
Run git reset
to unstage all pending changes from the local index.
This does not alter your local files at all, it just resets your local git index and is safe to run.
If there are only particular directories you want to unstage then you can pass a pathspec:
$ git reset -- ./some/path
Only files under that path will be unstaged, everything else will remain staged in the index.
Although it's a bit technical, this is discussed in the git documentation: https://git-scm.com/docs/git-reset
This means that
git reset <pathspec>
is the opposite ofgit add <pathspec>
. This command is equivalent togit restore [--source=<tree-ish>] --staged <pathspec>....
For example, after running these two commands it has no net effect - the local index will end up back where it started:
$ git add .
$ git reset .
git status
and it’ll tell you!git restore --staged <file>..."
but what if it's hundreds of files? I don't wanna type every. single. file. Can I dogit restore --staged .
then?git restore --staged
a directory or directories, including.
.