如何区分同一分支上两个不同提交之间的同一文件?

发布于 2024-09-12 05:02:56 字数 356 浏览 10 评论 0原文

在 Git 中,如何比较同一分支(例如 master)上两个不同提交(不连续)之间的同一文件?

我正在寻找一种比较功能,例如 Visual SourceSafe (VSS) 或 Team Foundation Server (TFS)。
在 Git 中可以吗?

In Git, how could I compare the same file between two different commits (not contiguous) on the same branch (master for example)?

I'm searching for a compare feature like the one in Visual SourceSafe (VSS) or Team Foundation Server (TFS).
Is it possible in Git?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

缘字诀 2024-09-19 05:02:56

来自 git-diff 手册页:

git diff [--options] <commit> <commit> [--] [<path>...]

例如,要查看文件“main.c”现在和两次提交之间的差异,这里有三个等效命令:

$ git diff HEAD^^ HEAD main.c
$ git diff HEAD^^..HEAD -- main.c
$ git diff HEAD~2 HEAD -- main.c

From the git-diff manpage:

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:

$ git diff HEAD^^ HEAD main.c
$ git diff HEAD^^..HEAD -- main.c
$ git diff HEAD~2 HEAD -- main.c
递刀给你 2024-09-19 05:02:56

您还可以比较两个不同版本中的两个不同文件,如下所示:

git diff <revision_1>:<file_1>> <revision_2>:<file_2>

You can also compare two different files in two different revisions, like this:

git diff <revision_1>:<file_1> <revision_2>:<file_2>

旧情别恋 2024-09-19 05:02:56

如果您已经配置了“difftool”,则可以使用

git difftool revision_1:file_1 revision_2:file_2

示例:将文件从上次提交到同一分支上的先前提交进行比较:
假设如果您位于项目根文件夹中,

$git difftool HEAD:src/main/java/com.xyz.test/MyApp.java HEAD^:src/main/java/com.xyz.test/MyApp.java

您的 ~/.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\"

注意:如果您使用 Intellij Enterprise 或 Community Edition - 它有一个很好的工具,可以在进行合并/变基时进行 3 路合并

对于简单的 diff,您可以右键单击 -> Git -> 与修订版本比较
输入图像描述此处

选择您感兴趣的版本

在此处输入图像描述

Intellij 将显示差异。

输入图像描述这里

If you have configured the "difftool" you can use

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.

[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\"

Note: If you are using Intellij Enterprise or Community Edition - It has a good tool for doing 3 way merge when doing a merging/rebasing

For simple diff you can right click->Git->Compare with revision
enter image description here

Select the revision you are interested in

enter image description here

Intellij will show the diff.

enter image description here

物价感观 2024-09-19 05:02:56

检查$ git log,复制SHA-1 ID两个不同的提交,并使用这些 ID 运行 git diff 命令。例如:

$ git diff (sha-id-one) (sha-id-two)

Check $ git log, copy the SHA-1 ID of the two different commits, and run the git diff command with those IDs. for example:

$ git diff (sha-id-one) (sha-id-two)
单身情人 2024-09-19 05:02:56

如果您想逐个提交地查看两次提交之间文件的所有更改,您还可以执行

git log -u $start_commit..$end_commit -- path/to/file

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

尤怨 2024-09-19 05:02:56

下面是一个 Perl 脚本,它打印出 Git log 命令中给定文件的 Git diff 命令。

例如

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

然后可以将其剪切并粘贴到 shell 窗口会话中或通过管道传输到 /bin/sh

注意:

  1. 数字(本例中为 3)指定打印
  2. 文件(本例中为 pom.xml)的行数必须在两个位置都一致(您可以将其包装在 shell 函数中以在两个位置提供相同的文件)或将其作为shell脚本放在二进制目录中

代码:

# 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 = $1;
    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;
}

Here is a Perl script that prints out Git diff commands for a given file as found in a Git log command.

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.

Notes:

  1. the number (3 in this case) specifies how many lines to print
  2. 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

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 = $1;
    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;
}
单调的奢华 2024-09-19 05:02:56

如果您有多个文件或目录并且想要比较非连续提交,您可以这样做:

创建一个临时分支(本例中为“修订版”)

git checkout -b revision

回滚到第一个提交目标

git reset --hard <commit_target>

在这些提交上精挑细选有兴趣

git cherry-pick <commit_interested> ...

应用 diff

git diff <commit-target>^

完成后

git branch -D revision

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
行至春深 2024-09-19 05:02:56

如果你想对多个文件进行 diff,可以使用 @mipadi 指定的方法:

例如 HEAD 和你的 master 之间的 diff,找到所有 。 Coffee 文件:

git diff master..HEAD -- `find your_search_folder/ -name '*.coffee'`

这将递归搜索您的 your_search_folder/ 中的所有 .coffee 文件,并在它们与其 master 版本之间进行比较。

If you want to make a diff with more than one file, with the method specified by @mipadi:

E.g. diff between HEAD and your master, to find all .coffee files:

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.

恋你朝朝暮暮 2024-09-19 05:02:56

这只是使用 Git 强大功能的另一种方式......

git difftool HEAD HEAD@{N} /PATH/FILE.ext

Just another way to use Git's awesomeness...

git difftool HEAD HEAD@{N} /PATH/FILE.ext
几味少女 2024-09-19 05:02:56

所有其他回复都更完整,因此请投票。
这只是为了记住你可以避免知道最近提交的 id。通常,我将自己设置在要比较的分支中,并运行知道旧提交 uid 的 diff 工具(您可以使用其他符号):

git checkout master
git difftool 6f8bba my/file/relative/path.py

此外,请在此处检查其他响应以设置您希望 git 打开来比较文件的工具:
使用 .gitconfig 配置 diff 工具
要了解有关 difftool 的更多信息,转到 difftool 文档

All the other responses are more complete, so upvote them.
This one is just to remember that you can avoid knowing the id of the recent commit. Usually, I set my self in the branch that I want to compare and run diff tools knowing the old commit uid (You can use other notations):

git checkout master
git difftool 6f8bba my/file/relative/path.py

Also, check this other response here to set the tool you want git open to compare the file:
Configuring diff tool with .gitconfig
And to learn more about difftool, go to the difftool doc

薄凉少年不暖心 2024-09-19 05:02:56

如果您想在 Windows 上进行简单的视觉比较,例如您可以访问 Visual SourceSafeTeam Foundation Server (TFS),尝试以下操作:

  • 右键单击文件资源管理器中的文件,
  • 选择“Git”历史记录'

注意:升级到 Windows 10 后,我丢失了 Git 上下文菜单选项。但是,您可以在命令窗口中使用“gitk”或“gitk filename”来实现相同的效果。

一旦您调用“Git History”,Git GUI工具将启动,并在顶部显示文件的历史记录左窗格。选择您想要比较的版本之一。然后右键单击第二个版本并选择

Diff this ->选择

Diff 选择 ->这种

颜色编码的差异将显示在左下窗格中。

If you want a simple visual comparison on Windows such as you can get in Visual SourceSafe or Team Foundation Server (TFS), try this:

  • right-click on the file in File Explorer
  • select 'Git History'

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.

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

Diff this -> selected

or

Diff selected -> this

Colour-coded differences will appear in the lower left-hand pane.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文