4

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.

oberstet
  • 21,353
  • 10
  • 64
  • 97
  • Just out of curiosity: Why do you need this? It seems a rather unusual idea. – sleske Oct 15 '13 at 08:57
  • Why don't you just flatten the history with rebase? Rather than mutilating the history, just put it out of its misery. – Dan D. Oct 15 '13 at 09:08
  • @dan-d: Well, the whole point is to keep history of files now in HEAD. Otherwise I could simply create a new repo from scratch and copy over the files. Or do I understand wrong? – oberstet Oct 15 '13 at 11:57
  • @sleske: Multiple parts of the repo were broken out into own repos. But the parts broken out have been heavily renamed (dirs and files), so there is no way of just using `--subdirectory-filter`. – oberstet Oct 15 '13 at 11:59
  • You can find lots of good answers here: https://stackoverflow.com/questions/17901588/new-repo-with-copied-history-of-only-currently-tracked-files – exstral Sep 13 '17 at 17:44

1 Answers1

1

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)?

FelipeC
  • 9,123
  • 4
  • 44
  • 38
  • Thanks for the `--all` hint. Would your suggestion keep the history on the files not removed? – oberstet Oct 15 '13 at 11:56
  • Note: you need to use `-- --all`. The other doesn't work. So it's `git filter-branch -f --prune-empty --index-filter "git rm -rf --cached --ignore-unmatch '*.js'" -- --all` e.g. – oberstet Oct 15 '13 at 12:24