git 如何在同一分支上的两个不同提交之间区分同一文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3338126/
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
How do I diff the same file between two different commits on the same branch?
提问by systempuntoout
In Git, how could I compare the same file between two different commits (not contiguous) on the same branch (master for example)?
在 Git 中,如何在同一分支(例如 master)上的两个不同提交(不连续)之间比较同一个文件?
I'm searching for a comparefeature like the one in Visual SourceSafe(VSS) or Team Foundation Server(TFS).
Is it possible in Git?
我正在寻找一种比较功能,例如Visual SourceSafe(VSS) 或Team Foundation Server(TFS) 中的功能。
在 Git 中可能吗?
回答by mipadi
From the git-diff
manpage:
从git-diff
联机帮助页:
git diff [--options] <commit> <commit> [--] [<path>...]
For instance, to see the difference for a file "main.c" between now and two commits back, here are three equivalent commands:
例如,要查看文件“main.c”现在和两次提交之间的差异,以下是三个等效命令:
$ git diff HEAD^^ HEAD main.c
$ git diff HEAD^^..HEAD -- main.c
$ git diff HEAD~2 HEAD -- main.c
回答by Jakub Nar?bski
You can also compare two different files in two different revisions, like this:
您还可以比较两个不同版本中的两个不同文件,如下所示:
git diff <revision_1>:<file_1> <revision_2>:<file_2>
git diff <revision_1>:<file_1> <revision_2>:<file_2>
回答by Anver Sadhat
If you have configured the "difftool" you can use
如果您已经配置了“difftool”,您可以使用
git difftool revision_1:file_1 revision_2:file_2
Example: Comparing a file from its last commit to its previous commit on the same branch: Assuming that if you are in your project root folder
示例:将文件从上次提交与其在同一分支上的上一次提交进行比较:假设您在项目根文件夹中
$git difftool HEAD:src/main/java/com.xyz.test/MyApp.java HEAD^:src/main/java/com.xyz.test/MyApp.java
You should have the following entries in your ~/.gitconfig or in project/.git/config file. Install the p4merge [This is my preferred diff and merge tool]
您应该在 ~/.gitconfig 或 project/.git/config 文件中包含以下条目。安装 p4merge [这是我首选的差异和合并工具]
[merge]
tool = p4merge
keepBackup = false
[diff]
tool = p4merge
keepBackup = false
[difftool "p4merge"]
path = C:/Program Files (x86)/Perforce/p4merge.exe
[mergetool]
keepBackup = false
[difftool]
keepBackup = false
[mergetool "p4merge"]
path = C:/Program Files (x86)/Perforce/p4merge.exe
cmd = p4merge.exe \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"
回答by Vibhuti
回答by cxreg
If you want to see all changes to the file between the two commits on a commit-by-commit basis, you can also do
如果您想逐个提交地查看两次提交之间对文件的所有更改,您也可以这样做
git log -u $start_commit..$end_commit -- path/to/file
git log -u $start_commit..$end_commit -- path/to/file
回答by user2520657
Here is a Perl script that prints out Git diff commands for a given file as found in a Git log command.
这是一个 Perl 脚本,用于打印在 Git 日志命令中找到的给定文件的 Git diff 命令。
E.g.
例如
git log pom.xml | perl gldiff.pl 3 pom.xml
Yields:
产量:
git diff 5cc287:pom.xml e8e420:pom.xml
git diff 3aa914:pom.xml 7476e1:pom.xml
git diff 422bfd:pom.xml f92ad8:pom.xml
which could then be cut and pasted in a shell window session or piped to /bin/sh
.
然后可以将其剪切并粘贴到 shell 窗口会话中或通过管道传输到/bin/sh
.
Notes:
笔记:
- the number (3 in this case) specifies how many lines to print
- the file (pom.xml in this case) must agree in both places (you could wrap it in a shell function to provide the same file in both places) or put it in a binary directory as a shell script
- 数字(在本例中为 3)指定要打印的行数
- 该文件(在这种情况下为 pom.xml)必须在两个地方都一致(您可以将它包装在一个 shell 函数中以在两个地方提供相同的文件)或将它作为 shell 脚本放在一个二进制目录中
Code:
代码:
# gldiff.pl
use strict;
my $max = shift;
my $file = shift;
die "not a number" unless $max =~ m/\d+/;
die "not a file" unless -f $file;
my $count;
my @lines;
while (<>) {
chomp;
next unless s/^commit\s+(.*)//;
my $commit = ;
push @lines, sprintf "%s:%s", substr($commit,0,6),$file;
if (@lines == 2) {
printf "git diff %s %s\n", @lines;
@lines = ();
}
last if ++$count >= $max *2;
}
回答by Andrei-Niculae Petre
If you want to make a diff with more than one file, with the method specified by @mipadi:
如果要对多个文件进行比较,请使用@mipadi 指定的方法:
E.g. diff between HEAD
and your master
, to find all .coffee
files:
例如 diffHEAD
和 your master
, 以查找所有.coffee
文件:
git diff master..HEAD -- `find your_search_folder/ -name '*.coffee'`
This will recursively search your your_search_folder/
for all .coffee
files and make a diff between them and their master
versions.
这将递归搜索您your_search_folder/
的所有.coffee
文件并在它们及其master
版本之间进行区分。
回答by dvdvck
If you have several files or directories and want to compare non continuous commits, you could do this:
如果您有多个文件或目录并且想要比较非连续提交,您可以这样做:
Make a temporary branch ("revision"in this example)
创建一个临时分支(本例中的“修订版”)
git checkout -b revision
Rewind to the first commit target
回退到第一个提交目标
git reset --hard <commit_target>
Cherry picking on those commit interested
樱桃采摘那些感兴趣的提交
git cherry-pick <commit_interested> ...
Apply diff
应用差异
git diff <commit-target>^
When you done
当你完成
git branch -D revision
回答by Eddie B
Just another way to use Git's awesomeness...
使用 Git 的另一种方式......
git difftool HEAD HEAD@{N} /PATH/FILE.ext
回答by Resource
If you want a simple visual comparison on Windows such as you can get in Visual SourceSafeor Team Foundation Server(TFS), try this:
如果您想在 Windows 上进行简单的视觉比较,例如可以在Visual SourceSafe或Team Foundation Server(TFS) 中获得,请尝试以下操作:
- right-click on the file in File Explorer
- select 'Git History'
- 右键单击文件资源管理器中的文件
- 选择“Git 历史”
Note: After upgrading to Windows 10 I have lost the Git context menu options. However, you can achieve the same thing using 'gitk' or 'gitk filename' in a command window.
注意:升级到 Windows 10 后,我丢失了 Git 上下文菜单选项。但是,您可以在命令窗口中使用 'gitk' 或 'gitk filename' 来实现相同的目的。
Once you call 'Git History', the Git GUI tool will start, with a history of the file in the top left pane. Select one of the versions you would like to compare. Then right-click on the second version and choose either
一旦您调用“Git History”,Git GUI 工具将启动,并在左上角的窗格中显示文件的历史记录。选择您要比较的版本之一。然后右键单击第二个版本并选择
Diff this -> selected
区分此 -> 选择
or
或者
Diff selected -> this
差异选择 -> 这个
Colour-coded differences will appear in the lower left-hand pane.
左下窗格中将出现颜色编码差异。