sscirrus
sscirrus

Reputation: 56719

git add files that contain X

In git, how can you add all files to the next commit whose diffs contain a specified string?

I'm looking for something like: git add search('changes').

Upvotes: 0

Views: 672

Answers (2)

user1902824
user1902824

Reputation:

You could do something like this:

git diff -S "regex" --name-only | xargs git add

Take note that the command above will add all changes in a matching file. If you want more control which hunks get added, I highly recommend using git add -p (not just for this scenario, but all the time).

If you want to reduce the files you want to look at, you can combine it with the first command mentioned.

git diff -S "regex" --name-only | xargs git add -p

Upvotes: 3

tomlogic
tomlogic

Reputation: 11694

Take a look at grep -Rl (recursive grep, listing files with matches) and xargs (pass list of files as arguments to another program).

Upvotes: 3

Related Questions