43

Is there any way to get a list of files that will be committed when I type the following?

git commit -m "my changes"

git status lists too much. I could strip out all the words, but I'd rather not. And I don't want to be told about untracked files.

I've tried

git ls-files -md

but that doesn't show files that have been recently added, but not yet committed.

I'm looking for the same output you'd get from

svn status -q

For example $ svn status -q
A file.py
M dir/database.py
M start.py

Cœur
  • 37,241
  • 25
  • 195
  • 267
gene
  • 999
  • 1
  • 7
  • 7

5 Answers5

54

This is what I was looking for. Thanks to notnoop for the lead I needed. I wanted to post back my solution in case it helps others.

git diff HEAD  --name-only

Since I intended to do

git commit -s -F mesage.txt

with the files found in the first line.

My intent is to create a little system that totally ignores the index i.e. that I never need to do git add. (From what I understand, the index is useful when creating patches, which isn't by no means the norm in my workflow.)

gene
  • 999
  • 1
  • 7
  • 7
  • 6
    don't you mean `git diff --cached --name-only`? the `diff HEAD` will show the files last committed and not pushed. the `--cached` one will show files that will be committed when you run `git commit` – gcb Jul 15 '16 at 17:18
  • 5
    `git diff --staged --name-only` also works. Depending upon how you think about git you may want to use --staged [synonym](https://stackoverflow.com/a/39877896) instead of --cached. – SpeedCoder5 Mar 01 '18 at 15:14
14

This command will tell you what files in your index/cache/staging area differ from the current HEAD (and whether they are additions, modifications or deletions) which is the changes which will be committed if you use git commit without explicit paths or the -a option. It's format is reasonably similar to the svn status output which you show.

git diff --cached --name-status
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
6

You can try:

git diff --name-status

I get the following:

$ git diff --name-status
M       README.markdown

Without the untracked files.

notnoop
  • 58,763
  • 21
  • 123
  • 144
  • 6
    This isn't correct. The question is, "what will be commited when I say `git commit -m message`". This gives the differences between the work tree and the cache which is closer to what *won't* be committed. – CB Bailey Nov 29 '09 at 21:58
6

I know OP original asked to avoid git status, but I felt this would be nice to leave for posterity (i.e other people who don't share OP's reservations).

git status --porcelain | grep -v '^[ |??]' | sed -e 's/[A-Z] *//'

My reasoning is that git status --porcelain seems like it was built for exactly this type of quandary...

source: http://git-scm.com/docs/git-status.html

EDIT: You may chose to not use sed -e 's/[A-Z] *//' if you wish to keep git's modification tags in front of each file name.

jonathan3692bf
  • 1,398
  • 1
  • 12
  • 14
  • 1
    +1. I agree that `git status --porcelain` can be used in a script. I have documented as much in http://stackoverflow.com/a/6978402/6309, saying that "The new option \[`--porcelain`\] makes the command's native output format to emit output that is easier to handle by Porcelain." – VonC Jan 04 '14 at 20:23
  • 1
    This does not seem to list the actual files that would be pushed during a git push (and that have been staged by a git add/commmit) – Paul Oct 29 '15 at 11:49
0

I found this to be useful git diff HEAD --compact-summary

From the documentation : https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---compact-summary

Output a condensed summary of extended header information such as file creations or deletions ("new" or "gone", optionally "+l" if it’s a symlink) and mode changes ("+x" or "-x" for adding or removing executable bit respectively) in diffstat. The information is put between the filename part and the graph part. Implies --stat.

This seems to be very close to the svn example mentioned in the question.

Sarfraaz Ahmed
  • 577
  • 5
  • 12