如何区分同一分支上两个不同提交之间的同一文件?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
来自
git-diff
手册页:例如,要查看文件“main.c”现在和两次提交之间的差异,这里有三个等效命令:
From the
git-diff
manpage:For instance, to see the difference for a file "main.c" between now and two commits back, here are three equivalent commands:
您还可以比较两个不同版本中的两个不同文件,如下所示:
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>
如果您已经配置了“difftool”,则可以使用
示例:将文件从上次提交到同一分支上的先前提交进行比较:
假设如果您位于项目根文件夹中,
您的 ~/.gitconfig 或 project/.git/config 文件中应该有以下条目。安装 p4merge。
注意:如果您使用 Intellij Enterprise 或 Community Edition - 它有一个很好的工具,可以在进行合并/变基时进行 3 路合并
对于简单的 diff,您可以右键单击 -> Git -> 与修订版本比较
选择您感兴趣的版本
Intellij 将显示差异。
If you have configured the "difftool" you can use
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
You should have the following entries in your ~/.gitconfig or in project/.git/config file. Install the p4merge.
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
Select the revision you are interested in
Intellij will show the diff.
检查
$ git log
,复制SHA-1 ID两个不同的提交,并使用这些 ID 运行 git diff 命令。例如:Check
$ git log
, copy the SHA-1 ID of the two different commits, and run thegit diff
command with those IDs. for example:如果您想逐个提交地查看两次提交之间文件的所有更改,您还可以执行
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
下面是一个 Perl 脚本,它打印出 Git log 命令中给定文件的 Git diff 命令。
例如
Yields:
然后可以将其剪切并粘贴到 shell 窗口会话中或通过管道传输到
/bin/sh
。注意:
代码:
Here is a Perl script that prints out Git diff commands for a given file as found in a Git log command.
E.g.
Yields:
which could then be cut and pasted in a shell window session or piped to
/bin/sh
.Notes:
Code:
如果您有多个文件或目录并且想要比较非连续提交,您可以这样做:
创建一个临时分支(本例中为“修订版”)
回滚到第一个提交目标
在这些提交上精挑细选有兴趣
应用 diff
完成后
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)
Rewind to the first commit target
Cherry picking on those commit interested
Apply diff
When you done
如果你想对多个文件进行 diff,可以使用 @mipadi 指定的方法:
例如
HEAD
和你的master
之间的 diff,找到所有。 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 yourmaster
, to find all.coffee
files:This will recursively search your
your_search_folder/
for all.coffee
files and make a diff between them and theirmaster
versions.这只是使用 Git 强大功能的另一种方式......
Just another way to use Git's awesomeness...
所有其他回复都更完整,因此请投票。
这只是为了记住你可以避免知道最近提交的 id。通常,我将自己设置在要比较的分支中,并运行知道旧提交 uid 的 diff 工具(您可以使用其他符号):
此外,请在此处检查其他响应以设置您希望 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):
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
如果您想在 Windows 上进行简单的视觉比较,例如您可以访问 Visual SourceSafe 或 Team Foundation Server (TFS),尝试以下操作:
注意:升级到 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:
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.