How to undelete a file previously deleted in git's history?
Asked Answered
G

2

53

Using Chris's answer on another question I could prepend a snapshot-history to my git repository. Since one of the files is not part of my history but only in the snapshots, the first original commit now also contains the deletion of this file. How can I undo that?

At first I thought this was the opposite of How do I remove sensitive files from git’s history, but actually I don't want to insert a file into history but just remove the deletion from history.

Grunt answered 30/6, 2010 at 14:49 Comment(1)
Git does not track intention, only content. If the file exists in an ancestor and is removed in a child commit, then “remove the deletion from history” and “re-insert the into history” amount to the same thing: make sure the file shows up in the child and its children.Huckaby
G
70

I got it:

git tag originalHead # just in case
git rebase -i <id of the parent of the commit that deleted the file>
# change pick to edit for that commit
git checkout <id of the previous commit> <filename> # thanks for reminding, kubi
git commit --amend
git rebase --continue
git tag -d originalHead

edit unfortunately this will leave all tags at the old timeline, see here

Grunt answered 30/6, 2010 at 15:3 Comment(3)
Seriously! git rebase -i <id of the parent of the commit that deleted the file> This saved my lifeCsc
What does it mean/what are the consequences of leaving all tags at the old timeline?Futurity
@Futurity Your log won't show them at the "proper" commits (also for git describe) and the originally tagged ones won't ever be removed by git clean. Using git diff (or any command actually) with tags will not compare the proper commits as well. Basically everything you do involving history will use the "wrong" commits in a wayGrunt
C
73
git checkout <commit> <filename>
Crocker answered 30/6, 2010 at 14:53 Comment(1)
thanks, but that only restores it. I want history to look as if the file was never removedGrunt
G
70

I got it:

git tag originalHead # just in case
git rebase -i <id of the parent of the commit that deleted the file>
# change pick to edit for that commit
git checkout <id of the previous commit> <filename> # thanks for reminding, kubi
git commit --amend
git rebase --continue
git tag -d originalHead

edit unfortunately this will leave all tags at the old timeline, see here

Grunt answered 30/6, 2010 at 15:3 Comment(3)
Seriously! git rebase -i <id of the parent of the commit that deleted the file> This saved my lifeCsc
What does it mean/what are the consequences of leaving all tags at the old timeline?Futurity
@Futurity Your log won't show them at the "proper" commits (also for git describe) and the originally tagged ones won't ever be removed by git clean. Using git diff (or any command actually) with tags will not compare the proper commits as well. Basically everything you do involving history will use the "wrong" commits in a wayGrunt

© 2022 - 2024 — McMap. All rights reserved.