我可以在未跟踪的文件上使用 git diff 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/855767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:27:13  来源:igfitidea点击:

Can I use git diff on untracked files?

gitdiff

提问by Andrew Grimm

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 在其 diff 输出中包含未跟踪的文件?或者我最好的选择是 git add 我创建的新文件和我编辑过的现有文件,然后使用

git diff --cached

?

?

采纳答案by araqnid

With recent git versions you can git add -Nthe 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 版本,您可以git add -N将文件(或--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

Sadly, as pointed out, you can't git stashwhile you have an --intent-to-addfile 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 stash当你有一个--intent-to-add像这样待处理的文件时,你不能。虽然如果您需要存储,您只需添加新文件然后存储它们。或者您可以使用仿真解决方法:

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

(setting up an alias is your friend here).

(在这里设置别名是您的朋友)。

回答by Harold

I believe you can diff against files in your index and untracked files by simply supplying the path to both files.

我相信您可以通过简单地提供两个文件的路径来区分索引中的文件和未跟踪的文件。

git diff --no-index tracked_file untracked_file

回答by Jo Liss

For my interactive day-to-day gitting (where I diff the working tree against the HEAD all the time, and would like to have untracked files included in the diff), add -N/--intent-to-addis unusable, because it breaksgit stash.

对于我的交互式日常 gitting(我一直将工作树与 HEAD 进行比较,并希望将未跟踪的文件包含在 diff 中),add -N/--intent-to-add无法使用,因为它破坏了git stash.

So here's my git diffreplacement. It's not a particularly clean solution, but since I really only use it interactively, I'm OK with a hack:

所以这是我的git diff替代品。这不是一个特别干净的解决方案,但因为我真的只以交互方式使用它,所以我可以接受黑客攻击:

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
}

Typing just dwill include untracked files in the diff (which is what I care about in my workflow), and d args...will behave like regular git diff.

打字只会d在差异中包含未跟踪的文件(这是我在工作流程中关心的),并且d args...会像常规git diff.

Notes:

笔记:

  • We're using the fact here that git diffis really just individual diffs concatenated, so it's not possible to tell the doutput 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 gitat some point in the future, please leave a note here!
  • 我们在这里使用的事实git diff实际上只是连接了单个差异,因此不可能d从“真正的差异”中判断输出——除了所有未跟踪的文件最后排序的事实。
  • 此函数的唯一问题是即使重定向时输出也会着色;但我懒得为此添加逻辑。
  • 我找不到任何方法来通过为git diff. 如果有人想出了如何做到这一点,或者git将来某个时候添加了某个功能,请在此处留言!

回答by user1587520

Not 100% to the point, but if for some reason you don't want to add your files to the index as suggested by the accepted answer, here is another option:

不是 100%,但如果由于某种原因您不想按照已接受的答案的建议将文件添加到索引中,这里有另一种选择:

If the files are untracked, obviously the diff is the whole file, so you can just view them with less:

如果文件未跟踪,显然差异是整个文件,因此您可以使用 less 查看它们:

less $(git ls-files --others --exclude-standard)

Navigate between them with :nand :pfor next and previous..

使用:n:p为下一个和上一个在它们之间导航。

Update from the comments:If you need a patch format you can also combine it with git diff:

从评论更新:如果您需要补丁格式,您还可以将其与git diff

git ls-files --others --exclude-standard | xargs -n 1 git --no-pager diff /dev/null | less

You can also redirect the output to a file or use an other diff command in this case.

在这种情况下,您还可以将输出重定向到文件或使用其他 diff 命令。

回答by Amol Pujari

git add -A
git diff HEAD

Generate patch if required, and then:

如果需要,生成补丁,然后:

git reset HEAD

回答by Alejandro Moreno

this works for me:

这对我有用:

git add my_file.txt
git diff --cached my_file.txt
git reset my_file.txt

Last step is optional, it will leave the file in the previous state (untracked)

最后一步是可选的,它将使文件保持先前的状态(未跟踪)

useful if you are creating a patch too:

如果您也正在创建补丁,则很有用:

  git diff --cached my_file.txt > my_file-patch.patch

回答by alairock

Changes work when staged and non-staged with this command. New files work when staged:

使用此命令在暂存和非暂存时更改工作。新文件在暂存时工作:

$ git diff HEAD

If they are not staged, you will only see file differences.

如果它们没有暂存,您将只会看到文件差异。

回答by radzimir

For one file:

对于一个文件:

git diff --no-index /dev/null new_file

For all new files:

对于所有新文件:

for next in $( git ls-files --others --exclude-standard ) ; do git --no-pager diff --no-index /dev/null $next; done;

As alias:

作为别名:

alias gdnew="for next in $( git ls-files --others --exclude-standard ) ; do git --no-pager diff --no-index /dev/null $next; done;"

For all modified and new files combined as one command:

对于合并为一个命令的所有已修改文件和新文件:

{ git --no-pager diff; gdnew }

回答by adg

usually when i work with remote location teams it is important for me that i have prior knowledge what change done by other teams in same file, before i follow git stages untrack-->staged-->commit for that i wrote an bash script which help me to avoid unnecessary resolve merge conflict with remote team or make new local branch and compare and merge on main branch

通常,当我与远程位置团队合作时,重要的是我事先了解其他团队在同一文件中所做的更改,然后在我遵循 git stage untrack-->staged-->commit 之前,我编写了一个 bash 脚本帮助我避免不必要的解决与远程团队的合并冲突或创建新的本地分支并在主分支上进行比较和合并

#set -x 
branchname=`git branch | grep -F '*' |  awk '{print }'`
echo $branchname
git fetch origin ${branchname}
for file in `git status | grep "modified" | awk "{print }" `
do
echo "PLEASE CHECK OUT GIT DIFF FOR "$file 
git difftool FETCH_HEAD $file ;
done

in above script i fetch remote main branch (not necessary its master branch)to FETCH_HEAD them make a list of my modified file only and compare modified files to git difftool

在上面的脚本中,我获取远程主分支(不需要它的主分支)到 FETCH_HEAD 他们只列出我修改过的文件并将修改后的文件与 git difftool 进行比较

here many difftool supported by git, i configure 'Meld Diff Viewer' for good GUI comparison .

这里有许多 git 支持的 difftool,我配置了“Meld Diff Viewer”以进行良好的 GUI 比较。

回答by Pradhan

Assuming you do not have local commits,

假设您没有本地提交,

git diff origin/master