3

I am writing a script that finds all files that are added to the stage. I have only come up with solutions that work when there is already a initial commit (ie using git diff-index --name-status HEAD). But no solution that works when there is no HEAD.

ie:

% git init
Initialized empty Git repository in /Users/jocke/dev/agical/test/.git/

% cat >> test
content
^C
% git add --all    
% git st
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   test
#

% git diff-index --name-status HEAD
fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions

git status seems to be able to figure out what needs to be added. There is probably some plumbing that I could use but I can't seem to find it. Any ideas?

johlrogge
  • 31
  • 1
  • Not sure it answers your question, but have a look at the option `-N` of `git add` – fge Jan 08 '12 at 23:35
  • Does not look like what I am after. I want to be able to find "A added_file.txt" in any repository including repositories without an initial commit. I have found how to do the diff now when I know there is no initial commit: git diff-index --name-status `git hash-object -t tree /dev/null` But don't know yet how to determine if there is an inititial commit or not... – johlrogge Jan 09 '12 at 00:02
  • Well, `git rev-parse HEAD` will error out if there isn't one, for instance. – fge Jan 09 '12 at 00:03
  • Thanks, that may be the best way to do it – johlrogge Jan 09 '12 at 00:04

1 Answers1

1

All you want is:

git diff --cached --name-status

And works even if there is no initial commit.

Or you want to do:

git status --porcelain
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • results: $ git diff --cached --name-status fatal: No HEAD commit to compare with (yet) $ git status --porcelain A hepp` --porcelain does not work with git 1.6.5 but that may be something I can live with. When was --porcelain introduced? – johlrogge Jan 09 '12 at 08:54
  • @johlrogge - The first command works for me. Maybe even that has changed. 1.6.5 is pretty old. Update to 1.7+ – manojlds Jan 09 '12 at 08:56
  • The first command does not work in: git version 1.7.4.msysgit.0 (tried it at work, 1.6.5 is what I happened to have on my laptop at home. Time to update that one :)) – johlrogge Jan 09 '12 at 08:58
  • @johlrogge - Works for me on 1.7.6. Too sleepy to see when it was changed. But either should work on latest git. – manojlds Jan 09 '12 at 09:00