0

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

Error:

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.

Thanks

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

Update

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

git -i
denormalizer
  • 491
  • 2
  • 5
  • 15

2 Answers2

2
git add --all

and

git add --all <pathspec>

are different.

So,

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/' )
fragmede
  • 36
  • 1
  • 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 06 '14 at 22:16
1

Instead of using grep use the -G option of the git diff command:

git add --all $(git diff -G 'cache' --name-only)
soyuka
  • 111
  • 3