Sarah
Sarah

Reputation: 754

Restore MULTIPLE files deleted from one commit to their state in previous commit (in bulk)?

I've been searching this for a little bit and, apologies, I'm sure the answer I need is already somewhere but I am unable to find the exact answer I want for multiple files. The answers I've found only describe reverting individual file paths.

I have hundreds of files across different directories of my repo that were deleted by someone in a specific git commit.

5678a22cbf20f2571fe8d37d8a8cc42ba7f89c62 - commit where files were deleted
309c00b621e696b138c80f7db321bb33980067f6 - commit I want to restore those deleted files to

I can get a list of the files that have been deleted in the last commit with:

git log --diff-filter=D -n 1 --summary

This gives me an output like:

delete mode 100755 path/to/file.js
delete mode 100755 path/to/another/file.css

I can revert individual files with:

git checkout 309c00b621e696b138c80f7db321bb33980067f6 path/to/file

But how do I restore ALL the deleted files from that commit in bulk?

My end goal is to have the reverted files as Uncommitted changes in my working copy.

Thanks in advance!

(sidenote: I am also using SourceTree if that has any tricks)

Upvotes: 1

Views: 817

Answers (1)

eftshift0
eftshift0

Reputation: 30297

You almost solved it yourself:

git log --name-only --diff-filter=D -n 1 --summary | while read file; do
    git checkout 309c00b621e696b138c80f7db321bb33980067f6 "$file"
    git reset "$file" # so that it shows as uncommitted
done

You should consider amending the revision where they were deleted if it was a real mistake so that history doesn't get busted by that deletion..... but, of course, rewriting history is always a thing that has consequences that have to be weighted in to decide if it's worth it.

Upvotes: 1

Related Questions