git 如何显示已上演的更改?

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

How do I show the changes which have been staged?

gitdiffdvcsgit-diffgit-stage

提问by Frerich Raabe

I staged a few changes to be committed; how can I see the diff of all files which are staged for the next commit? I'm aware of git status, but I'd like to see the actual diffs - not just the names of files which are staged.

我上演了一些要提交的更改;如何查看为下一次提交暂存的所有文件的差异?我知道git status,但我想看到实际的差异 - 而不仅仅是暂存的文件的名称。

I saw that the git-diff(1)man page says

我看到git-diff(1)手册页说

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 couldtell git to further add to the index but you still haven't. You can stage these changes by using git-add(1).

git diff [--options] [--] […]

此表单用于查看您相对于索引所做的更改(下一次提交的暂存区)。换句话说,不同之处在于您可以告诉 git 进一步添加到索引中,但您仍然没有。您可以使用 git-add(1) 暂存这些更改。

Unfortunately, I can't quite make sense of this. There must be some handy one-liner which I could create an alias for, right?

不幸的是,我不能完全理解这一点。一定有一些方便的单线,我可以为其创建别名,对吗?

回答by CB Bailey

It should just be:

它应该只是:

git diff --cached

--cachedmeans show the changes in the cache/index (i.e. staged changes) against the current HEAD. --stagedis a synonym for --cached.

--cached表示针对当前HEAD. --staged是 的同义词--cached

--stagedand --cacheddoes not point to HEAD, just difference with respect to HEAD. If you cherry pick what to commit using git add --patch(or git add -p), --stagedwill return what is staged.

--staged--cached不是指HEAD,只是区别就相对HEAD。如果您选择使用git add --patch(或git add -p)提交的内容,--staged将返回已暂存的内容。

回答by Abizern

A simple graphic makes this clearer:

一个简单的图形使这更清楚:

Simple Git diffs

简单的 Git 差异

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

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.

显示索引和 HEAD(这是此分支上的最后一次提交)之间的更改。这显示了已添加到索引并暂存以进行提交的内容。

git diff HEAD

git diff 头

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.

显示工作目录和 HEAD 之间的所有更改(包括索引中的更改)。这显示自上次提交以来的所有更改,无论它们是否已暂存以进行提交。

Also:

还有

There is a bit more detail on 365Git.

365Git上有更多细节

回答by krlmlr

If you'd be interested in a visual side-by-side view, the diffusevisual 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.

如果您对视觉并排视图感兴趣,漫反射视觉差异工具可以做到这一点。如果某些但不是所有更改都已上演,它甚至会显示三个窗格。在发生冲突的情况下,甚至会有四个窗格。

Screenshot of diffuse with staged and unstaged edits

带有分阶段和非分阶段编辑的漫反射屏幕截图

Invoke it with

调用它

diffuse -m

in your Git working copy.

在您的 Git 工作副本中。

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, ...

如果你问我,这是我十年来见过的最好的视觉效果。此外,它不是 Git 特有的:它与大量其他 VCS 互操作,包括 SVN、Mercurial、Bazaar ......

See also: Show both staged & working tree in git diff?

另请参阅:在 git diff 中同时显示暂存树和工作树?

回答by VonC

Note that git status -valsoshows the staged changes! (meaning you need to have staged -- git add-- some changes. No staged changes, no diff with git status -v.
It does that since Git 1.2.0, February 2006)

请注意,git status -v显示了分阶段更改!(这意味着您需要暂存 -- git add-- 一些更改。没有暂存的更改,没有差异git status -v
Git 1.2.0, 2006 年 2 月以来就是这样)

In its long form (default), git statushas an undocumented "verbose" option which actually display the diff between HEAD and index.

在其长格式(默认)中,git status有一个未记录的“详细”选项,它实际显示 HEAD 和索引之间的差异。

And it is about to become even more complete: see "Show both staged & working tree in git diff?" (git 2.3.4+, Q2 2015):

它即将变得更加完整:请参阅“在 git diff 中同时显示暂存树和工作树?”(git 2.3.4+,2015 年第二季度):

git status -v -v

回答by Yash Patadia

You can use this command.

您可以使用此命令。

git diff --cached --name-only

The --cachedoption of git diffmeans to get staged files, and the --name-onlyoption means to get only names of the files.

--cached的选择git diff手段来获得筹备的文件,并--name-only选择手段,只得到文件名。

回答by ML13

From version 1.7 and later it should be:

从 1.7 版及更高版本开始,它应该是:

git diff --staged

回答by LightCC

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:

此处的最佳答案正确显示了如何查看缓存/暂存更改Index

$ git diff --cached

or $ git diff --stagedwhich is an alias.

或者$ git diff --staged哪个是别名。



Launching the Visual Diff Tool Instead

改为启动 Visual Diff 工具

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:

默认答案将在 git bash(即在命令行或控制台中)吐出差异更改。对于那些更喜欢暂存文件差异的可视化表示的人,git 中有一个脚本可用,它为每个查看的文件启动一个可视化差异工具,而不是在命令行上显示它们,称为difftool

$ git difftool --staged

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 kdiff3).

这将与 执行相同的操作git diff --staged,除了 diff 工具运行时(即每次 diff 处理文件时),它将启动默认的可视化 diff 工具(在我的环境中,这是kdiff3)。

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.

工具启动后,git diff 脚本将暂停,直到您的可视化差异工具关闭。因此,您需要关闭每个文件才能看到下一个文件。



You Can Always Use difftoolin place of diffin git commands

你总是可以用difftool代替diffgit 命令

For all your visual diff needs, git difftoolwill work in place of any git diffcommand, including all options.

对于您所有的视觉差异需求,git difftool将代替任何git diff命令,包括所有选项。

For example, to have the visual diff tool launch without asking whether to do it for each file, add the -yoption (I think usually you'll want this!!):

例如,要启动视觉差异工具而不询问是否为每个文件执行此操作,请添加-y选项(我认为通常您会想要这个!!):

$ git difftool -y --staged

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:

或者查看暂存于 中的特定文件的差异Index

$ git difftool -y --staged <<relative path/filename>>

For all the options, see the man page:

有关所有选项,请参阅手册页:

$ git difftool --help



Setting up Visual Git Tool

设置可视化 Git 工具

To use a visual git tool other than the default, use the -t <tool>option:

要使用默认值以外的可视化 git 工具,请使用以下-t <tool>选项:

$ git difftool -t <tool> <<other args>>

Or, see the difftool man page for how to configure git to use a different default visual diff tool.

或者,请参阅 difftool 手册页了解如何配置 git 以使用不同的默认可视化差异工具。



Example .gitconfigentries for vscode as diff/merge tool

.gitconfigvscode 作为差异/合并工具的示例条目

Part of setting up a difftool involves changing the .gitconfigfile, either through git commands that change it behind the scenes, or editing it directly.

设置 difftool 的一部分涉及更改.gitconfig文件,要么通过在幕后更改它的 git 命令,要么直接编辑它。

You can find your .gitconfigin your home directory,such as ~in Unix or normally c:\users\<username>on Windows).

您可以.gitconfig在您的主目录中找到您的目录,例如~在 Unix 中或通常c:\users\<username>在 Windows 中)。

Or, you can open the user .gitconfigin your default Git editor with git config -e --global.

或者,您可以.gitconfig在默认 Git 编辑器中使用git config -e --global.

Here are example entries in my global user .gitconfigfor VS Code as both diff tool and merge tool:

以下是我的全局用户.gitconfig中 VS Code 的示例条目,作为差异工具和合并工具:

[diff]
    tool = vscode
    guitool = vscode
[merge]
    tool = vscode
    guitool = vscode
[mergetool]
    prompt = true
[difftool "vscode"]
    cmd = code --wait --diff \"$LOCAL\" \"$REMOTE\"
    path = c:/apps/vscode/code.exe
[mergetool "vscode"]
    cmd = code --wait \"$MERGED\"
    path = c:/apps/vscode/code.exe

回答by Goyal Vicky

For Staging Area vs Repository(last commit) comparison use

对于暂存区与存储库(最后一次提交)比较使用

 $git diff --staged

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.

该命令将您的 staged( $ git add fileName) 更改与上次提交进行比较。如果您想查看将进入下一次提交的暂存内容,可以使用 git diff --staged。此命令将您的暂存更改与上次提交进行比较。

For Working vs Staging comparison use

对于工作与分期比较使用

$ git diff 

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.

该命令将工作目录中的内容与暂存区中的内容进行比较。重要的是要注意 git diff 本身并不显示自上次提交以来所做的所有更改?-?仅显示仍未暂存的更改。如果您已暂存所有更改( $ git add fileName),则 git diff 不会给您任何输出。

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.

此外,如果您$ git add fileName暂存文件()然后对其进行编辑,则可以使用 git diff 查看已暂存的文件中的更改以及未暂存的更改。

回答by Marc Condon

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.

如果您的意图是推送目标远程 repo 分支并且您在提交更改日志中的第一次传递不完整,您可以在像这样推送之前更正提交语句。

Locally

本地

... make some changes ...

……做些改变……

git diff # look at unstaged changes

git commit -am"partial description of changes"

... recall more changes unmentioned in commit ...

... 回想提交中未提及的更多更改 ...

git diff origin/master # look at staged but not pushed changes

git diff origin/master # 查看暂存但未推送的更改

... amend staged commit statement ...

... 修改分阶段提交声明 ...

git commit --amend -m"i missed mentioning these changes ...."

git push

回答by Fred Schoen

If you have more than one file with staged changes, it may more practical to use git add -i, then select 6: diff, and finally pick the file(s) you are interested in.

如果您有多个分阶段更改的文件,使用 可能更实用git add -i,然后选择6: diff,最后选择您感兴趣的文件。