Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute command on every changed file

Tags:

git

bash

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?

like image 407
Paweł Adamski Avatar asked Jan 07 '23 10:01

Paweł Adamski


2 Answers

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

like image 177
dvxam Avatar answered Jan 17 '23 21:01

dvxam


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.

like image 37
Sergei G Avatar answered Jan 17 '23 19:01

Sergei G