【问题标题】:How an I make `git ls-files` work in subdirectories?我如何让 `git ls-files` 在子目录中工作?
【发布时间】:2019-10-02 07:58:02
【问题描述】:

我想运行git ls-file --modified 以获取包含未暂存更改的文件列表。我惊讶地发现它可以在根目录中工作,但不能在任何子目录中工作,它只打印子目录中的文件(例如,与 git status 相比)。

重现步骤:

mkdir repo
cd repo
git init
touch file.txt
git add file.txt 
mkdir subdirectory
echo "foo" >> file.txt    # file now has unstaged changes
git ls-files --modified   # This prints 'file.txt'
cd subdirectory/
git ls-files --modified   # This prints nothing

如何在此处更改 git 的行为?

【问题讨论】:

  • ls-files 不适用于父目录,但它适用于子目录。

标签: git git-ls-files


【解决方案1】:

默认情况下,许多 git 命令针对您当前的工作目录运行(git status、ls-files、...)。

你可以使用-C:

git -C ../ ls-files --modified

如果您希望命令相对于 git 'toplevel' 运行,您可以运行:

git -C $(git rev-parse --show-toplevel) ls-files --modified

来自 git 的手册页:

   -C <path>
       Run as if git was started in <path> instead of the current working directory. When multiple -C options are given, each subsequent

编辑 它还取决于您想要什么样的输出,@phd 不是。请参阅下面的命令和输出以了解不同的选项及其输出:

> git -C ../ ls-files --modified
file.txt
> git -C $(git rev-parse --show-toplevel) ls-files --modified # wrt git toplevel no matter how deep you are in the tree
file.txt
> git ls-files --modified ../
../file.txt
> git ls-files --modified $(git rev-parse --show-cdup) # wrt git toplevel no matter how deep you are in the tree
../file.txt

【讨论】:

  • git -C 在这种情况下的缺点是git ls-files 显示与根目录相关的修改文件,而不是当前目录。 IMO 更好的方法是git ls-files --modified $(git rev-parse --show-cdup)
  • @phd:感谢您的提示;我将此添加到我的答案中。
猜你喜欢
  • 2012-05-14
  • 1970-01-01
  • 2019-08-08
  • 1970-01-01
  • 1970-01-01
  • 2016-08-13
  • 2020-01-03
  • 2020-08-06
  • 2012-02-25
相关资源
最近更新 更多