Flagbug
Flagbug

Reputation: 2103

How can I git filter-branch with a regular expression to remove files/directories from the history permanently?

Is there a way to pass git filter-branch a regular expression to remove files/directories permanently from the history?

Upvotes: 2

Views: 1847

Answers (1)

Adam Dymitruk
Adam Dymitruk

Reputation: 129564

Assuming you want to do an index filter (which won't need to change your work tree), you can script on all commits and only change the files you need by sed, egrep or grep on the output of git ls-files.

git ls-files --cached | \
sed 's/weirdprefix\(pattern\)/git reset HEAD weirdprefix\1/g' | \ 
xargs -i{} sh -c '{}'

(this is the script you would provide in the filter-branch command)

Here is an example from the documentation ( http://www.kernel.org/pub/software/scm/git/docs/git-filter-branch.html ):

git filter-branch --index-filter \
        'git ls-files -s | sed "s-\t\"*-&newsubdir/-" |
                GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
                        git update-index --index-info &&
         mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"' HEAD

Upvotes: 3

Related Questions