Find files with particular removed text and stage them in git

34 Views Asked by At

Suppose I've removed the text 'foo' from a lot of files in my repository, and I want to commit that change.

But there are also lots of unrelated changes.

How do I find just those files and stage them?

1

There are 1 best solutions below

1
joachim On

git grep's --cached option appears to search in changes that are UNstaged (which looks like a bug, as it's not what its docs say, and it's not how git diff treats its --cached option).

Doing git grep --cached foo gets:

somefile.txt: foo: true

To stage these, use cut to extract just the filenames: git grep --cached foo | cut -d : -f 1:

somefile.txt

and then xargs to stage that list: git grep --cached foo | cut -d : -f 1 | xargs git add.