How to permanently delete a commit from Git's history?

前端 未结 5 1974
甜味超标
甜味超标 2021-02-04 16:02

How can I permanently delete a commit from Git\'s history?

One of the developers on the team has accidentally committed a 200 MB file and pushed it to our Git server. It

5条回答
  •  别那么骄傲
    2021-02-04 16:28

    As forvaidya suggested, git filter-branch is the way to go. Specifically, in your case, you can execute the following command to remove that one file from the repo's history:

    git filter-branch --tree-filter 'rm -f filename' HEAD
    

    Substitute filename with the actual file name. Again, as forvaidya said, this rewrites the entire history of the repo so anyone who pulls after you make this change will get an error.

    Edit: for performance reasons, it's actually better to use Git's rm command:

    git filter-branch --index-filter 'git rm --cached --ignore-unmatch filename' HEAD
    

提交回复
热议问题