简体   繁体   English

如何在某个目录上“git diff”?

[英]How do I 'git diff' on a certain directory?

git diff actually runs a diff on all source code. git diff实际上在所有源代码上运行一个差异。 How do I do this on a certain directory, so that I can view modifications on files underneath it?如何在某个目录上执行此操作,以便我可以查看对其下文件的修改?

提供路径(本例中为 myfolder)并运行:

git diff myfolder/

If you're comparing different branches, you need to use -- to separate a Git revision from a filesystem path.如果要比较不同的分支,则需要使用--将 Git 修订版与文件系统路径分开。 For example, with two local branches, master and bryan-working :例如,有两个本地分支masterbryan-working

$ git diff master -- AFolderOfCode/ bryan-working -- AFolderOfCode/

Or from a local branch to a remote:或者从本地分支到远程:

$ git diff master -- AFolderOfCode/ origin/master -- AFolderOfCode/

Not only you can add a path, but you can add git diff --relative to get result relative to that folder.您不仅可以添加路径,还可以添加git diff --relative以获取相对于该文件夹的结果。

git -C a/folder diff --relative

And with Git 2.28 (Q3 2020), the commands in the " diff " family learned to honor the " diff.relative " configuration variable.在 Git 2.28(2020 年第三季度)中,“ diff ”系列中的命令学会了尊重“ diff.relative ”配置变量。

See commit c28ded8 (22 May 2020) by Laurent Arnoud ( spk ) .提交c28ded8通过(2020 5月22日)洛朗·阿诺德·( spk
(Merged by Junio C Hamano -- gitster -- in commit e34df9a , 02 Jun 2020) (由Junio C gitster合并-- gitster --提交 e34df9a 中,2020 年 6 月 2 日)

diff : add config option relative diff : 添加配置选项relative

Signed-off-by: Laurent Arnoud签字人:Laurent Arnoud
Acked-by: Đoàn Trần Công Danh确认者:Đoàn Trần Công Danh

The diff.relative boolean option set to true shows only changes in the current directory/value specified by the path argument of the relative option and shows pathnames relative to the aforementioned directory. diff.relative布尔选项设置为true仅显示由relative选项的path参数指定的当前目录/值的更改,并显示相对于上述目录的路径名。

Teach --no-relative to override earlier --relative--no-relative覆盖之前的--relative

Add for git-format-patch(1) options documentation --relative and --no-relative添加 git-format-patch(1) 选项文档--relative--no-relative

The documentation now includes :文档现在包括

diff.relative : diff.relative :

If set to ' true ', ' git diff ' does not show changes outside of the directory and show pathnames relative to the current directory.如果设置为 ' true ',' git diff ' 不会显示目录外的更改并显示相对于当前目录的路径名。


Warning: Before Git 2.34 (Q4 2021), " git diff --relative " ( man ) segfaulted and/or produced incorrect result when there are unmerged paths.警告:在 Git 2.34(2021 年第四季度)之前,当存在未合并的路径时,“ git diff --relativeman )会出现段错误和/或产生错误的结果。

See commit 8174627 (22 Aug 2021) by Đoàn Trần Công Danh ( sgn ) .请参阅Đoàn Trần Công Danh ( sgn ) 的提交 8174627 (2021 年 8 月 22 日
(Merged by Junio C Hamano -- gitster -- in commit c8f4916 , 08 Sep 2021) (由Junio C gitster合并gitster提交 c8f4916 中,2021 年 9 月 8 日)

diff-lib : ignore paths that are outside $cwd if --relative asked diff-lib :如果 --relative 询问,则忽略 $cwd 之外的路径

Reported-by: Thomas De Zeeuw报道者:Thomas De Zeeuw
Tested-by: Carlo Arenas测试者:Carlo Arenas
Signed-off-by: Đoàn Trần Công Danh签字人:Đoàn Trần Công Danh

For diff family commands, we can tell them to exclude changes outside of some directories if --relative is requested.对于 diff 系列命令,如果请求--relative ,我们可以告诉它们排除某些目录之外的更改。

In diff_unmerge() , NULL will be returned if the requested path is outside of the interesting directories, thus we'll run into NULL pointer dereference in run_diff_files when trying to dereference its return value.diff_unmerge() ,如果请求的路径在感兴趣的目录之外,则将返回NULL ,因此在尝试取消引用其返回值时,我们将在run_diff_files遇到NULL指针取消引用。

Checking for return value of diff_unmerge before dereferencing is not sufficient, though. diff_unmerge在取消引用之前检查diff_unmerge返回值是不够的。
Since, diff engine will try to work on such pathspec later.因为,差异引擎稍后将尝试处理此类路径规范。

Let's not run diff on those unintesting entries, instead.让我们不要对那些不感兴趣的条目运行 diff,而是。
As a side effect, by skipping like that, we can save some CPU cycles.作为副作用,通过这样跳过,我们可以节省一些 CPU 周期。

我要找的是这个:

git diff <ref1>..<ref2> <dirname>

Add Beyond Compare as your difftool in Git and add an alias for diffdir as:在 Git 中添加Beyond Compare作为 difftool 并为 diffdir 添加别名:

git config --global alias.diffdir = "difftool --dir-diff --tool=bc3 --no-prompt"

Get the gitdiff as:获取 gitdiff 为:

git diffdir 4bc7ba80edf6  7f566710c7

Reference: Compare entire directories w git difftool + Beyond Compare参考:使用git difftool + Beyond Compare 比较整个目录

You should make a habit of looking at the documentation for stuff like this.您应该养成查看此类文档的习惯。 It's very useful and will improve your skills very quickly.这是非常有用的,会很快提高你的技能。 Here's the relevant bit when you do git help diff当您执行git help diff时,这是相关的部分

   git diff [options] [--no-index] [--] <path> <path>

The two <path> s are what you need to change to the directories in question.两个<path>是您需要更改为相关目录的内容。

To use Beyond Compare as the difftool for directory diff, remember enable follow symbolic links like so:要将Beyond Compare用作目录差异的差异工具,请记住启用如下符号链接:

In a Folder Compare ViewRules (Referee Icon):文件夹比较视图规则(裁判图标):

在此处输入图片说明

And then, enable follow symbolic links and update session defaults:然后,启用跟随符号链接并更新会话默认值:

在此处输入图片说明


OR,或者,

set up the alias like so:像这样设置别名:

git config --global alias.diffdir "difftool --dir-diff --tool=bc3 --no-prompt --no-symlinks"

Note that in either case, any edits made to the side (left or right) that refers to the current working tree are preserved.请注意,在任何一种情况下,对引用当前工作树的一侧(左侧或右侧)所做的任何编辑都将保留。

如果要排除子目录,可以使用

git diff <ref1>..<ref2> -- $(git diff <ref1>..<ref2> --name-only | grep -v /)

如果您安装并配置了 diff 工具(例如: kdiff3

git difftool -d  branch1 branch2 /path/to/dir/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM