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

Adamos
  • 41
  • 3

2 Answers2

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.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
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.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • Thank You very much for the answer but becouse – Adamos Jul 09 '17 at 07:49
  • I want to be sure because my change was pushed to common repository snd othet develooers – Adamos Jul 09 '17 at 07:50
  • Other developers made theirs changes to the project? – Adamos Jul 09 '17 at 07:51
  • So it will Yor solution be correct in my sytuation? – Adamos Jul 09 '17 at 07:57
  • In posts on ammenf option i find warning: Don't push amended commits to a public repo. – Adamos Jul 09 '17 at 08:03
  • Rewriting history of a branch is always "troublesome" if the branch was already published and other people are working on top of it.... _however_ it's rather trivial to move to a different branch using git (rebase or cherry-pick) if somebody tells me that a branch as been "rewritten". It's business as usual, if you ask me. You have to balance cost/benefit. – eftshift0 Jul 09 '17 at 16:38
  • Thans for help. Adam – Adamos Jul 10 '17 at 08:40