如何在 git diff --no-index 中排除文件和目录

8

如何在 git diff --no-index 中排除文件和目录?

例如,我想比较两个目录 project1project2 的差异,而忽略它们顶级的 .git 目录(project1/.gitproject2/.git)以及它们所有的 .DS_Store 文件。


1
相关链接:https://dev59.com/_Gkv5IYBdhLWcg3wsC3i - ma11hew28
@jjmerelo 我尝试了 diff,但它没有 --color-words - ma11hew28
还有相关的:https://dev59.com/-lQJ5IYBdhLWcg3wnXN-,https://dev59.com/JFsW5IYBdhLWcg3wm4UK - Michael
1个回答

1
这并不是您要求的完全相同,但它可能会给您想要的结果:将project2添加为project1的远程,然后只需比较它们的HEAD,使用git diff命令。
git remote add other ../project2/.git
git fetch other

# Perform the diff
git diff other/HEAD

如果您的project2尚不是Git存储库,您可以暂时将其变成一个(感谢Josiah Yoder提供的评论)。
# Set up project2 repo
cd ../project2
git init
git add .

# Add as remote from project1
cd ../project1
git remote add other ../project2/.git
git fetch other

# Perform the diff
git diff other/master

# Clean up
git remote remove other
cd ../project2
rm -r .git

1
你不能在非仓库目录中执行临时的 git init; git add .,然后在完成差异比较后,在非仓库目录中执行 rm -r .git 吗? - Josiah Yoder
顺便说一下,添加是通过 git remote add other path/to/project2/.git 完成的,然后执行 git fetch othergit diff other/master。在非仓库的情况下,我会从非仓库执行这些命令。 - Josiah Yoder

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接