Nagev
Nagev

Reputation: 13295

How to git restore ONLY deleted files

After running some bash commands, some files were accidentally deleted. Doing git status shows a mix of deleted and modified files, e.g.

        deleted:    folder/README.md
        modified:   folder/somescript.sh

If there is a long list of deleted files I don't want to manually git restore each individually, and a git checkout . would discard my yet to be stashed or committed changes. How do I restore only the deleted files leaving the modified files untouched?

Sharing my own quick hack in an answer, but better solutions are welcome. Thanks.

Upvotes: 4

Views: 955

Answers (1)

Nagev
Nagev

Reputation: 13295

Combined the suggestions in the comments, to make a git alias:

git config --global alias.restore-deleted '!git restore $(git ls-files -d)'

Now I can just run git restore-deleted to restore deleted files and leave the modified ones untouched.

Original Version

This is what I put in my ~/.bashrc:

alias restoredeleted="git status | grep deleted | sed -e 's/deleted://' | xargs git restore"

Then I can conveniently run restoredeleted.

Upvotes: 4

Related Questions