1

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 .

3
  • 3
    Do git status and it’ll tell you!
    – Ry-
    Commented Sep 23, 2021 at 1:08
  • Yup, it says git restore --staged <file>..." but what if it's hundreds of files? I don't wanna type every. single. file. Can I do git restore --staged . then?
    – 10110
    Commented Sep 23, 2021 at 1:10
  • 1
    Yes, you can git restore --staged a directory or directories, including ..
    – Ry-
    Commented Sep 23, 2021 at 3:07

2 Answers 2

7

How to undo a 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.

Illustration of git restore

Warning: performing restore on changes made to your Working Tree will lead to loss of data.

Prior to Git 2.23 (released in 2019)

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 -- .

Pay attention to Git's verbosity

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)

2
  • Thanks for the illustration and the explanation… helped me understand it better
    – 10110
    Commented Sep 23, 2021 at 19:24
  • Happy that you found the illustration useful! For more similar illustrations on Git, you can always visit my blog on the subject. Commented Sep 24, 2021 at 5:35
2

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 of git add <pathspec>. This command is equivalent to git 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 .
1
  • Thanks for the thorough explanation and alternatives
    – 10110
    Commented Sep 23, 2021 at 19:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.