4

I'v accidentally removed a file (I wasn't his creator) and commit and push it to the remote. Now I want to make git to undelete this change but when I use git revert #mistaken commit it works BUT blame information points at me as the creator of this file not for his original creator. Can anyone knows how to undelete this file in git and keep its original blame history ? Thanks in advance for any help. Adam

2
  • Have you made any commits since deleting the file? Commented Jul 8, 2017 at 20:23
  • Yes and not only me this change was pushed to common repository and other developers add their commits too
    – Adamos
    Commented Jul 9, 2017 at 7:46

2 Answers 2

2

The only way to do what you want is to rewrite the revision history with something like Find and restore a deleted file in a Git repository. However, this has the nasty side-effect that everyone who has made commits since the file was deleted will now need to rebase their code. As you can imagine, this will cause a lot of work for everyone on the team.

For the sake of everyone's sanity, it is best just to revert the single commit and leave the "blame" as your own. A little bit of history archaeology will show anyone interested what happened.

0

Sure... you revert but the file is "new" (it didn't exist on the previous revision... so who's to blame for the new lines? Of course it's you). If you want to keep the file as it was (with blame and everything) why don't you just amend the revision where you deleted it? Say you have something like this:

REV1--REV2--REV3--REV4--REV5--REV6--REV7--REV8

Say it was on REV3 where you deleted it and on REV6 you reverted REV3. Try this instead:

git checkout REV3
git checkout HEAD~1 -- <path-to-file> # get file back from its ashes
git commit --amend --no-edit # created a new REV3 revision that has the file
git cherry-pick REV3..REV5
git cherry-pick REV6..REV8

when you amend REV3 you will get a different ID for the revision from the original REV3. When you cherry-pick on the following command make sure to use the original REV3 ID, not the new one. REV6 won't be applied (because you don't need it anymore). When you are done now you have an alternate branch where the file was never gone and now blame should show you the original revisions for the lines. Feel free to push the new branch if you like it better.

7
  • Thank You very much for the answer but becouse
    – Adamos
    Commented Jul 9, 2017 at 7:49
  • I want to be sure because my change was pushed to common repository snd othet develooers
    – Adamos
    Commented Jul 9, 2017 at 7:50
  • Other developers made theirs changes to the project?
    – Adamos
    Commented Jul 9, 2017 at 7:51
  • So it will Yor solution be correct in my sytuation?
    – Adamos
    Commented Jul 9, 2017 at 7:57
  • In posts on ammenf option i find warning: Don't push amended commits to a public repo.
    – Adamos
    Commented Jul 9, 2017 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.