Execute command on every changed file

Viewed 2259

I would like to execute some shell command on every file that has not staged changes.

For example, if git status shows

On branch xxxxxxx
Your branch is up-to-date with 'origin/xxxxxxxxx'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   client/.../reports.coffee
    modified:   client.../tools.coffee

I want to execute command on files reports.coffee and tools.coffee.

I don't want to use find because files can change in different time. How can I achieve that?

4 Answers

This is a better starting pattern:

git status -s | grep '.M ' | cut -c 4- | xargs echo

Change .M to states you are looking to capture.

cut -c 4- simply cuts out first 3 characters and returns only 4th and to the end.

I came up with this logic less solution: git diff --name-only | xargs -I{} echo {}

Related