简体   繁体   中英

Commit a file previously deleted from git

I'm using XCode, SourceTree and i accidentally removed the Podfile.lock from the repository, but I have it in my pc. How can I add it again or unmarked it as removed in the repository?

If it's ok for you to go back in time to "remove the removal" of the file from history (as if it had never been deleted in the first place), then this is what you do. Let's assume the file was removed on master~3.

git checkout master~3
git checkout HEAD~1 -- Podfile.lock # get the file back from previous revision
git commit --amend --no-edit # commit (file should not be deleted on this revision)
git cherry-pick master~3..master # replay history of master after the revision we modified
# at this point you should verify that everything is ok
# if everything is ok, then we need to move master pointer
git branch -f master
git checkout master # checkout new "fixed" master

That should be enough. This is rewriting the history of the branch.... it has costs associated with it (other developers are already working on the old branch? They will have to rebase/cherry-pick their work on top of the new branch, for example).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM