git:如何在拉取后将更改的文件与以前的版本进行比较?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2428270/
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
git: How to diff changed files versus previous versions after a pull?
提问by doug
When I run "git pull" I often want to know what changed between the last version of a file and the new one. Say I want to know what someone else committed to a particular file.
当我运行“git pull”时,我经常想知道文件的最后一个版本和新版本之间发生了什么变化。假设我想知道其他人对特定文件的承诺。
How is that done?
这是怎么做的?
I'm assuming it's "git diff" with some parameters for commit x versus commit y but I can't seem to get the syntax. I also find "git log" confusing a bit and am not sure where to get the commit ID of my latest version of the file versus the new one.
我假设它是带有一些参数的“git diff”,用于提交 x 与提交 y,但我似乎无法获得语法。我还发现“git log”有点令人困惑,并且不确定从哪里获取文件的最新版本与新版本的提交 ID。
回答by Cascabel
There are all kinds of wonderful ways to specify commits - see the specifying revisionssection of man git-rev-parse
for more details. In this case, you probably want:
有各种各样的好方法来指定提交 -有关更多详细信息,请参阅 的指定修订部分man git-rev-parse
。在这种情况下,您可能想要:
git diff HEAD@{1}
The @{1}
means "the previous position of the ref I've specified", so that evaluates to what you had checked out previously - just before the pull. You can tack HEAD
on the end there if you also have some changes in your work tree and you don't want to see the diffs for them.
的@{1}
意思是“我指定裁判的前面位置”,从而使计算结果为你以前签出什么-只是前拉。HEAD
如果您的工作树也有一些更改并且您不想看到它们的差异,则可以在那里添加。
I'm not sure what you're asking for with "the commit ID of my latest version of the file" - the commit "ID" (SHA1 hash) is that 40-character hex right at the top of every entry in the output of git log. It's the hash for the entire commit, not for a given file. You don't really ever need more - if you want to diff just one file across the pull, do
我不确定您对“我最新版本文件的提交 ID”要求什么 - 提交“ID”(SHA1 哈希)是输出中每个条目顶部的 40 个字符的十六进制git 日志。它是整个提交的哈希值,而不是给定文件的哈希值。你真的不需要更多 - 如果你只想在 pull 中区分一个文件,请执行
git diff HEAD@{1} filename
This is a general thing - if you want to know about the state of a file in a given commit, you specify the commit and the file, not an ID/hash specific to the file.
这是一般的事情 - 如果您想了解给定提交中文件的状态,请指定提交和文件,而不是特定于文件的 ID/哈希。
回答by cadizm
I like to use:
我喜欢使用:
git diff HEAD^
Or if I only want to diff a specific file:
或者,如果我只想比较特定文件:
git diff HEAD^ -- /foo/bar/baz.txt
回答by CB Bailey
If you do a straight git pull
then you will either be 'fast-forwarded' or merge an unknown number of commits from the remote repository. This happens as one action though, so the last commit that you were at immediately before the pull will be the last entry in the reflog and can be accessed as HEAD@{1}
. This means that you can do:
如果您直接这样做,git pull
那么您将被“快进”或合并来自远程存储库的未知数量的提交。但是,这是作为一个操作发生的,因此您在拉取之前的最后一次提交将是 reflog 中的最后一个条目,并且可以作为HEAD@{1}
. 这意味着您可以:
git diff HEAD@{1}
However, I would strongly recommend that if this is something you find yourself doing a lot then you should consider just doing a git fetch
and examining the fetched branch before manually merging or rebasing onto it. E.g. if you're on master and were going to pull in origin/master:
但是,我强烈建议,如果这是您发现自己经常做的事情,那么您应该考虑git fetch
在手动合并或重新绑定之前只做一个并检查获取的分支。例如,如果您在 master 上并且要拉入 origin/master:
git fetch
git log HEAD..origin/master
# looks good, lets merge
git merge origin/master