git diff 没有输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8544211/
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 12:32:24  来源:igfitidea点击:

git diff gives no output

git

提问by blokeley

If I run git diffI would expect to see a list of changes of my working directory relative to whatever had been committed before (or a list of the working directory contents if it's a new repo with no commits). Try this example:

如果我运行,git diff我希望看到我的工作目录相对于之前提交的任何内容的更改列表(或者工作目录内容的列表,如果它是一个没有提交的新存储库)。试试这个例子:

$ mkdir temp
$ cd temp
$ git init
$ echo "first line" > test.txt
$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       test.txt
nothing added to commit but untracked files present (use "git add" to track)

Let's see a diff of test.txt:

让我们看看 test.txt 的差异:

$ git diff

This gives no output!

这没有输出!

I would expect to see a diff like + first linebut instead I get nothing. It doesn't tell me what's going on. People on stackoverflow tell me to git addsome files so I do:

我希望看到类似的差异,+ first line但我什么也没得到。它没有告诉我发生了什么。stackoverflow 上的人告诉我git add一些文件,所以我这样做:

$ git add .
$ git diff

Still nothing!

依然没有!

Git GUIshows the changes.

Git GUI显示更改。

git status -vshows the changes.

git status -v显示变化。

But for some reason git diffdoesn't show anything.

但由于某种原因git diff没有显示任何内容。

So my questions are:

所以我的问题是:

  1. How, in plain English, does git diffwork?
  2. How can I show a diff of all the changes I've made (unstaged and staged)?
  1. 用简单的英语如何git diff工作?
  2. 如何显示我所做的所有更改(未暂存和暂存)的差异?

Some people at my company are using git but the SVN crowd are going to point at this as a case of where git is too confusing to be usable.

我公司的一些人正在使用 git,但 SVN 人群将指出这是 git 太混乱而无法使用的情况。

回答by m0tive

Why do you get no git diff before adding?

为什么在添加之前没有 git diff ?

git does not treat files on the filesystem as automatically included in the version control system, you have to add things explicitly into the git repository (as you are doing by adding the current directory with git add .).

git 不会将文件系统上的文件视为自动包含在版本控制系统中,您必须将内容显式添加到 git 存储库中(就像您通过添加当前目录一样git add .)。

There is no output to git diffbecause git sees no changes insideyour repository, only files outsidethe repository, which it considers 'untracked' and so ignores when generating a diff.

没有输出git diff,因为Git把没有变化里面你的资料库,只有文件存储库,它产生差异时,认为“未跟踪”等忽略。

I found this one of the key differences to vcs like svn (along with staging and ignoring directories).

我发现这是与 svn 等 vcs 的主要区别之一(以及暂存和忽略目录)。

If you want the untracked files to be included, git addthem.

如果您希望包含未跟踪的文件,则git add它们。

If you don't want them in your repo add them to your .gitignore(see git ignore --help). This is good for C object files or python .pycfiles.

如果您不希望它们出现在您的 repo 中,请将它们添加到您的.gitignore(请参阅参考资料git ignore --help)。这对 C 对象文件或 python.pyc文件很有用。

Why do I get no git diff afteradding?!

为什么我添加没有 git diff ?!

So this is slightly different. If you do git statusyou will see the file is now in the staging area. This is the area for files that you are about to commit.

所以这有点不同。如果您这样做,git status您将看到该文件现在位于暂存区。这是您将要提交的文件的区域。

When you git adda new file into the git repo it skips the working copy and goes straight into the staging area. This make sense in a way, git addalways moves files into staging area whether it is tracked or untracked.

当您git add将新文件放入 git 存储库时,它会跳过工作副本并直接进入暂存区。这在某种程度上是有意义的,git add无论文件被跟踪还是未跟踪,总是将文件移动到暂存区。

To see the differences between the last check in and the staging area do git diff --cached.

要查看上次登记入住和暂存区之间的差异,请执行以下操作git diff --cached

To see the differences between the staging area and your working copy do git diff. If there is nothing in the staging area then this is the same as doing a diff between the last check in and your working copy.

要查看暂存区和您的工作副本之间的差异,请执行git diff. 如果暂存区中没有任何内容,则这与在上次签入和您的工作副本之间进行差异相同。

回答by Dan Tenenbaum

I have seen situations where there really shouldbe output from git diffbut there isn't; adding
--no-pagerin between gitand diffDOES work:

我见过确实应该有输出git diff但没有输出的情况;
--no-pager在两者之间添加git并且diff确实有效:

git --no-pager diff

...so does explicitly setting the pager to be lesswith

......所以没有明确设置寻呼机是less

git config --global core.pager 'less'

Even though lessis supposed to be the default pager.

即使less应该是默认寻呼机。

This was in Ubuntu 12.04 LTS. I'm just adding this in case others with the same problem come looking for a solution and the above doesn't answer the question.

这是在 Ubuntu 12.04 LTS 中。我只是添加这个以防其他有同样问题的人来寻找解决方案并且上面没有回答问题。

I found the information about pager settings in the git docs.

我在git docs 中找到了有关寻呼机设置的信息 。

回答by lig

Basing on your git statusoutput there is nothing to show for git diffwithout additional parameters. There is nothing to show for git diff --cachedand git diff HEADas all of these commands rely on changes already known to git.

根据您的git status输出,git diff没有其他参数就没有什么可显示的。没有什么可显示的git diff --cachedgit diff HEAD因为所有这些命令都依赖于 git 已知的更改。

You have no staged files and no changed files from those that are under version control now.

您没有暂存文件,也没有从现在受版本控制的文件中更改的文件。

After you add test.txtunder gitcontrol you will get desired output.

在控制test.txt下添加后,git您将获得所需的输出。

Just type

只需输入

git add test.txt

or

或者

git add .

Than this file will be added under version control. And future changes of this file will be shown by git diff.

此文件将在版本控制下添加。并且此文件的未来更改将由 git diff 显示。

回答by Bludzee

1.How, in plain English, does git diff work?

1.用简单的英语来说,git diff 是如何工作的?

How git diffworks all depends on what parameters you pass to it.

如何git diff工作完全取决于您传递给它的参数。

Depending on the parameters, git diffwill show the changes between two commits, or between a commit and the working tree, etc...

根据参数,git diff将显示两次提交之间或提交与工作树之间的更改等...

For example, git diff, git diff --stagedand git diff HEADare described further below.

例如,git diffgit diff --stagedgit diff HEAD在下文进一步描述。

2.How can I show a diff of all the changes I've made (unstaged and staged)?

2.如何显示我所做的所有更改(未暂存和暂存)的差异?

By using both commands git diff HEADand git diff --staged.

通过同时使用命令git diff HEADgit diff --staged.

But in the case of a repository just created with git init, the question doesn't really make sense and git diff HEADcannot be used: the history being empty, there's no commit and no HEADyet!

但是对于刚刚创建的存储库git init,这个问题没有意义并且git diff HEAD不能使用:历史为空,没有提交,HEAD还没有!



  • git diffshows the changes in your working tree relative to the last commit, only for tracked files

  • git diff HEADshows the changes in your working tree relative to the last commit (includes files that are not tracked)

  • git diff --staged(or its synonym git diff --cached) shows the changes you staged for the next commit relative to the last commit

  • git diff显示相对于上次提交的工作树中的更改,仅适用于跟踪文件

  • git diff HEAD显示相对于上次提交的工作树中的更改(包括未跟踪的文件)

  • git diff --staged(或其同义词git diff --cached)显示您为下一次提交相对于上次提交所做的更改

There are many other ways to invoke git diff, for comparing arbitrary commits or branches, etc. Invoke git help diffto read more about it.

还有许多其他方法可以调用git diff,用于比较任意提交或分支等。调用git help diff以了解更多信息。

回答by fafl

I had a LESS=-Renvironment variable set. It made git diffshow a blank screen.

我有一个LESS=-R环境变量集。它使git diff显示一个空白屏幕。

The solution for me:

对我来说的解决方案:

unset LESS

回答by Vallier

GIT DIFF DOESN'T WORK AFTER STASH

GIT DIFF 在 STASH 后不起作用

Someone may run into this after attempting to apply stashed changes like I did.

有人可能会像我一样尝试应用隐藏的更改后遇到这个问题。

  • Stash changes
  • Move to different branch and do some work
  • return to work-in-progress branch, update (git pull) then git stash apply(possibly with merge conflict)
  • git diff no longer works...
  • 存储更改
  • 移动到不同的分支并做一些工作
  • 返回 work-in-progress 分支,更新 ( git pull) 然后git stash apply(可能有合并冲突)
  • git diff 不再有效...

At this point simply stash your changes again (git stash), do git pullto ensure everything is up to date (because I'm paranoid about it), double check that all files are up to date in working directory (possibly run rsync if dealing with remote/local), then attempt to git stash applyagain and it should work!

此时只需再次存储您的更改 ( git stash),git pull确保一切都是最新的(因为我对此很偏执),仔细检查工作目录中的所有文件是否都是最新的(如果处理远程/本地),然后git stash apply再次尝试,它应该可以工作!

In answer to the original question, git diffisn't showing anything because you have a brand new directory, with a newly added file, but there are zero changes in the file for git diff to show. git statusis showing that you added a new file, but git diffis for showing changes within files. If you made changes to the file, committed it, then made additional changes to it and ran git diffit would then show you the changes to the file.

在回答原始问题时git diff没有显示任何内容,因为您有一个全新的目录和一个新添加的文件,但是文件中 git diff 显示的更改为零。git status显示您添加了一个新文件,但git diff用于显示文件中的更改。如果您对文件进行了更改,提交了它,然后对其进行了其他更改并运行git diff它,则会显示对文件的更改。

回答by Henk Langeveld

This is indeed related to the pager settings.

这确实与寻呼机设置有关。

I found that including '-e' in

我发现包括'-e'

export PAGER=less LESS=-cse

helps.

有帮助。

Without the '-e', lesswill quit on the first page. Somehow git will not even bother to display output in that case.

如果没有'-e',less将在第一页退出。在这种情况下,git 甚至不会以某种方式显示输出。

回答by Logesh R.

The first things is that I suggest you to save your file before using "git diff" command to make changes.

首先,我建议您在使用“git diff”命令进行更改之前先保存文件。

回答by H-man

You don't need any additional flags for git diff to work. First you create a file, and add it to the git repo with git add to staging and or execute a git commit to get it into your repository. Now, go to the file and make a change to it and save it. Just put a sentence in there stating "I will run the "git diff " command right after I save it". Save it. It is at that point you will see the difference when you run the git diff command. It will show the recent changes prior to executing a "git commit" or if you had only put the file initially into staging.

你不需要任何额外的标志来让 git diff 工作。首先,您创建一个文件,然后使用 git add 将其添加到 git 存储库中,或者执行 git commit 以将其添加到您的存储库中。现在,转到文件并对其进行更改并保存。只需在那里放一句话,说明“我将在保存后立即运行“git diff”命令”。保存。正是在这一点上,您将在运行 git diff 命令时看到差异。它将在执行“git commit”之前显示最近的更改,或者如果您最初只将文件放入暂存。

\TestGit>git diff File2.txt
diff --git a/File2.txt b/File2.txt
index e2ba019..eff7299 100644
--- a/File2.txt
+++ b/File2.txt
@@ -6,4 +6,6 @@ File two. Which is the second file that I will NOT add with the first file File1

 The goal here is to add the File1 but not the File 2

-using git add File1.txt
\ No newline at end of file
+using git add File1.txt.
+
+So far I have just done a git add on this file.
\ No newline at end of file

The lines with the + signs is the added content. If you use the --cached flag, and get result,s then your git status for the same file should report that the file has not been committed. Once you commit the file, you will not get any output for git diff. even with the --cached flag.

带 + 号的行是添加的内容。如果您使用 --cached 标志并获得结果,那么您对同一文件的 git status 应报告该文件尚未提交。提交文件后,您将不会获得 git diff 的任何输出。即使使用 --cached 标志。