Git: undo delete

Updated . Posted . Visible to the public.

Assuming you're wanting to undo the effects of git rm or rm followed by git add -A or something similar:

This restores the file status in the index:

git reset -- <file>

then check out a copy from the index

git checkout -- <file>

To undo git add , the first line above suffices, assuming you haven't committed yet.


Note:

Make sure to use double dashes -- to tell git to checkout a file instead of a branch. This only is relevant for files having the same name as a branch Show archive.org snapshot . Also be aware that it is not the same as a single dash, which is a shortcut for @{-1}(the last checked out branch) Show archive.org snapshot . This means you would checkout the file not from your current but the last branch you were on.

✗ git checkout - <file> # checks out <file> from previous branch
✔ git checkout -- <file>
Daniel Straßner
Last edit
Arne Hartherz
License
Source code in this card is licensed under the MIT License.
Posted by Daniel Straßner to makandra dev (2017-05-23 11:37)