是否有可能要求git diff在其diff输出中包括未跟踪的文件,或者我最好的选择是使用git添加新创建的文件和我编辑过的现有文件,然后使用:
git diff --cached
?
是否有可能要求git diff在其diff输出中包括未跟踪的文件,或者我最好的选择是使用git添加新创建的文件和我编辑过的现有文件,然后使用:
git diff --cached
?
当前回答
这对我来说很管用:
git add my_file.txt
git diff --cached my_file.txt
git reset my_file.txt
最后一步是可选的,它将使文件保持之前的状态(未跟踪)
如果你正在创建一个补丁也有用:
git diff --cached my_file.txt > my_file-patch.patch
其他回答
不是100%的重点,但如果出于某种原因,你不想把你的文件添加到索引中,这是另一个选择:
如果文件是未跟踪的,显然diff是整个文件,所以你可以用less查看它们:
less $(git ls-files --others --exclude-standard)
在它们之间导航:n和:p为下一个和前一个..
从评论更新:如果你需要一个补丁格式,你也可以结合git diff:
git ls-files --others --exclude-standard -z | xargs -0 -n 1 git --no-pager diff /dev/null | less
在这种情况下,还可以将输出重定向到文件或使用其他diff命令。
我相信您可以通过简单地提供两个文件的路径来区分索引文件和未跟踪文件中的文件。
git diff --no-index tracked_file untracked_file
在最新的git版本中,您可以git add -N the file(或——intent-to-add),这将在该位置的索引中添加一个零长度的blob。结果是,您的“未跟踪”文件现在变成了一个修改,将所有内容添加到这个零长度文件中,并显示在“git diff”输出中。
git diff
echo "this is a new file" > new.txt
git diff
git add -N new.txt
git diff
diff --git a/new.txt b/new.txt
index e69de29..3b2aed8 100644
--- a/new.txt
+++ b/new.txt
@@ -0,0 +1 @@
+this is a new file
不幸的是,正如所指出的,当你有一个——intent-to-add文件挂起时,你不能git stash。虽然如果你需要隐藏,你只需要添加新文件,然后隐藏它们。或者您可以使用仿真解决方案:
git update-index --add --cacheinfo \
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 new.txt
(在这里设置别名是你的朋友)。
对于我的交互式日常获取(我一直在根据HEAD对工作树进行差异,并且希望在diff中包含未跟踪的文件),add -N/——intent-to-add是不可用的,因为它破坏了git stash。
这是我的git diff替换。这不是一个特别干净的解决方案,但因为我真的只是交互地使用它,所以我可以接受一个hack:
d() {
if test "$#" = 0; then
(
git diff --color
git ls-files --others --exclude-standard |
while read -r i; do git diff --color -- /dev/null "$i"; done
) | `git config --get core.pager`
else
git diff "$@"
fi
}
只输入d将包括diff中未跟踪的文件(这是我在我的工作流中关心的),d参数…将表现得像普通的git差异。
注:
We're using the fact here that git diff is really just individual diffs concatenated, so it's not possible to tell the d output from a "real diff" -- except for the fact that all untracked files get sorted last. The only problem with this function is that the output is colorized even when redirected; but I can't be bothered to add logic for that. I couldn't find any way to get untracked files included by just assembling a slick argument list for git diff. If someone figures out how to do this, or if maybe a feature gets added to git at some point in the future, please leave a note here!
使用可以暂存新文件和区分暂存文件的想法,您可以将这两者结合起来查看差异,我发现它使用起来很简单。
添加您想要查看差异的文件。在您的情况下,只添加未跟踪的文件。您可以选择只添加那些想要查看差异的文件。 Git stash && Git添加。git隐藏pop 不同的阶段 Git diff——阶段性的 如果需要,重置暂存文件 git重置
综上所述,
git stash && git add . && git stash pop && git diff --staged && git reset