如何使 git 状态显示只分阶段的文件

我想要一份只有演出过的文件名的列表。我找不到 git status命令的 --name-only等效标志。什么是好的选择?

文件列表将通过管道传输到 php -l(PHP lint 语法检查器)。

解决方案: 完整的命令

git diff --name-only --cached | xargs -l php -l
44203 次浏览

Use git diff --name-only (with --cached to get the staged files)

The accepted answer won't let you know what kind of changes were there.

Yes, If you are not syntax checker but an ordinary person with a repository full of unstaged files, and you still want to know what will happen to staged files - there is another command:

git status --short | grep '^[MARCD]'

which leads to something like:

M  dir/modified_file
A  dir/new_file
R  dir/renamed -> dir/renamed_to
C  dir/copied_file
D  dir/deleted_file

Obviously, this files were staged, and after git commit:
deleted_file will be deleted,
new_file will be added,
renamed_file will become a renamed_to.

Here is an explanation of short-format output: https://git-scm.com/docs/git-status#_short_format

to view staged files with code changes

git diff --staged

or using --cached which is synonym for --staged

git diff --cached

or to view only file names without code changes

git diff --staged --name-only

git-diff manual

Inspired by @coffman21's answer I have setup the following alias in my .zshrc

alias gst="git status"
alias gst-staged="git status --short | grep '^\w.'"
alias gst-unstaged="git status  --short | grep '^\W.'"
alias gst-unstaged-tracked="git status  --short | grep '^\s.'"
alias gst-untracked="git status --short | grep '^??'"

or

alias gst="git status"
alias staged="git status --short | grep '^\w.'"
alias unstaged="git status  --short | grep '^\W.'"
alias unstaged-tracked="git status  --short | grep '^\s.'"
alias untracked="git status --short | grep '^??'"

It might be of use to anyone else. So adding it to the stack of answers.

To view which files are staged ,

git ls-files

Show only staged files

git status --porcelain --untracked-files=all | grep '^[A|M|D|R]'
  • --porcelain for parsing-friendly output
  • --untracked-files=all show all "untracked" files. Shows the files that are staged for commit.
  • grep '^[A|M|D|R]' filter the output for files that are
    • ^ Match from the start of a newline. The first character of a line indicates the status in the staging area, the second in the working tree.
    • A added
    • M modified
    • D deleted
    • R renamed

This was based on this comment

from @velocity
git diff --staged is exactly what I wanted. If anyone is looking to make this into a shortcut like git ds in your bashrc please see this example:

git() {
if [[ $@ == "ds" ]]; then
command git diff --staged
else
command git "$@"
fi
}