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

发布于 2024-10-20 22:12:36 字数 39 浏览 2 评论 0 原文

如何运行“git status”并只获取文件名而不是长相对路径?

How can I run "git status" and just get the filenames as opposed to the long relative path?

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

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

发布评论

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

评论(10

长安忆 2024-10-27 22:12:36

git status --porcelain 的输出旨在易于在脚本中解析,输出完整路径而不是相对路径,无论当前目录位于树中的位置。

git status --porcelain 输出的每一行都有两个前导字符,指示文件的状态(例如,是否未跟踪、修改、新建、删除等),后跟一个空格,所以如果您只是想要 git status 输出中提到的所有内容的完整路径,您可以执行以下操作:

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

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 of git status you can do:

git status --porcelain | sed s/^...//
凉栀 2024-10-27 22:12:36

我认为 cut 对此很有用。

git status -s | cut -c4-

I think cut is good for this.

git status -s | cut -c4-
余生一个溪 2024-10-27 22:12:36

啊哈,我我刚刚理解了这个问题:你想要基名吗?粘上<代码> |同时阅读一个;做基本名称“$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

ら栖息 2024-10-27 22:12:36

使用 git 的更简单的解决方案>ls-文件

来自文档:

选项

-c
--cached 在输出中显示缓存文件(默认)

-d
--deleted 在输出中显示已删除的文件

-m
--modified 在输出中显示修改后的文件

-o
--others 在输出中显示其他(即未跟踪的)文件

-i
--ignored 在输出中仅显示被忽略的文件。显示索引中的文件时,仅打印那些与排除模式匹配的文件。什么时候
显示“其他”文件,仅显示那些与排除模式匹配的文件。
标准忽略规则不会自动激活,因此
至少需要一个 --exclude* 选项。

-s
--stage 在输出中显示暂存内容的模式位、对象名称和阶段编号。

-u
--unmerged 在输出中显示未合并的文件(强制 --stage)

显示标志的示例也可以组合:

git ls-files -dmo

A much simpler solution that is built-in to git using ls-files.

From the docs:

OPTIONS

-c
--cached Show cached files in the output (default)

-d
--deleted Show deleted files in the output

-m
--modified Show modified files in the output

-o
--others Show other (i.e. untracked) files in the output

-i
--ignored Show only ignored files in the output. When showing files in the index, print only those matched by an exclude pattern. When
showing "other" files, show only those matched by an exclude pattern.
Standard ignore rules are not automatically activated, therefore at
least one of the --exclude* options is required.

-s
--stage Show staged contents' mode bits, object name and stage number in the output.

-u
--unmerged Show unmerged files in the output (forces --stage)

Example showing flags can be combined as well:

git ls-files -dmo
动次打次papapa 2024-10-27 22:12:36

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

嘦怹 2024-10-27 22:12:36

获取修改后的文件的名称

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

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

git status --porcelain|awk '{if($1=="M") {print "scp " $2 " account_name@server_ip:~/my_codebase/$(dirname " $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 --porcelain|awk '{if($1=="M") {print "scp " $2 " account_name@server_ip:~/my_codebase/$(dirname " $2 ")/;"} }'|sh
楠木可依 2024-10-27 22:12:36

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")

即,列出的“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.

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.

空气里的味道 2024-10-27 22:12:36

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

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

$2 选择每行的第二列。

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

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

The $2 selects second the column of each line.

往事风中埋 2024-10-27 22:12:36

如果您只查找没有路径 mumbo-jumbo 的文件名,则以下内容应该有效:

git status --porcelain | cut -c 3- | xargs basename -a

此脚本获取 git status 的缩短格式,将每行的第三列剪切到末尾并将其提供给 basename 命令仅输出完整路径中的文件名。

If you're looking for only the filenames without the path mumbo-jumbo the following should work:

git status --porcelain | cut -c 3- | xargs basename -a

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.

蓝海 2024-10-27 22:12:36

如果您使用 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 日)

ls-files:澄清文件的描述选择选项

签字人:伊利亚·纽伦

前面对文件选择选项的描述很容易被误解。
例如:

  • 在输出中显示缓存的文件”这可以解释为“显示已修改和 git 添加的文件,即
    缓存了相对于 HEAD 的更改的文件。
  • 显示已删除的文件”这可以解释为“对于每个git rm $FILE(man) 我们跑了,给我看看$FILE"
  • 显示修改的文件”这可以解释为“显示已修改和 git 添加的文件”或“显示与 HEAD 不同的文件”或“显示我未删除与 HEAD 不同的文件”(假设 --deleted 是一个单独的选项),其中没有一个是正确的。

此外,当某些选项仅修改和/或覆盖其他选项时也不是很清楚,例如 --ignored--directory 和 < code>--unmerged (我在邮件列表上看到过有人对它们感到困惑,有时甚至是其他 Git 开发人员。)

调整这些定义以及 --killed 的定义,尝试让它们更加清晰。
最后,还要尽早澄清路径的重复报告通常是预期的(当(a)索引中的文件有多个条目时 - 即
当存在冲突时,以及 (b) 当用户指定可能多次选择同一文件的选项时,例如 git ls-files --cached --deleted --modified``< super>(男人) 当存在未暂存删除的文件时)。


git ls-files 现在包含在其 手册页

--缓存

显示Git索引中缓存的所有文件,即所有跟踪的文件。
(如果没有 -c/-s/-d/-o/,则这是默认值-u/-k/-m/--resolve-undo
指定选项。)

git ls-files 现在包含在其 手册页

--已删除

显示未暂存删除的文件

git ls-files 现在包含在其 手册页

--修改

显示具有未暂存修改的文件(请注意,未暂存的修改
删除也算作未暂存的修改)

git ls-files 现在包含在其手册页

--忽略

在输出中仅显示被忽略的文件。
必须与
显式“-c”或“-o”。

  • 显示文件时
    索引(即与“-c”一起使用时),仅打印那些文件
    匹配排除模式。
  • 显示“其他”文件时
    (即与“-o”一起使用时),仅显示与
    排除模式。

标准忽略规则不会自动
已激活,因此至少需要一个 --exclude* 选项
是必需的。

git ls-files 现在包含在其 手册页

--未合并

在输出中显示有关未合并文件的信息,但是
不显示任何其他跟踪文件(强制 --stage,覆盖
--缓存)。

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)

ls-files: clarify descriptions of file selection options

Signed-off-by: Elijah Newren

The previous descriptions of the file selection options were very easy to misunderstand.
For example:

  • "Show cached files in the output" This could be interpreted as meaning "show files which have been modified and git-add'ed, i.e.
    files which have cached changes relative to HEAD".
  • "Show deleted files" This could be interpreted as meaning "for each git rm $FILE(man) we ran, show me $FILE"
  • "Show modified files" This could be interpreted as meaning "show files which have been modified and git-add'ed" or as "show me files that differ from HEAD" or as "show me undeleted files different from HEAD" (given that --deleted is a separate option), none of which are correct.

Further, it's not very clear when some options only modify and/or override other options, as was the case with --ignored, --directory, and --unmerged (I've seen folks confused by each of them on the mailing list, sometimes even fellow Git developers.)

Tweak these definitions, and the one for --killed, to try to make them all a bit more clear.
Finally, also clarify early on that duplicate reports for paths are often expected (both when (a) there are multiple entries for the file in the index -- i.e.
when there are conflicts, and also (b) when the user specifies options that might pick the same file multiple times, such as git ls-files --cached --deleted --modified``(man) when there is a file with an unstaged deletion).

git ls-files now includes in its man page:

--cached:

Show all files cached in Git's index, i.e. all tracked files.
(This is the default if no -c/-s/-d/-o/-u/-k/-m/--resolve-undo
options are specified.)

git ls-files now includes in its man page:

--deleted:

Show files with an unstaged deletion

git ls-files now includes in its man page:

--modified:

Show files with an unstaged modification (note that an unstaged
deletion also counts as an unstaged modification)

git ls-files now includes in its man page:

--ignored

Show only ignored files in the output.
Must be used with
either an explicit '-c' or '-o'.

  • When showing files in the
    index (i.e. when used with '-c'), print only those files
    matching an exclude pattern.
  • When showing "other" files
    (i.e. when used with '-o'), show only those matched by an
    exclude pattern.

Standard ignore rules are not automatically
activated, therefore at least one of the --exclude* options
is required.

git ls-files now includes in its man page:

--unmerged:

Show information about unmerged files in the output, but do
not show any other tracked files (forces --stage, overrides
--cached).

git ls-files now includes in its man page:

--killed

Show untracked files on the filesystem that need to be removed
due to file/directory conflicts for tracked files to be able to
be written to the filesystem.

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