oberstet
oberstet

Reputation: 22011

How to remove Git history of anything but current files

How can I remove the complete history of anything on any branch but the files currently existing on HEAD (my master branch)?

I have been doing things like

 git filter-branch -f --prune-empty --index-filter \
    "git rm -rf --cached --ignore-unmatch '*.js' '*.html' '*.css'" \
        HEAD

but this is getting tedious, since I have to make explicit what to remove, not what to keep.

Git in principle should be able to determine all ancestors of current files, go back in history, and remove everything thats not in the ancestors. How?

I gave up: this whole rewriting of history seems to be very brittle and messes up things. It sucks. I just copy over files and bite the bullet of loosing history.

Upvotes: 4

Views: 399

Answers (1)

FelipeC
FelipeC

Reputation: 9488

Why not remove all the files first, and then add the ones you want? Something along these lines:

git ls-files HEAD > /tmp/files
git filter-branch ... --index-filter \
  "git rm *; xargs -a /tmp/files git add" --all

Also, why are you using HEAD in your filter-branch command, if you want to filter all the branches (--all)?

Upvotes: 1

Related Questions