我可以在未跟踪的文件上使用git diff吗? [英] Can I use git diff on untracked files?

查看:442
本文介绍了我可以在未跟踪的文件上使用git diff吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以要求git diff在其diff输出中包含未跟踪文件?或者是我最好的选择git添加我创建的新文件和我编辑的现有文件,并使用

  git diff --cached 

解决方案使用最近的git版本,您可以 git add -N 文件(或 - intent-to-add ),它会在该位置的索引中添加一个零长度的blob。结果是你的未跟踪文件现在成为一个修改,将所有内容添加到这个零长度文件中,并显示在git diff输出中。

  git diff 

echo这是一个新文件> 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 @@
+这是一个新文件

不幸的是,正如您指出的那样,您不能 git stash ,而你有一个 - 意图添加这样的待处理文件。虽然如果您需要隐藏,您只需添加新文件然后储存它们。或者你可以使用的仿真解决方法:

 的git更新索引--add --cacheinfo \ 
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 new.txt

(设置别名是你的朋友)。

Is it possible to ask git diff to include untracked files in its diff output? Or is my best bet to git add the new files I've created and the existing files I have edited, and use

git diff --cached

?

解决方案

With recent git versions you can git add -N the file (or --intent-to-add), which adds a zero-length blob to the index at that location. The upshot is that your "untracked" file now becomes a modification to add all the content to this zero-length file, and that shows up in the "git diff" output.

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

Sadly, as pointed out, you can't git stash while you have an --intent-to-add file pending like this. Although if you need to stash, you just add the new files and then stash them. Or you can use the emulation workaround:

git update-index --add --cacheinfo \
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 new.txt

(setting up an alias is your friend here).

这篇关于我可以在未跟踪的文件上使用git diff吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆