dr_rk dr_rk - 11 days ago 6x
Git Question

Git command to retrieve deleted files not committed yet

I am using

versioning control and have noticed that a number of tracked files (100+) have been deleted by mistake.

These deletions have not been committed yet.

Here is what I see when I do
git status
. There are 100+ such tracked files deleted by mistake.

Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

deleted: some_tracked_file1.php
deleted: some_tracked_file2.php
deleted: some_tracked_file3.php

How can I undo these changes and thus retrieve the deleted files?


The answer is right there in the output you quote:

(use "git checkout -- file..." to discard changes in working directory)

This will discard changes including deletions.

If these are the only changes listed, and you want to get rid of all of them, then you can do:

git reset --hard

This will put your working directory back into the state it was at your last commit, discarding any local or staged changes.

If you have other changes that you don't want to revert, you'll have to use the checkout -- command. You can easily script this though, to restore only deleted but not staged files on *nix:

git status --porcelain | grep -E '^ D' | awk '{print $2}' | xargs git checkout --

I don't know what an equivalent for Windows might look like, and you'll have issues if you have file names that contain spaces.