如何运行“git status”并获取文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5237605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:17:49  来源:igfitidea点击:

How can I run "git status" and just get the filenames

gitversion-control

提问by myusuf3

as opposed to the long relative path?

而不是相对较长的路径?

回答by Mark Longair

The output of git status --porcelain, designed to be easy to parse in a script, outputs the full paths rather than relative paths regardless of where your current directory is within the tree.

的输出git status --porcelain设计为易于在脚本中解析,输出完整路径而不是相对路径,而不管当前目录在树中的哪个位置。

Each line output by git status --porcelainhas two leading characters indicating the status of the file (e.g. whether it's untracked, modified, new, deleted, etc.) followed by a space, so if you just want the full paths of everything that would be mentioned in the output of git statusyou can do:

输出的每一行git status --porcelain都有两个前导字符,指示文件的状态(例如它是否未跟踪、修改、新建、删除等),后跟一个空格,因此如果您只想要在git status你可以做的输出:

git status --porcelain | sed s/^...//

回答by earlonrails

I think cut is good for this.

我认为 cut 对此有好处。

git status -s | cut -c4-

回答by sehe

Aha, I thinkI just understood the question: you want basenames? Tack on | while read a; do basename "$a"; doneto any of the following:

啊哈,我我刚刚理解了这个问题:你想要基名吗?坚持| while read a; do basename "$a"; done以下任何一项:

how about

怎么样

  • git diff --name-only

    for changes relative to index

  • git diff --name-only --staged

    for ... well staged chages :)

  • git diff --name-only HEAD

    got both

  • git diff --name-only

    相对于索引的变化

  • git diff --name-only --staged

    对于......精心策划的chages :)

  • git diff --name-only HEAD

    得到了两个

回答by Johnsyweb

To get just the filenames, as you requested:

要获得只是文件名,为你的要求:

git status --porcelain | sed -e 's!.*/!!'

I cannot see how this would be useful.

我看不出这会有什么用。

回答by Dan McClain

git statuswill always walk down (editand up) the tree and display relative paths. If you only want the file in the directory you are in, See this related answer

git status将始终向下走(编辑和向上)树并显示相对路径。如果您只想要您所在目录中的文件,请参阅此相关答案

回答by You-Liang Shih

Get the name of modified files

获取修改后的文件名

git status --porcelain|awk '{if($1=="M") {print "basename " $2}}'|sh

git status --porcelain|awk '{if($1=="M") {print "basename " $2}}'|sh

I use similar script to copy my modified files to a remote server, as below:

我使用类似的脚本将修改后的文件复制到远程服务器,如下所示:

git status --porcelain|awk '{if(=="M") {print "scp "  " account_name@server_ip:~/my_codebase/$(dirname "  ")/;"} }'|sh

回答by Stassa Patsantzis

git status outputs relative paths so if you cd to the same directory as the file (under your working directory's root) before running git status it will only output the basenames of added/staged files.

git status 输出相对路径,因此如果您在运行 git status 之前 cd 到与文件相同的目录(在您的工作目录的根目录下),它将只输出添加/暂存文件的基本名称。

cd /long/path/to/my/repo/root/dir
"stuff" > ./newfile.txt
> git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   /long/path/to/my/repo/root/dir/plus/some/more/levels/of/directory/structure/inside/it/changed_file.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#       long/path/to/my/repo/root/dir/plus/some/even/more/levels/of/directory/structure/inside/it/but_another_new_file.txt
#       newfile.txt
no changes added to commit (use "git add" and/or "git commit -a")

ie, 'newfile.txt' is listed without the full path because you're in the same path as it.

即,'newfile.txt' 未列出完整路径,因为您与它位于同一路径中。

回答by Gabriel

In addition to the accepted answer, another way of doing it is with awk

除了接受的答案之外,另一种方法是使用 awk

git status --porcelain | awk '{ print  }'

The $2selects second the column of each line.

$2选择第二每一行的列。