问题描述
我正在用git比较两个分支上的路径以准备合并。我想列出此路径下的所有文件,并用git diff
列出它们的状态,而不仅仅是已更改的文件。
示例:
$ git status
On branch main
nothing to commit,working tree clean
$ ls -1
a.file
b.file
c.file
在第二个分支new-branch
上,我执行了三个更改:
- 删除
b.file
- 更改
c.file
- 创建
d.file
$ git status
On branch new-branch
nothing to commit,working tree clean
$ ls -1
a.file
c.file
d.file
使用git diff
,我可以列出所有已更改文件的名称和状态:
$ git diff --name-status main new-branch
D b.file
M c.file
A d.file
这几乎是我想要的 :我也想在此列表中找到a.file
。像这样:
a.file
D b.file
M c.file
A d.file
git diff
有可能吗?也许使用git show
?
解决方法
请使用以下代码。
git ls-files | while read -r line;
do
st=$(git diff --name-status main new-branch "$line");
if [ -n "$st" ]; then
echo "$st";
else
echo " $line";
fi;
done
git ls-files
:显示所有由Git控制的文件。
git diff --name-status main new-branch "$line"
我们将对特定文件使用git diff。
注意:您只需要将以上代码粘贴到终端中即可。