Git revert deleted file and preserve file history -
suppose have file a.txt
. 1 day, deleted it, committed, , pushed.
the next day, wanted revert last commit, bringing a.txt
. tried using git revert
, when did git blame
, lines showing revert commit hash. original blame history lost.
can recover file , preserve file history, i.e., if file has not been deleted before? note must not change history commit has been pushed.
thanks!
you can this! here's how:
- start new branch commit preceding delete want undo.
- merge offending change
git merge <sha> -s ours
. - if commit had changes besides deletion want keep:
- reapply changes working copy
git diff <sha>^..<sha> | git apply
. - discard deletions (many techniques available;
git checkout -p
may work you).
- reapply changes working copy
- merge branch main branch (e.g. master).
this produces history 2 branches; 1 in file deleted, , 1 in it never deleted. result, git able track file history without resorting heroics such -c -c -c
. (in fact, -c -c -c
, file isn't "restored", because git sees new file created as copy of existing file. technique, reintroducing the same file repository.)
Comments
Post a Comment