Paweł Adamski
Paweł Adamski

Reputation: 3415

Execute command on every changed file

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?

Upvotes: 6

Views: 3641

Answers (4)

Lajos
Lajos

Reputation: 2827

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

Upvotes: 5

Sergei G
Sergei G

Reputation: 1690

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.

Upvotes: 5

dvxam
dvxam

Reputation: 604

You could also use this command :

git status -s | grep '??' | cut -f2 -d' ' | xargs echo

and replace echo by the command you want to execute

Upvotes: 7

Till
Till

Reputation: 4523

You should use git hooks :

https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks

You will find the files to change in .git/hooks.

Here is a good tutorial

Upvotes: 0

Related Questions