How can I delete a file from a Git repository?

后端 未结 24 1997
無奈伤痛
無奈伤痛 2020-11-22 13:41

I have added a file named \"file1.txt\" to a Git repository. After that, I committed it, added a couple of directories called dir1 and dir2

24条回答
  •  名媛妹妹
    2020-11-22 14:08

    git rm will only remove the file on this branch from now on, but it remains in history and git will remember it.

    The right way to do it is with git filter-branch, as others have mentioned here. It will rewrite every commit in the history of the branch to delete that file.

    But, even after doing that, git can remember it because there can be references to it in reflog, remotes, tags and such.

    If you want to completely obliterate it in one step, I recommend you to use git forget-blob

    https://ownyourbits.com/2017/01/18/completely-remove-a-file-from-a-git-repository-with-git-forget-blob/

    It is easy, just do git forget-blob file1.txt.

    This will remove every reference, do git filter-branch, and finally run the git garbage collector git gc to completely get rid of this file in your repo.

提交回复
热议问题