如何在 Git 中将一个文件区分为任意版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5586383/
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 to diff one file to an arbitrary version in Git?
提问by Chris K
How can I diff a file, say pom.xml
, from the master branch to an arbitrary older version in Git?
我如何将文件pom.xml
从 master 分支区分为 Git 中的任意旧版本?
回答by Mark Longair
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.
...通过第一个父级将您的当前版本pom.xml
与master
20 个修订版之前的版本进行比较。master~20
当然,您可以将 替换为提交的对象名称 (SHA1sum) 或指定修订的许多其他方法中的任何一种。
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:
请注意,这实际上是将旧pom.xml
版本与工作树中的版本进行比较,而不是在master
. 如果需要,则可以执行以下操作:
git diff master~20:pom.xml master:pom.xml
回答by Benjamin Pollack
git diff <revision> <path>
For example:
例如:
git diff b0d14a4 foobar.txt
回答by Gerardo
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.
这将为您提供 git 中文件的差异,而不是比较您的本地文件。
回答by Juampy NR
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.
您可以将数字 (~1) 更改为要与之进行比较的第 n 个提交。
回答by Juampy NR
Generic Syntax :
通用语法:
$git diff oldCommit..newCommit -- **FileName.xml > ~/diff.txt
for all files named "FileName.xml" anywhere in your repo.
存储库中任何位置名为“FileName.xml”的所有文件。
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.
与 git 一样,您可以使用 tag/sha1/"HEAD^" 来标识提交。
Tested with git 1.9.1 on Ubuntu.
在 Ubuntu 上用 git 1.9.1 测试。
回答by robstarbuck
If neither commit is your HEAD then bash's brace expansionproves really useful, especially if your filenames are long, the example above:
如果这两个提交都不是你的 HEAD,那么 bash 的大括号扩展证明真的很有用,尤其是如果你的文件名很长,上面的例子:
git diff master~20:pom.xml master:pom.xml
Would become
会成为
git diff {master~20,master}:pom.xml
More on Brace expansionwith bash.
更多关于使用 bash进行大括号扩展的信息。
回答by Alireza
For comparing to 5 commit to the current one, both on master
, just simply do:
为了与当前的 5 次提交进行比较,两者都在master
,只需执行以下操作:
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:
您也可以参考提交散列号,例如,如果散列号是x110bd64
,您可以执行以下操作以查看差异:
git diff x110bd64 pom.xml
回答by CatalinBerta
git diff -w HEAD origin/master path/to/file
git diff -w HEAD 原点/主路径/到/文件
回答by Martin G
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.
在 gitk 中,您可以单击任何提交(以“选择”它)并右键单击任何其他提交以在弹出菜单中选择“Diff this -> selected”或“Diff selected -> this”,具体取决于您喜欢的顺序。
回答by Gunar Gessner
git diff master~20 -- pom.xml
Works if you are not in master branch too.
如果您不在 master 分支中也可以工作。