我想运行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
Run Code Online (Sandbox Code Playgroud)
我怎样才能改变 git 的行为呢?
默认情况下,许多 git 命令针对当前工作目录运行(git status、ls-files...)。
您可以使用-C:
git -C ../ ls-files --modified
Run Code Online (Sandbox Code Playgroud)
如果您希望命令针对 git 'toplevel' 运行,您可以运行:
git -C $(git rev-parse --show-toplevel) ls-files --modified
Run Code Online (Sandbox Code Playgroud)
来自 git 的手册页:
Run Code Online (Sandbox Code Playgroud)-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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1440 次 |
| 最近记录: |