git 提交前如何在git中查看文件差异

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

How to view file diff in git before commit

gitgit-diff

提问by Sauce McBoss

This often happens to me:

这经常发生在我身上:

I'm working on a couple related changes at the same time over the course of a day or two, and when it's time to commit, I end up forgetting what changed in a specific file. (This is just a personal git repo, so I'm ok with having more than one update in a commit.)

我在一两天内同时处理几个相关的更改,当需要提交时,我最终忘记了特定文件中的更改。(这只是一个个人 git 存储库,所以我可以在一次提交中进行多个更新。)

Is there any way to preview the changes between my local file, which is about to be checked in, and the last commit for that file?

有什么方法可以预览即将签入的本地文件与该文件的最后一次提交之间的更改吗?

Something like:

就像是:

git diff --changed /myfile.txt

And it would print out something like:

它会打印出如下内容:

line 23
  (last commit): var = 2+2
  (current):     var = myfunction() + 2

line 149
  (last commit): return var
  (current):     return var / 7

This way, I could quickly see what I had done in that file since it was last checked in.

这样,我可以快速查看自上次签入该文件以来我在该文件中所做的事情。

回答by Amber

If you want to see what you haven't git added yet:

如果您想查看尚未git add编辑的内容:

git diff myfile.txt

or if you want to see already added changes

或者如果您想查看已添加的更改

git diff --cached myfile.txt

回答by ouah

git diff HEAD file

will show you changes you added to your worktree from the last commit. All the changes (staged or not staged) will be shown.

将显示您从上次提交添加到工作树的更改。将显示所有更改(已暂存或未暂存)。

回答by kenorb

To check for local differences:

要检查本地差异:

git diff myfile.txt

or you can use a diff tool (in case you'd like to revert some changes):

或者您可以使用差异工具(以防您想恢复某些更改):

git difftool myfile.txt

To use git difftoolmore efficiently, install and use your favourite GUI tool such as Meld, DiffMerge or OpenDiff.

git difftool更有效地使用,请安装并使用您最喜欢的 GUI 工具,例如 Meld、DiffMerge 或 OpenDiff。

Note: You can also use .(instead of filename) to see current dir changes.

注意:您还可以使用.(而不是文件名)来查看当前目录更改。

In order to check changes per each line, use: git blamewhich will display which line was commited in which commit.

为了检查每行的更改,请使用:git blame这将显示在哪个提交中提交了哪一行。



To view the actual file before the commit (where masteris your branch), run:

要在提交之前查看实际文件(master您的分支在哪里),请运行:

git show master:path/my_file

回答by vhallac

Did you try -v(or --verbose) option for git commit? It adds the diff of the commit in the message editor.

您是否尝试过-v(或--verbose)选项git commit?它在消息编辑器中添加了提交的差异。

回答by anisbet

Another technique to consider if you want to compare a file to the last commit which is more pedantic:

如果要将文件与更迂腐的最后一次提交进行比较,则需要考虑的另一种技术:

git diff master myfile.txt

The advantage with this technique is you can also compare to the penultimate commit with:

这种技术的优点是您还可以与倒数第二次提交进行比较:

git diff master^ myfile.txt

and the one before that:

以及之前的那个:

git diff master^^ myfile.txt

Also you can substitute '~' for the caret '^' character and 'you branch name' for 'master' if you are not on the master branch.

如果您不在 master 分支上,您也可以用 '~' 代替插入符号 '^' 字符,用 'you branch name' 代替 'master'。

回答by Lakshman Prasad

I think this is the perfect use case warranting a GUI. - Although I totally understand that it can also be achieved well enough within the command line.

我认为这是保证 GUI 的完美用例。- 虽然我完全理解它也可以在命令行中很好地实现。

Personally, every commit of mine, I do from the git-gui. In which I can make multiple atomic commits with separate hunks/lines if it makes sense to do so.

就个人而言,我的每一次提交,我都是从 git-gui 做的。如果这样做有意义,我可以使用单独的大块/行进行多次原子提交。

Gut Gui enables viewing of the diffs in a well formatted colored interface, is rather light. Looks like this is something you should checkout too.

Gut Gui 可以在格式良好的彩色界面中查看差异,相当轻。看起来这也是你应该结账的东西。

回答by catanore

On macOS, go to the git root directory and enter git diff *

在 macOS 上,转到 git 根目录并输入 git diff *

回答by VitalyB

The best way I found, aside of using a dedicated commit GUI, is to use git difftool -d- This opens your diff tool in directory comparison mode, comparing HEAD with current dirty folder.

除了使用专用的提交 GUI,我发现的最好方法是使用git difftool -d- 这会在目录比较模式下打开您的差异工具,将 HEAD 与当前的脏文件夹进行比较。

回答by Tomachi

git difftool -d HEAD filename.txt

This shows a comparison using VI slit window in the terminal.

这显示了在终端中使用 VI 狭缝窗口的比较。