如何在 Git 中将一个文件与任意版本进行比较?

发布于 2024-10-31 05:14:22 字数 66 浏览 6 评论 0原文

如何将文件(例如 pom.xml)从 master 分支与 Git 中的任意旧版本进行比较?

How can I diff a file, say pom.xml, from the master branch to an arbitrary older version in Git?

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

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

发布评论

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

评论(14

似狗非友 2024-11-07 05:14:22

您可以执行以下操作:

git diff master~20:pom.xml pom.xml

...将当前的 pom.xml 与通过第一个父级的 20 个修订版之前的 master 进行比较。当然,您可以将 master~20 替换为提交的对象名称 (SHA1sum) 或任何 许多其他指定修订版的方法

请注意,这实际上是将旧的 pom.xml 与工作树中的版本进行比较,而不是在 master 中提交的版本。如果您想要这样,那么您可以执行以下操作:

git diff master~20:pom.xml master:pom.xml

You can do:

git diff master~20:pom.xml pom.xml

... to compare your current pom.xml to the one from master 20 revisions ago through the first parent. You can replace master~20, of course, with the object name (SHA1sum) of a commit or any of the many other ways of specifying a revision.

Note that this is actually comparing the old pom.xml to the version in your working tree, not the version committed in master. If you want that, then you can do the following instead:

git diff master~20:pom.xml master:pom.xml
誰認得朕 2024-11-07 05:14:22
git diff <revision> <path>

例如:

git diff b0d14a4 foobar.txt
git diff <revision> <path>

For example:

git diff b0d14a4 foobar.txt
魔法唧唧 2024-11-07 05:14:22

如果你想查看单个文件的最后一次提交之间的差异,你可以这样做:

git log -p -1 filename

这会给你 git 中文件的差异,而不是比较你的本地文件。

If you want to see the difference between the last commit of a single file you can do:

git log -p -1 filename

This will give you the diff of the file in git, is not comparing your local file.

请恋爱 2024-11-07 05:14:22

要查看上次提交中文件中的更改内容:

git diff HEAD~1 -- path/to/file

您可以将数字 (~1) 更改为要进行比较的第 n 个提交。

To see what was changed in a file in the last commit:

git diff HEAD~1 -- path/to/file

You can change the number (~1) to the n-th commit which you want to diff with.

放低过去 2024-11-07 05:14:22
git diff -w HEAD origin/master path/to/file

-w 忽略空格

git diff -w HEAD origin/master path/to/file

-w ignores whitespaces

陈独秀 2024-11-07 05:14:22

通用语法:

$git diff oldCommit..newCommit -- **FileName.xml > ~/diff.txt

适用于存储库中任何位置名为“FileName.xml”的所有文件。

请注意“--”和“**”之间的空格

回答您的问题:

$git checkout master
$git diff oldCommit..HEAD -- **pom.xml 
or
$git diff oldCommit..HEAD -- relative/path/to/pom.xml 

与 git 一样,您可以使用 tag/sha1/"HEAD^" 来标识提交。

在 Ubuntu 上使用 git 1.9.1 进行了测试。

Generic Syntax :

$git diff oldCommit..newCommit -- **FileName.xml > ~/diff.txt

for all files named "FileName.xml" anywhere in your repo.

Notice the space between "--" and "**"

Answer for your question:

$git checkout master
$git diff oldCommit..HEAD -- **pom.xml 
or
$git diff oldCommit..HEAD -- relative/path/to/pom.xml 

as always with git, you can use a tag/sha1/"HEAD^" to id a commit.

Tested with git 1.9.1 on Ubuntu.

最偏执的依靠 2024-11-07 05:14:22

如果两个提交都不是您的 HEAD,那么 bash 的大括号扩展 证明非常有用,特别是如果您的文件名很长,上面的示例:

git diff master~20:pom.xml master:pom.xml

将成为

git diff {master~20,master}:pom.xml

使用 bash 进行大括号扩展

If neither commit is your HEAD then bash's brace expansion proves really useful, especially if your filenames are long, the example above:

git diff master~20:pom.xml master:pom.xml

Would become

git diff {master~20,master}:pom.xml

More on Brace expansion with bash.

别念他 2024-11-07 05:14:22

为了将 5 次提交与当前提交进行比较,都在 master 上,只需简单地执行以下操作:

git diff master~5:pom.xml master:pom.xml

您还可以参考提交哈希值,例如如果哈希值是 x110bd64,你可以做这样的事情来看看差异:

git diff x110bd64 pom.xml

For comparing to 5 commit to the current one, both on master, just simply do:

git diff master~5:pom.xml master:pom.xml

Also you can refer to commit hash number, for example if the hash number is x110bd64, you can do something like this to see the difference:

git diff x110bd64 pom.xml
忆梦 2024-11-07 05:14:22
git diff master~20 -- pom.xml

如果您不在主分支中也可以使用。

git diff master~20 -- pom.xml

Works if you are not in master branch too.

且行且努力 2024-11-07 05:14:22

如果您可以使用图形工具(或者更喜欢它),您可以:

gitk pom.xml

在 gitk 中,您可以单击任何提交(以“选择”它),然后右键单击任何其他提交以选择“Diff this -> selected”或“在弹出菜单中选择“Diff selected -> this”,具体取决于您喜欢的顺序。

If you are fine using a graphical tool (or even prefer it) you can:

gitk pom.xml

In gitk you can then click any commit (to "select" it) and right click any other commit to select "Diff this -> selected" or "Diff selected -> this" in the popup menu, depending on what order you prefer.

梦里°也失望 2024-11-07 05:14:22

对于有兴趣在 GitHub 上执行相同操作的人,请参阅比较不同时间的提交

For people interested in doing the same from GitHub, see comparing commits across time.

治碍 2024-11-07 05:14:22

例如,如果您需要对存储中的单个文件进行比较,您可以这样做

git diff stash@{0} -- path/to/file

If you need to diff on a single file in a stash for example you can do

git diff stash@{0} -- path/to/file
水染的天色ゝ 2024-11-07 05:14:22

假设您想要


在工作区域(尚未提交或暂存的内容)和最新提交之间比较位于 firmware/src/ difftool 中的文件 interrupt.c

git difftool头固件/src/interrupt.c

git difftool 头 *interrupt.c

注意:要比较最后提交版本之前的 x 版本,请将“head”替换为 head~x。例如,将“head”替换为“head~2”,以在工作区域和最新提交之前的 2 个版本之间进行比较


两个特定提交版本之间的比较工具:

首先获取要比较的版本的 ID通过使用下一行

git log --oneline

您将获得所有已提交版本的列表。选择两个并复制他们的 ID。

输入图片此处描述

{} 内输入 id:

git difftool {2fae9e6,a21dd00} 固件/src/interrupt.c

git difftool {2fae9e6,a21dd00} *interrupt.c

注意:如果文件之间没有差异,则不会打开任何内容。你只会在 git bash 中得到一个空行

lets say you want to diff the file interrupt.c located in firmware/src/


difftool between working area (what yet to be committed or staged) and latest commit:

git difftool head firmware/src/interrupt.c

or

git difftool head *interrupt.c

Note: to diff x versions before the last committed version replace "head" with head~x. For example replace "head" with "head~2" to diff between working area and 2 versions prior to the latest commit


difftool between two specific committed versions:

first get the ids for the versions you want to compare by using next line

git log --oneline

you will get a list of all your committed version. choose two and copy their id's.

enter image description here

enter the ids inside {}:

git difftool {2fae9e6,a21dd00} firmware/src/interrupt.c

or

git difftool {2fae9e6,a21dd00} *interrupt.c

Note: if there is no difference between the files, nothing will open. You will just get an empty line in the git bash

何止钟意 2024-11-07 05:14:22

如果您正在查找特定提交的差异,并且想要使用 github UI 而不是命令行(假设您想将其链接到其他人),您可以这样做:

https://github.com/<org>/<repo>/commit/<commit-sha>/<path-to-file>

例如:

https://github.com/grails/grails-core/commit /02942c5b4d832b856fbc04c186f1d02416895a7e/grails-test-suite-uber/build.gradle

请注意右上角的上一个和下一个链接,它们允许您浏览提交中的所有文件。

但这仅适用于特定提交,不适用于在任意两个任意版本之间进行比较。

If you are looking for the diff on a specific commit and you want to use the github UI instead of the command line (say you want to link it to other folks), you can do:

https://github.com/<org>/<repo>/commit/<commit-sha>/<path-to-file>

For example:

https://github.com/grails/grails-core/commit/02942c5b4d832b856fbc04c186f1d02416895a7e/grails-test-suite-uber/build.gradle

Note the Previous and Next links at the top right that allow you to navigate through all the files in the commit.

This only works for a specific commit though, not for comparing between any two arbitrary versions.

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