我进行了一些需要提交的更改。如何查看为下一次提交暂存的所有文件的差异?有没有一个方便的单行本?
git status
仅显示暂存文件的名称,但我想看看实际的差异。
git-diff(1)
手册页显示:
git diff [--选项] [--] [...]
这个表单是查看你相对于索引(下一次提交的暂存区)所做的更改。换句话说,差异是您可以告诉 git 进一步添加到索引中但您仍然没有添加的内容。您可以使用 git-add(1) 暂存这些更改。
I staged a few changes to be committed. How do I see the diffs of all files which are staged for the next commit? Is there a handy one-liner for this?
git status
only shows names of files which are staged, but I want to see the actual diffs.
The git-diff(1)
man page says:
git diff [--options] [--] […]
This form is to view the changes you made relative to the index (staging area for the next commit). In other words, the differences are what you could tell git to further add to the index but you still haven't. You can stage these changes by using git-add(1).
发布评论
评论(16)
它应该只是:
--cached
表示根据当前HEAD
显示缓存/索引中的更改(即分阶段更改)。--staged
是--cached
的同义词。--staged
和--cached
并不指向HEAD
,只是相对于HEAD
的区别。如果您使用git add --patch
(或git add -p
)选择要提交的内容,--staged
将返回暂存的内容。It should just be:
--cached
means show the changes in the cache/index (i.e. staged changes) against the currentHEAD
.--staged
is a synonym for--cached
.--staged
and--cached
does not point toHEAD
, just difference with respect toHEAD
. If you cherry pick what to commit usinggit add --patch
(orgit add -p
),--staged
will return what is staged.一个简单的图形使这一点更清楚:
git diff
显示工作目录之间的更改和索引。这显示了已更改的内容,但未进行提交。
git diff --cached
显示索引和 HEAD 之间的更改(这是此分支上的最后一次提交)。这显示了已添加到索引并暂存提交的内容。
git diff HEAD
显示工作目录和 HEAD 之间的所有更改(包括索引中的更改)。这显示了自上次提交以来的所有更改,无论它们是否已暂存以供提交。
另外:
365Git。
A simple graphic makes this clearer:
git diff
Shows the changes between the working directory and the index. This shows what has been changed, but is not staged for a commit.
git diff --cached
Shows the changes between the index and the HEAD (which is the last commit on this branch). This shows what has been added to the index and staged for a commit.
git diff HEAD
Shows all the changes between the working directory and HEAD (which includes changes in the index). This shows all the changes since the last commit, whether or not they have been staged for commit or not.
Also:
There is a bit more detail on 365Git.
请注意,
git status -v
还显示了分阶段的更改!(这意味着您需要使用 git add 进行一些更改)。没有分阶段更改,没有与
git status -v
的差异。自 Git 1.2.0,2006 年 2 月 起,它就
以长格式(默认) 执行此操作),
git status
有一个未记录的“verbose”选项它实际上显示了 HEAD 和索引之间的差异。它即将变得更加完整:请参阅“在 git diff 中显示暂存树和工作树?”(git 2.3.4+,2015 年第 2 季度):
Note that
git status -v
also shows the staged changes!(meaning you need to have staged some changes, with
git add
). No staged changes, no diff withgit status -v
.It does that since Git 1.2.0, February 2006)
In its long form (default),
git status
has an undocumented "verbose" option which actually display the diff between HEAD and index.And it is about to become even more complete: see "Show both staged & working tree in git diff?" (git 2.3.4+, Q2 2015):
如果您对视觉并排视图感兴趣,diffuse 视觉差异工具可以这样做。如果进行一些但不是全部更改,它甚至会显示三个窗格。如果发生冲突,甚至会有四个窗格。
调用它
在 Git 工作副本中
。如果你问我,我会说这是我十年来见过的最好的视觉效果。此外,它并不特定于 Git:它可以与大量其他 VCS 进行互操作,包括 SVN、Mercurial、Bazaar...
另请参阅:展示舞台和表演git diff 中的工作树?
If you'd be interested in a visual side-by-side view, the diffuse visual diff tool can do that. It will even show three panes if some but not all changes are staged. In the case of conflicts, there will even be four panes.
Invoke it with
in your Git working copy.
If you ask me, the best visual differ I've seen for a decade. Also, it is not specific to Git: It interoperates with a plethora of other VCS, including SVN, Mercurial, Bazaar, ...
See also: Show both staged & working tree in git diff?
对于暂存区域与存储库(上次提交)比较,使用
该命令将暂存(
$ git add fileName
)更改与上次提交进行比较。如果您想查看已暂存的内容将进入下一次提交,可以使用 git diff --staged。此命令将您的分阶段更改与上次提交进行比较。对于工作与暂存比较,使用
该命令将工作目录中的内容与暂存区域中的内容进行比较。需要注意的是,git diff 本身并不显示自上次提交以来所做的所有更改 - 仅显示尚未暂存的更改。如果您已暂存所有更改(
$ git add fileName
),则 git diff 不会给您任何输出。另外,如果您暂存一个文件(
$ git add fileName
),然后对其进行编辑,则可以使用 git diff 查看已暂存的文件中的更改和未暂存的更改。For Staging Area vs Repository(last commit) comparison use
The command compares your staged(
$ git add fileName
) changes to your last commit. If you want to see what you’ve staged that will go into your next commit, you can use git diff --staged. This command compares your staged changes to your last commit.For Working vs Staging comparison use
The command compares what is in your working directory with what is in your staging area. It’s important to note that git diff by itself doesn’t show all changes made since your last commit — only changes that are still unstaged. If you’ve staged all of your changes(
$ git add fileName
), git diff will give you no output.Also, if you stage a file(
$ git add fileName
) and then edit it, you can use git diff to see the changes in the file that are staged and the changes that are unstaged.使用视觉差异工具
默认答案(在命令行)
此处的最佳答案正确显示了如何查看
Index
中的缓存/暂存更改:或
$ git diff --staged< /code> 这是一个别名。
相反,启动 Visual Diff 工具
默认答案将在 git bash 上(即在命令行或控制台中)吐出差异更改。对于那些喜欢以视觉方式表示暂存文件差异的人,git 中提供了一个脚本,它为每个查看的文件启动一个可视化差异工具,而不是在命令行上显示它们,称为
difftool
:将执行与 git diff --staged 相同的操作,除了任何时候运行 diff 工具时(即每次 diff 处理文件时),它将启动默认的可视化 diff 工具(在我的环境,这是 VS Code,使用
code
可执行文件)。该工具启动后,git diff 脚本将暂停,直到您的可视化 diff 工具关闭。因此,您需要关闭每个文件才能查看下一个文件。
您始终可以在 git 命令中使用
difftool
代替diff
对于您所有的视觉差异需求,
git difftool
将代替任何>git diff
命令,包括所有选项。例如,要启动视觉差异工具而不询问是否为每个文件执行此操作,请添加
-y
选项(我想通常您会需要这个!!):在这种情况下,它将一次拉出视觉差异工具中的每个文件,并在工具关闭后调出下一个文件。
或者查看
Index
中暂存的特定文件的差异:有关所有选项,请参阅手册页:
设置可视化 Git 工具
要使用默认值以外的可视化 git 工具,使用
-t
选项:或者,请参阅 difftool 手册页,了解如何配置 git 以使用不同的默认可视化 diff 工具。
vscode 作为 diff/merge 工具的
.gitconfig
条目示例设置 difftool 的一部分涉及更改
.gitconfig
文件,可以通过在幕后更改它的 git 命令,或者直接编辑即可。您可以在主目录中找到
.gitconfig
,例如 Unix 中的~
或 Windows 上的c:\users\
)。或者,您可以使用
git config -e --global
在默认 Git 编辑器中打开用户.gitconfig
。以下是我的全局用户
.gitconfig
中 VS Code 作为 diff 工具和合并工具的示例条目:USING A VISUAL DIFF TOOL
The Default Answer (at the command line)
The top answers here correctly show how to view the cached/staged changes in the
Index
:or
$ git diff --staged
which is an alias.Launching the Visual Diff Tool Instead
The default answer will spit out the diff changes at the git bash (i.e. on the command line or in the console). For those who prefer a visual representation of the staged file differences, there is a script available within git which launches a visual diff tool for each file viewed rather than showing them on the command line, called
difftool
:This will do the same this as
git diff --staged
, except any time the diff tool is run (i.e. every time a file is processed by diff), it will launch the default visual diff tool (in my environment, this is VS Code, using thecode
executable).After the tool launches, the git diff script will pause until your visual diff tool is closed. Therefore, you will need to close each file in order to see the next one.
You Can Always Use
difftool
in place ofdiff
in git commandsFor all your visual diff needs,
git difftool
will work in place of anygit diff
command, including all options.For example, to have the visual diff tool launch without asking whether to do it for each file, add the
-y
option (I think usually you'll want this!!):In this case it will pull up each file in the visual diff tool, one at a time, bringing up the next one after the tool is closed.
Or to look at the diff of a particular file that is staged in the
Index
:For all the options, see the man page:
Setting up Visual Git Tool
To use a visual git tool other than the default, use the
-t <tool>
option:Or, see the difftool man page for how to configure git to use a different default visual diff tool.
Example
.gitconfig
entries for vscode as diff/merge toolPart of setting up a difftool involves changing the
.gitconfig
file, either through git commands that change it behind the scenes, or editing it directly.You can find your
.gitconfig
in your home directory,such as~
in Unix or normallyc:\users\<username>
on Windows).Or, you can open the user
.gitconfig
in your default Git editor withgit config -e --global
.Here are example entries in my global user
.gitconfig
for VS Code as both diff tool and merge tool:您可以使用此命令。
git diff 的
--cached
选项表示获取暂存文件,--name-only
选项表示仅获取暂存文件的名称。文件。You can use this command.
The
--cached
option ofgit diff
means to get staged files, and the--name-only
option means to get only names of the files.从 1.7 版本及更高版本开始,它应该是:
From version 1.7 and later it should be:
如果您的意图是推送目标远程存储库分支,并且您在提交更改日志中的第一次传递不完整,则可以在像这样推送之前更正提交语句。
本地
...进行一些更改......
回忆提交中未提及的更多更改...
git diff origin/master # 查看暂存但未推送的更改
...修改暂存提交语句...
If your intentions are to push-target a remote repo branch and your first pass at a commit change log were incomplete, you can correct the commit statement before pushing like this.
Locally
... make some changes ...
... recall more changes unmentioned in commit ...
git diff origin/master # look at staged but not pushed changes
... amend staged commit statement ...
如果您有多个文件进行了分阶段更改,那么使用 git add -i 可能更实用,然后选择 6: diff,最后选择文件你有兴趣。
If you have more than one file with staged changes, it may more practical to use
git add -i
, then select6: diff
, and finally pick the file(s) you are interested in.要查看特定阶段文件(或多个文件)的差异,您可以使用
例如,
To see the differences for a specific stage file (or files) you can use
For example,
默认情况下,git diff 用于显示未添加到 git 更新文件列表中的更改。但是如果你想显示添加或标记的更改,那么你需要提供额外的选项,让 git 知道你对标记或添加的文件 diff 感兴趣。
示例
一旦添加了文件,您就不能使用默认的“git diff”。您必须这样做:-
By default git diff is used to show the changes which is not added to the list of git updated files. But if you want to show the changes which is added or stagged then you need to provide extra options that will let git know that you are interested in stagged or added files diff .
Example
Once you added the files , you can't use default of 'git diff' .You have to do like this:-
git gui
和git-cola
是图形实用程序,可让您查看和操作索引。两者都包括暂存文件的简单视觉差异,并且 git-cola 还可以启动更复杂的并排视觉差异工具。请参阅我密切相关的答案如何从git索引中删除文件?,以及Git - GUI 客户端。
git gui
andgit-cola
are graphical utilities that let you view and manipulate the index. Both include simple visual diffs for staged files, andgit-cola
can also launch a more sophisticated side-by-side visual diff tool.See my closely related answer at How to remove a file from the index in git?, and also this official catalog of Git - GUI Clients.
--cached
对我不起作用,...其中,受到git log
git diff origin/.. 的 启发;
做到了。The
--cached
didn't work for me, ... where, inspired bygit log
git diff origin/<branch>..<branch>
did.另一个使这一切变得简单的工具是 Emacs 中的 Magit 模式。其默认视图列出了已暂存和未暂存的更改。它的作用就像 git add -p 一样,因为您可以使用编辑器命令轻松地暂存或取消暂存块(甚至单行代码)。了解标准 git 瓷器很重要,但我很少再使用 git diff --cached 了。
https://magit.vc/
Another tool that makes this easy is Magit mode in Emacs. Its default view lists both staged and unstaged changes. It acts like
git add -p
on steroids, because you can easily stage or unstage hunks (or even single lines of code) with editor commands. It's important to know the standard git porcelain, but I rarely usegit diff --cached
anymore.https://magit.vc/
还想一下 gitk 工具,它随 git 一起提供,对于查看更改非常有用
Think about the
gitk
tool also , provided with git and very useful to see the changes