如何在Git中显示未提交的更改

时间:2016-03-14 02:20:58

标签: git git-diff

如何在Git中显示未提交的更改?

我SFTW'ed,这些命令无效。

teyan@TEYAN-THINK MINGW64 /d/nano/repos/PSTools/psservice (teyan/psservice)
$ git status
On branch teyan/psservice
Your branch is up-to-date with 'origin/teyan/psservice'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   psservice.c
        modified:   psservice.vcxproj.filters


teyan@TEYAN-THINK MINGW64 /d/nano/repos/PSTools/psservice (teyan/psservice)
$ git diff

teyan@TEYAN-THINK MINGW64 /d/nano/repos/PSTools/psservice (teyan/psservice)
$ git diff master
fatal: ambiguous argument 'master': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

4 个答案:

答案 0 :(得分:210)

  

如何在Git中显示未提交的更改

您要查找的命令是git diff

  

git diff - 显示提交,提交和工作树等之间的更改

以下是它可以使用的一些选项

git diff (无参数)
打印工作目录和索引之间的差异。

<强> git diff --cached
打印索引和HEAD(当前提交)之间的差异。

<强> git diff HEAD
打印工作目录与HEAD之间的差异。

<强> git diff --name-only
仅显示已更改文件的名称。

<强> git diff --name-status
仅显示已更改文件的名称和状态

<强> git diff --color-words
逐字逐句而不是逐行。

以下是git diff --color-words的输出示例:

enter image description here

enter image description here

答案 1 :(得分:30)

您已经进行了更改(可能是通过运行git add),因此为了获得差异,您需要运行:

git diff --cached

(普通git diff只会显示未分期更改。)

答案 2 :(得分:0)

对我来说,唯一起作用的是

git diff HEAD

(包括暂存文件),git diff --cached仅显示暂存文件。

答案 3 :(得分:0)

我遇到git status显示更改的情况,但是git diff不打印任何内容,尽管有几行更改。但是:

$ git diff data.txt > myfile
$ cat myfile
<prints diff>

Raspbian上的Git 2.20.1。其他命令,例如git checkoutgit pull,可以毫无问题地打印到标准输出。