Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 was deleted a few days later but the history is there. Our code files are only about 75 MB, and we have 4 branches. Due to the 200 MB file commit, the history is preserved and the size of our project folder (specifically the hidden .git folder) has ballooned to close to 700 MB. How do I permanently delete the two check-ins (commit of the huge file, delete of the huge file) from git as if it never happened? I'm using `TortoiseGit if that matters.

like image 241
tempid Avatar asked Aug 02 '13 06:08

tempid


People also ask

How do I delete a specific commit in history?

By deleting line of the commit or writing drop instead of pick, you can remove the commit from history. Thus, we deleted a commit locally. To remove it from remote repository, we should push our changes to remote. The + sign before the name of the branch you are pushing, this tells git to force the push.

How do you remove a commit from GitHub permanently?

To entirely remove unwanted files from a repository's history you can use either the git filter-repo tool or the BFG Repo-Cleaner open source tool. The git filter-repo tool and the BFG Repo-Cleaner rewrite your repository's history, which changes the SHAs for existing commits that you alter and any dependent commits.

How long git commit history is automatically deleted?

90 days. no (you actually can contact GitHub support to ask them and restore a deleted pushed branch)


1 Answers

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
like image 176
mart1n Avatar answered Oct 11 '22 13:10

mart1n