如何运行“git status”并获取文件名
如何运行“git status”并只获取文件名而不是长相对路径?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何运行“git status”并只获取文件名而不是长相对路径?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(10)
git status --porcelain 的输出旨在易于在脚本中解析,输出完整路径而不是相对路径,无论当前目录位于树中的位置。
git status --porcelain
输出的每一行都有两个前导字符,指示文件的状态(例如,是否未跟踪、修改、新建、删除等),后跟一个空格,所以如果您只是想要 git status 输出中提到的所有内容的完整路径,您可以执行以下操作: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.Each line output by
git status --porcelain
has 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 ofgit status
you can do:我认为
cut
对此很有用。I think
cut
is good for this.啊哈,我想我刚刚理解了这个问题:你想要基名吗?粘上<代码> |同时阅读一个;做基本名称“$a”;完成以下任何一项:
怎么样
git diff --name-only
用于相对于索引的更改
git diff --name-only --staged
对于...精心策划的变更:)
git diff --仅名称 HEAD
两者都得到
Aha, I think I just understood the question: you want basenames? Tack on
| while read a; do basename "$a"; done
to any of the following: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
的更简单的解决方案>ls-文件。来自文档:
显示标志的示例也可以组合:
A much simpler solution that is built-in to
git
usingls-files
.From the docs:
Example showing flags can be combined as well:
git status
将始终沿着树向下走(编辑并向上)并显示相对路径。如果您只需要所在目录中的文件,查看此相关答案git status
will always walk down (edit and 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 --porcelain|awk '{if($1=="M") {print "basename " $2}}'|sh
我使用类似的脚本来复制修改后的文件文件到远程服务器,如下:
Get the name of modified files
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 输出相对路径,因此如果您在运行 git status 之前 cd 到与文件相同的目录(在工作目录的根目录下),它将仅输出添加/暂存文件的基本名称。
即,列出的“newfile.txt”没有完整路径,因为您与它位于同一路径中。
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.
ie, 'newfile.txt' is listed without the full path because you're in the same path as it.
除了接受的答案之外,另一种方法是使用
awk
$2
选择每行的第二列。In addition to the accepted answer, another way of doing it is with
awk
The
$2
selects second the column of each line.如果您只查找没有路径 mumbo-jumbo 的文件名,则以下内容应该有效:
此脚本获取 git status 的缩短格式,将每行的第三列剪切到末尾并将其提供给
basename
命令仅输出完整路径中的文件名。If you're looking for only the filenames without the path mumbo-jumbo the following should work:
This script gets the shortened format of git status, cuts each line's 3rd column to the end and feeds it to the
basename
command which outputs only the filename from the full path.如果您使用
git ls-files
,正如 此处建议,作者为 Peter Benjamin,知道在 Git 2.40(2023 年第一季度)中,它的选项已经得到澄清。请参阅 提交 e750951、提交 4173b80, 提交2b02d2d,提交 2a34b31(2023 年 1 月 13 日),作者:伊利亚·纽伦 (
newren
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 3eda830,2023 年 2 月 3 日)git ls-files
现在包含在其 手册页:git ls-files
现在包含在其 手册页:git ls-files
现在包含在其 手册页:git ls-files
现在包含在其手册页:git ls-files
现在包含在其 手册页:git ls-files
现在包含在其 手册页:If you are using
git ls-files
, as suggested here by Peter Benjamin, know that, with Git 2.40 (Q1 2023), its options have been clarified.See commit e750951, commit 4173b80, commit 2b02d2d, commit 2a34b31 (13 Jan 2023) by Elijah Newren (
newren
).(Merged by Junio C Hamano --
gitster
-- in commit 3eda830, 03 Feb 2023)git ls-files
now includes in its man page:git ls-files
now includes in its man page:git ls-files
now includes in its man page:git ls-files
now includes in its man page:git ls-files
now includes in its man page:git ls-files
now includes in its man page: