如何使 git status 仅显示暂存文件

发布于 2024-10-09 01:06:59 字数 252 浏览 5 评论 0原文

我想获取仅包含暂存文件名的列表。我找不到 git status 命令的 --name-only 的等效标志。什么是好的替代方案?

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

解决方案:完整命令

git diff --name-only --cached | xargs -l php -l

I would like to get a list of only the staged filenames. I can't find the equivalent flag for --name-only for the git status command. What is a good alternative?

The file list will be piped to php -l (PHP lint syntax checker).

Solution: the complete command

git diff --name-only --cached | xargs -l php -l

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

萌︼了一个春 2024-10-16 01:06:59

使用 git diff --name-only (使用 --cached 获取暂存文件)

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

铃予 2024-10-16 01:06:59

接受的答案不会让您知道发生了什么样的变化

是的,如果您不是语法检查器,而是一个普通人,拥有一个充满未暂存文件的存储库,并且您仍然想知道暂存文件会发生什么 - 还有另一个命令:

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

这会导致类似以下内容:

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

显然,该文件已暂存,以及 git commit 之后:
deleted_file 将被删除,
将添加 new_file
renamed_file 将成为 renamed_to

以下是短格式输出的说明: https://git-scm.com/docs /git-status#_short_format

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

蔚蓝源自深海 2024-10-16 01:06:59

中设置了以下别名

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 '^??'"

受到 @coffman21 的回答的启发,我在我的 .zshrc

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 '^??'"

它 可能对其他人有用。因此将其添加到答案堆栈中。

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.

狼亦尘 2024-10-16 01:06:59

查看代码更改的暂存文件

git diff --staged   

或使用 --cached (它是 --staged 的同义词)

git diff --cached

或仅查看没有代码更改的文件名

git diff --staged --name-only  

git-diff 手册

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

薆情海 2024-10-16 01:06:59

要查看哪些文件已暂存,

git ls-files

To view which files are staged ,

git ls-files
晚雾 2024-10-16 01:06:59

仅显示暂存文件

git status --porcelain --untracked-files=all | grep '^[A|M|D|R]'
  • - -porcelain 用于解析友好的输出
  • --untracked-files=all 显示所有“未跟踪”文件。显示暂存以供提交的文件。
  • grep '^[A|M|D|R]' 过滤以下文件的输出
    • ^ 从换行符开头匹配。一行的第一个字符表示暂存区域中的状态,第二个字符表示工作树中的状态。
    • 添加了
    • M 已修改
    • D 已删除
    • R 已重命名

这是基于 此评论

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

逆流 2024-10-16 01:06:59

来自@velocity
git diff --staged 正是我想要的。如果有人希望将其变成 bashrc 中的 git ds 之类的快捷方式,请参阅以下示例:

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

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
}
黑色毁心梦 2024-10-16 01:06:59

您可以使用 git 命令

$ git diff --name-only --staged

该命令将列出仅已暂存的文件的名称

You can use the git command

$ git diff --name-only --staged

This command will list you the name of files that has been staged only

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文