bilabila
bilabila

Reputation: 1163

Git: How to checkout all deleted files through whole history

I want to restore all deleted files through whole history, which means to checkout files to the commit before the commit that delete it?

Upvotes: 1

Views: 233

Answers (1)

Romain Valeri
Romain Valeri

Reputation: 22067

Within a bash context*, you can get a sorted list of all deleted files from your repo with

git log --all --diff-filter=D --name-only --pretty=format:'' | sort | uniq

where

--all means log all commits, regardless of the current position of HEAD

--diff-filter=D filters displayed commits along this condition : "D", meaning "deleted"

--name-only outputs the names of the files

--pretty=format:'' shuts down any other output than file names

and the git command then formed returns a list which is sent to

| sort to sort the list alphabetically, and then

| uniq to keep only one instance for each doubled line (or else a file deleted then re-created then re-deleted would appear twice in the list)


Of course, you might also want to restrict the range to take into account for the history traversal. If you need only commits which were deleted from, let's say, branch super-feature, add that as the <revision range> for log (see doc) which, when omitted, falls back to HEAD :

git log --all --diff-filter=D --name-only --pretty=format:'' master..super-feature | sort | uniq

* (either natively on linux-based systems, or for Windows users in GitBash for example)

Upvotes: 3

Related Questions