git diff 显示不够

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5256249/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 05:10:46  来源:igfitidea点击:

git diff doesn't show enough

gitgithubgit-diff

提问by codr

I want to see the difference between the master branch and my feature branch. I have many pulls from the master to my feature branch and want to see the changes that would be added if I merged my feature into the master.

我想看看 master 分支和我的 feature 分支之间的区别。我有很多从 master 到我的 feature 分支的 pull,并且希望看到如果我将我的 feature 合并到 master 中将会添加的更改。

This is my situation:

这是我的情况:

-*--*--*-----*<master>
  \     \     \
   1--*--*--*--2--*<feature>

My problem is the git diff master featureseems to only display commit number 2. I want to see the diff that a github pull request would show, which I believe is all the way to commit 1. I noticed git cherryshows me the commits I want to see the difference for.

我的问题是git diff master feature似乎只显示提交编号 2。我想查看 github 拉取请求将显示的差异,我相信这是提交 1 的所有方式。我注意到git cherry向我展示了我想要查看差异的提交.

Thanks for any advice.

感谢您的任何建议。

回答by Mark Longair

The important thing to realize about git diff A Bis that it only ever shows you the difference between the states of the tree between exactly two points in the commit graph - it doesn't care about the history. The ..and ...notations used for git diffhave the following meanings:

要意识到的重要一点git diff A B是,它只向您显示提交图中恰好两个点之间的树状态之间的差异 - 它不关心历史。用于的.....符号git diff具有以下含义:

An illustration of the different ways of specifying commits for git diff

为 git diff 指定提交的不同方式的说明

So when you run git diff master featurethat's not just showing you the change introduced by the commit you've marked as 2- the output should show the exact differences between the state of the tree commited in masterand the state of the tree commited in feature. If it's not showing you the earlier changes on your feature branch, perhaps you resolved conflicts from the earlier merges from master in favour of the version in master?

所以,当你运行git diff master feature这不只是显示你在提交已标记为引入的变化2-输出应该显示在COMMITED树的状态之间的确切差异master和COMMITED树的状态feature。如果它没有向您显示您的功能分支上的早期更改,也许您解决了来自 master 的早期合并的冲突,而支持master?

As cebewee saysit may be that what you want is git log -p master..feature, since git logdoescare about history. The meaning of ..and ...for git logare different since they select a range of commits:

正如cebewee 所说,这可能是你想要的git log -p master..feature,因为git log确实关心历史。.....for的含义git log不同,因为它们选择了一系列提交:

An illustration of the different ways of specifying ranges of commits for git log

为 git log 指定提交范围的不同方法的说明

Incidentally, its often said that merging from masterinto a topic branch is the wrong thing to do - instead you should be rebasing, or merging your topic branch into masterafter it is complete. This keeps the meaning of the topic branch easily understood. The git maintainer did a (somewhat difficult to understand) blog post about the philosophy of mergingwhich discusses that.

顺便说一句,人们常说从master主题分支合并是错误的做法 - 相反,您应该重新定位,或在主题分支master完成后将其合并。这使主题分支的含义易于理解。git 维护者做了一篇(有点难以理解)关于合并哲学的博客文章其中讨论了这一点。

回答by Lars Noschinski

git diff master featuredoes not show any of the commits' but the textual difference between the commits master and feature. It sounds as if you want to see all commits from feature, which are not yet in master? In this case, try git log master..featureor git log -p master..feature, if you want to see the diffs, too.

git diff master feature不显示任何提交,而是提交主文件和功能之间的文本差异。听起来好像您想查看尚未掌握的功能的所有提交?在这种情况下,如果您也想查看差异,请尝试git log master..featuregit log -p master..feature

See the section SPECIFIYING RANGES in man git-rev-parsefor an explanation of the 'a..b' syntax.

有关man git-rev-parse“a..b”语法的解释,请参阅 SPECIFIYING RANGES 部分。

回答by Honey

I'm relatively new to git, but if I understand your question correctly. Your question is rooted from notproperly understanding remote and local repositories and how they relate to one another. I remember once I understood it, everything became 2X easier.

我对 git 比较陌生,但如果我正确理解你的问题。您的问题源于没有正确理解远程和本地存储库以及它们之间的关系。我记得一旦我理解了它,一切都变得简单了 2 倍。

You thinkyou are at this situation below where you have only 2 branches:

认为您处于以下只有 2 个分支的情况:

  • featureBranch
  • masterBranch
  • 功能分支
  • 主分支

Yet if you do a git branch -ayou will be able to see allof your branches, local and remote.

然而,如果你这样做,git branch -a你将能够看到你所有的分支,本地和远程

So your realsituation is:

所以你的真实情况是:

  • featureBranch
  • masterBranch
  • remoteBranch(s)
  • 功能分支
  • 主分支
  • 远程分支
  *---* <remote's Master> which is behind your local
      \
      -*--*--*-----*<master> which is ahead of your remote/origin
       \     \     \
        1--*--*--*--2--*<feature>
  *---* <remote's Master> which is behind your local
      \
      -*--*--*-----*<master> which is ahead of your remote/origin
       \     \     \
        1--*--*--*--2--*<feature>

In order for you to be able to see the diff similar to what you see in Github Pull Requestyou must diffit with your remotebranch.

为了让您能够看到与您看到的相似的差异,Github Pull Request您必须diff使用远程分支。

git diff <local branch> <remote>/<remote branch>

See compare local git branch with remote branch?

请参阅将本地 git 分支与远程分支进行比较?