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

前端 未结 5 1975
甜味超标
甜味超标 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:32

    The simple way, if it was a recent commit, is:

    # check how many MB your .git dir is before you start
    du -m -d0 .git
    
    # rebase to remove the commits with large files
    git rebase -i HEAD~2 # or however many commits you need to go back
    
    # force push to remote origin
    git push -f origin HEAD
    

    Now reclone the repo and check if the large file is gone. Do this in a new dir.

    git clone  
    
    # check MB of .git dir (should be smaller by the size of the large file)
    du -m -d0 .git
    

    If successful, then the cleanest way for other developers to get back on track is to reclone to a new dir and manually apply their work in progress. If the .git size did not decrease, check if there are tags or anything referencing the offending commit. You will have to delete any tags referencing the commits from the origin too.

    For more complicated situations, you can try the answer by AD7six, but this is just a simple and clean way to do it.

提交回复
热议问题