Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to git restore ONLY deleted files

Tags:

git

bash

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.

like image 350
Nagev Avatar asked Oct 13 '25 07:10

Nagev


1 Answers

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.

like image 149
Nagev Avatar answered Oct 14 '25 21:10

Nagev