6

I performed a fresh clone and copied/pasted a working directory into the cloned directory. Now have a list of changed files:

$ git status --short | grep -v "??" | cut -d " " -f 3 
GNUmakefile
Readme.txt
base32.h
base64.h
...

When I try to get Git to add them, it results in an error (I don't care about adding 1 at a time):

$ git status --short | grep -v "??" | cut -d " " -f 3 | git add
Nothing specified, nothing added.
Maybe you wanted to say 'git add .'?

Adding the -:

$ git status --short | grep -v "??" | cut -d " " -f 3 | git add -
fatal: pathspec '-' did not match any files

And --:

$ git status --short | grep -v "??" | cut -d " " -f 3 | git add --
Nothing specified, nothing added.
Maybe you wanted to say 'git add .'?

Trying to use interactive from the man page appears to have made a greater mess of things:

$ git status --short | grep -v "??" | cut -d " " -f 3 | git add -i
           staged     unstaged path
  1:    unchanged        +1/-1 GNUmakefile
  2:    unchanged      +11/-11 Readme.txt
  ...

*** Commands ***
  1: status   2: update   3: revert   4: add untracked
  5: patch    6: diff     7: quit     8: help
Huh (GNUmakefile)?
What now> *** Commands ***
  1: status   2: update   3: revert   4: add untracked
  5: patch    6: diff     7: quit     8: help
Huh (Readme.txt)?

(I've already deleted the directory Git made a mess of, so I'm not trying to solve that issue).

How do I tell Git to add the files piped into it?

10

git add is expecting the files to be listed as arguments, not piped into stdin. Try either

git status --short | grep -v "??" | cut -d " " -f 3 | xargs git add

or

for file in $(git status --short | grep -v "??" | cut -d " " -f 3); do
    git add $file;
done
  • Is there any way to do this while also using git-add's -p flag? – Brian Hannay May 6 '18 at 17:53
2

If the files were already in the index (i.e. they show up as "modified", as opposed to untracked, when you run 'git status'), Then you can run

git commit -am "Useful commit message here"

This adds all tracked but modified files automatically.

1

I used

git add `cat filelist`

I only had 100 files. 1000s might be problematic.

But maybe not. Use "-n" for a dry run.

0

If you're using ag or ack, this is what works:

ag -l PATTERN | xargs git add

0

Similar to David King's:

git status --short | perl -lane '$F[0] == "M" and print $F[1]' | xargs -n 20 git add

That example finds files only in the Modified state, and adds them. To guard against very long parameter lists, xargs uses the -n 20 parameter to limit each call to git to 20 files.

Change the match condition in the perl script as needed. Another example:

| perl -lane '$F[0] == "M" and $F[1] =~ m/py$/ and print $F[1]' |

That example finds modified Python files (ends with py).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy