Nicholas Finch
Nicholas Finch

Reputation: 327

Removing all deleted files from git repo history w search command

Had to start version controlling a project not originally in version control. As a result the repo is now 17gb and needs to be paired down if we are going to get the repo up into github (the site itself is now paired down to about 600mb).

Basically we want to remove anything we have deleted in the process of pairing it down from the repo completely. I've found a great command to find every deleted file.

git log --all --pretty=format: --name-only --diff-filter=D | sort -u

Which lists the files beautifully.

Now how do I pipe this in to the command necessary to clear it from the history. Something along these lines.

git filter-branch --tree-filter 'rm -rf { file }' HEAD

I've made a backup copy of the repo so if I totally screw up things will be okay.

Is this the right command to loop all the files into? And if so how do I do this?

Upvotes: 2

Views: 63

Answers (1)

David Neiss
David Neiss

Reputation: 8237

ok, xargs needs a little help. We need to use one per line, but substituted into that filter-branch command:

git log --all --pretty=format: --name-only --diff-filter=D | sort -u | while read -r line; do git filter-branch -f --tree-filter "rm -rf { $line }" HEAD; done

We also must specify the -f flag to filter-branch to remove the old refs, per Purging file from Git repo failed, unable to create new backup

Upvotes: 2

Related Questions