Undo git filter-branch

Git

Git Problem Overview


I accidentally delete a file from my repo using git filter-branch:

git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch images/thumb/a.JPG' HEAD

How can I undo this? Is it possible? i.e. is the file permanently deleted?

Git Solutions


Solution 1 - Git

When you use git filter-branch, a backup file is created in

refs/original/refs/heads/master

If you used the command in branch master. You can check if you have the backup in .git/refs directory. With this in mind, you can use this backup to recover your files with:

git reset --hard refs/original/refs/heads/master

Solution 2 - Git

Probably a more proper way than just doing hard reset to the original master would be to restore all refs rewritten by git filter-branch, and maybe even delete backup refs afterwards in order to be able to invoke git filter-branch again without --force:

for orig_ref in $(git for-each-ref --format="%(refname)" refs/original/); do
    git update-ref "${orig_ref#refs/original/}" $orig_ref
    git update-ref -d $orig_ref  # to also remove backup refs
done

And after that:

git reset --hard master

UPD.

Here's (arguably) a bit more git'ish way to perform the same without a shell for-loop:

git for-each-ref --format="update %(refname:lstrip=2) %(objectname)" refs/original/ | git update-ref --stdin
git for-each-ref --format="delete %(refname) %(objectname)" refs/original/ | git update-ref --stdin

Solution 3 - Git

A much cleaner solution is given in this answer by @jthill.

git fetch . +refs/original/*:*

As noted in that answer you may need to detach the HEAD if the currently checked out branch is to be restored.

To delete the refs/original refs, issue:

git for-each-ref refs/original --format='delete %(refname) %(objectname)' | git update-ref --stdin

Solution 4 - Git

It's possible that your old branch tip will be preserved in your http://www.kernel.org/pub/software/scm/git/docs/git-reflog.html">reflog</a>;. Then you should be able to check out the unchanged commit with all the previous history.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChinView Question on Stackoverflow
Solution 1 - GitWilliam Seiti MizutaView Answer on Stackoverflow
Solution 2 - GitEldar AbusalimovView Answer on Stackoverflow
Solution 3 - GitMr_and_Mrs_DView Answer on Stackoverflow
Solution 4 - GitcheView Answer on Stackoverflow