0

I have a directory named packages immediately under my git repository (root/packages/files here) and another with the same name in some other subfolder(root/directory1/packages/files here). I want to make sure that everything in the former is removed and the latter stays as is. Is this command going to match both? If yes, what can I do to make sure only root/packages/all files is targeted by git rm?

git filter-branch --index-filter 'git rm --cached --ignore-unmatch packages/*' --prune-empty HEAD
Ritik Kumar
  • 61
  • 1
  • 9

1 Answers1

0

Because this is an --index-filter and not a --tree-filter, it's wisest to protect the glob character * from the shell.1 That is:

git filter-branch \
    --index-filter 'git rm --cached --ignore-unmatch "packages/*"' \
    ...

for instance. (In a --tree-filter, * will behave in the usual expected way.) This leaves the * glob expansion up to Git, which will match only names in the directory named packages/ as stored in the index. (That's what you say you want.)

As the git rm documentation notes, this will match directory names as well. It won't remove those directories, due to the lack of -r, but it will still match them. If there are no such directories this will not be an issue.


1The filter-branch command runs each filter in a temporary directory, so most shells under most settings wind up leaving the * un-expanded and do the right thing anyway.

Note that if you are using a --tree-filter, the temporary directory has a copy of the tree being filtered. Otherwise, it is empty.

Nathan Moinvaziri
  • 5,506
  • 4
  • 29
  • 30
torek
  • 448,244
  • 59
  • 642
  • 775
  • Thanks for the response. Accepting as answer for the glob expansion details :) It does match exactly what i need. – Ritik Kumar Mar 30 '17 at 04:25