I accidentally added a bunch of cache files into the repo. I've now removed them and now want to commit the change to the repo.

So I tried to do the following, but git doesn't seem to like it:

git diff --name-only | grep '/cache/' | xargs git add


warning: You ran 'git add' with neither '-A (--all)' or '--ignore-removal',
whose behaviour will change in Git 2.0 with respect to paths you removed.
Paths like 'sym_online_app_bs3/cache/demo_cust_can_create_tickets' that are
removed from your working tree are ignored with this version of Git.

* 'git add --ignore-removal <pathspec>', which is the current default,
  ignores paths you removed from your working tree.

* 'git add --all <pathspec>' will let you also record the removals.

Run 'git status' to check the paths you removed from your working tree.

I don't want to git add --all because there are some other changes I don't want committed yet.

Has anybody had to do something similar and have a solution to this.


I'm on: Darwin Kernel Version 13.4.0 (Mac OSX 10.9.5)


Git interactive is an acceptable alternative for now. But would be interested to find out how to go down the xargs route.

git -i
git add --all


git add --all <pathspec>

are different.


git diff --name-only | grep '/cache/' | xargs git add --all

is what you want. Additionally, git add can take multiple arguments on the command line, so what you want can be done with

git add --all $(git diff --name-only | grep '/cache/' )
  • Thanks. Both seem to work quite well. I'll stick with the first because it seems more natural for me. The second form is interesting because I've never seen it before (ie. using the dollar sign $) – denormalizer Oct 6 '14 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.