Remove directory from GIT history

GIT is not for a binary files, filter history

GIT is not for binary files, filter history

Have you messed GIT with any big and unwanted content? Committing binaries, huge media files or new test project? Last refactoring found a lot of duplicate code? It is a quite common problem and your repository grows all the time. It is recommended, e.g. to remove all binary files from repository. You can also remove them from repository history to optimize its size. The most common case is removing directory from GIT history.

Detailed usage of git filter-branch is described at Permanently remove files and folders from Git repo. This tutorial has just the most basic steps.

Identify commit in history

You can edit while repository history, but it can mess up other things accidentally. It is suggested to repair just part of history with unwanted directory.

How to show commit history:

git log

One log record can look like this:

commit 290d27264f11a77fbd4c4965c8fff9e8c82f0f36
Author: root <>
Date: Wed Mar 30 09:42:38 2016 +0200

stm – repair filter + sort

Identify commit and save first 8 characters from its hash-code.

Filter history/remove directory

You can filter history from identified commit to last/HEAD.

git filter-branch --tree-filter 'rm -rf home/big' 1ba9226c..HEAD

For removing directory from whole history use

git filter-branch --tree-filter 'rm -rf home/big' HEAD

Commit and push GIT history changes

Last step is to push all changes and master records.

git push origin master --force

Whole process is finished. You can also force local garbage collector, but GIT will execute it in defined period by itself.

Spread the word...Email this to someoneTweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on Tumblr
Posted in IT, Uncategorized Tagged with: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *