如何在 Git 中将一个文件与任意版本进行比较?
如何将文件(例如 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
您可以执行以下操作:
...将当前的
pom.xml
与通过第一个父级的 20 个修订版之前的master
进行比较。当然,您可以将master~20
替换为提交的对象名称 (SHA1sum) 或任何 许多其他指定修订版的方法。请注意,这实际上是将旧的
pom.xml
与工作树中的版本进行比较,而不是在master
中提交的版本。如果您想要这样,那么您可以执行以下操作:You can do:
... to compare your current
pom.xml
to the one frommaster
20 revisions ago through the first parent. You can replacemaster~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 inmaster
. If you want that, then you can do the following instead:例如:
For example:
如果你想查看单个文件的最后一次提交之间的差异,你可以这样做:
这会给你 git 中文件的差异,而不是比较你的本地文件。
If you want to see the difference between the last commit of a single file you can do:
This will give you the diff of the file in git, is not comparing your local file.
要查看上次提交中文件中的更改内容:
您可以将数字 (~1) 更改为要进行比较的第 n 个提交。
To see what was changed in a file in the last commit:
You can change the number (~1) to the n-th commit which you want to diff with.
-w 忽略空格
-w ignores whitespaces
通用语法:
适用于存储库中任何位置名为“FileName.xml”的所有文件。
请注意“--”和“**”之间的空格
回答您的问题:
与 git 一样,您可以使用 tag/sha1/"HEAD^" 来标识提交。
在 Ubuntu 上使用 git 1.9.1 进行了测试。
Generic Syntax :
for all files named "FileName.xml" anywhere in your repo.
Notice the space between "--" and "**"
Answer for your question:
as always with git, you can use a tag/sha1/"HEAD^" to id a commit.
Tested with git 1.9.1 on Ubuntu.
如果两个提交都不是您的 HEAD,那么 bash 的大括号扩展 证明非常有用,特别是如果您的文件名很长,上面的示例:
将成为
使用 bash 进行大括号扩展。
If neither commit is your HEAD then bash's brace expansion proves really useful, especially if your filenames are long, the example above:
Would become
More on Brace expansion with bash.
为了将 5 次提交与当前提交进行比较,都在
master
上,只需简单地执行以下操作:您还可以参考提交哈希值,例如如果哈希值是
x110bd64
,你可以做这样的事情来看看差异:For comparing to 5 commit to the current one, both on
master
, just simply do: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:如果您不在主分支中也可以使用。
Works if you are not in master branch too.
如果您可以使用图形工具(或者更喜欢它),您可以:
在 gitk 中,您可以单击任何提交(以“选择”它),然后右键单击任何其他提交以选择“Diff this -> selected”或“在弹出菜单中选择“Diff selected -> this”,具体取决于您喜欢的顺序。
If you are fine using a graphical tool (or even prefer it) you can:
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.
对于有兴趣在 GitHub 上执行相同操作的人,请参阅比较不同时间的提交。
For people interested in doing the same from GitHub, see comparing commits across time.
例如,如果您需要对存储中的单个文件进行比较,您可以这样做
If you need to diff on a single file in a stash for example you can do
假设您想要
在工作区域(尚未提交或暂存的内容)和最新提交之间比较位于
firmware/src/
difftool 中的文件interrupt.c
:或
注意:要比较最后提交版本之前的 x 版本,请将“head”替换为 head~x。例如,将“head”替换为“head~2”,以在工作区域和最新提交之前的 2 个版本之间进行比较
两个特定提交版本之间的比较工具:
首先获取要比较的版本的 ID通过使用下一行
您将获得所有已提交版本的列表。选择两个并复制他们的 ID。
在
{}
内输入 id:或
注意:如果文件之间没有差异,则不会打开任何内容。你只会在 git bash 中得到一个空行
lets say you want to diff the file
interrupt.c
located infirmware/src/
difftool between working area (what yet to be committed or staged) and latest commit:
or
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
you will get a list of all your committed version. choose two and copy their id's.
enter the ids inside
{}
:or
Note: if there is no difference between the files, nothing will open. You will just get an empty line in the git bash
如果您正在查找特定提交的差异,并且想要使用 github UI 而不是命令行(假设您想将其链接到其他人),您可以这样做:
例如:
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:
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.