有没有一种从点或分支原点“git diff”的快速方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29810331/
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
Is there a quick way to "git diff" from the point or branch origin?
提问by user776686
I have looked at various SO answers on using git diff
and git revisions (HEAD, ORIG_HEAD, FETCH_HEAD, etc.) and I still haven't found an easy way to list the changes have been made since the beginning of the local branch, or since last rebase.
我查看了有关 usinggit diff
和 git 修订版(HEAD、ORIG_HEAD、FETCH_HEAD 等)的各种 SO 答案,但我仍然没有找到一种简单的方法来列出自本地分支开始以来或自上次以来所做的更改变基。
By easyI mean without having to look up and paste commit SHA or having to count how many commits I want to look back.
通过简单的我的意思是,无需查找并粘贴提交SHA也不必指望我想多少提交回头。
git diff origin/master
is close, but it refers to remote which may have diverged since I checked out new branch from it.
git diff origin/master
很接近,但它指的是自从我从中检出新分支以来可能已经发生分歧的远程。
I would expect something like git diff BASE_HEAD
to be available.
我希望有类似的东西git diff BASE_HEAD
可用。
...unless there's already a way to do that. Does anyone have the answer?
...除非已经有办法做到这一点。有人有答案吗?
采纳答案by o11c
Use git diff @{u}...HEAD
, with three dots.
使用git diff @{u}...HEAD
, 带三个点。
With two dots, or with HEAD
omitted, it will show diffs from changes on both sides.
有两个点,或HEAD
省略,它将显示两侧变化的差异。
With three dots, it will only show diffs from changes on your side.
三个点,它只会显示与您这边的变化的差异。
Edit: for people with slightly different needs, you might be interested in git merge-base
(note that it has plenty more options than the other answer uses).
编辑:对于需求略有不同的人,您可能会感兴趣git merge-base
(请注意,它比其他答案使用的选项多得多)。
回答by frasertweedale
You can find the branch pointusing git merge-base
. Consider master
the mainline and dev
the branch whose history you are interested in. To find the point at which dev
was branched from master
, run:
你可以找到分支点使用git merge-base
。考虑master
主线和dev
您对其历史感兴趣的分支。要查找dev
从 分支的点master
,请运行:
git merge-base --fork-point master dev
We can now diff dev
against this basis:
我们现在可以dev
在此基础上进行比较:
git diff $(git merge-base --fork-point master dev)..dev
If dev
is the current branchthis simplifies to:
如果dev
是当前分支,则简化为:
git diff $(git merge-base --fork-point master)
For more information see the git-merge-base
documentation.
有关更多信息,请参阅git-merge-base
文档。
回答by crimbo
You can diff the current branch from the branch start point using:
您可以使用以下方法从分支起点区分当前分支:
git diff (start point)...
Where (start point) is a branch name, a commit-id, or a tag.
其中(起点)是分支名称、提交 ID 或标签。
Eg if you're working on a feature branch branched from develop
, you can use:
例如,如果您正在处理从 分支的功能分支develop
,则可以使用:
git diff develop...
for all changes on the current branch since the branch point.
自分支点以来当前分支上的所有更改。
This was already mentioned in a comment, but I think it deserves answer status. I don't know what it will do since last rebase.
这已经在评论中提到了,但我认为它值得回答。我不知道自上次 rebase 以来它会做什么。
回答by Lawrence Kesteloot
For diffs, you want the three-dot notation. If your branch is called dev
and it branched from master
:
对于差异,您需要三点符号。如果您的分支被调用dev
并且它从master
以下分支:
% git diff master...dev
For log, you want the two-dot notation:
对于日志,您需要两点表示法:
% git log master..dev
The revision syntax r1..r2
(with two dots) means "everything reachable from r2
(inclusive) but not reachable from r1
(inclusive)". The normal way to use this is to think of r1
and r2
as specifying a range in a sequence of commits (r1
exclusive, r2
inclusive), so if you have 10 revisions, 3..7
will show you changes 4, 5, 6, and 7. It's {1, 2, 3, 4, 5, 6, 7}
minus {1, 2, 3}
. But r1
doesn't necessarily have to be an ancestor of r2
. Think of it more like a set operation where r1
represents the entire ancestry from r1
backwards, and r2
represents the entire ancestry from r2
backwards, and you're subtracting the first set from the second set.
修订语法r1..r2
(带两个点)的意思是“从r2
(包含)可到达的所有东西,但从(包含的)不可到达的东西r1
”。使用它的正常方法是考虑r1
并r2
指定提交序列(r1
独占、r2
包含)中的范围,因此如果您有 10 个修订,3..7
将显示更改 4、5、6 和 7。它是{1, 2, 3, 4, 5, 6, 7}
减号{1, 2, 3}
。但r1
不一定必须是r2
. 把它想象成一个集合操作,r1
从r1
后面开始代表整个祖先,从后面r2
代表整个祖先r2
,你从第二个集合中减去第一个集合。
So then:
那么:
git log master..dev
is the entire history of the branch minus the entire history of master. In other words, just the branch.
是分支的整个历史减去 master 的整个历史。换句话说,只是分支。
回答by Harald Nordgren
To diff against the remote master branch:
要与远程主分支进行比较:
git diff $(git merge-base HEAD origin/master)..
回答by Felix Keil
In Visual Studio 2017 there is a comfortable way to show diffs:
在 Visual Studio 2017 中,有一种舒适的方式来显示差异:
- In Team Explorer -> Branchesright click the branch and select View History
- 在团队资源管理器 -> 分支中右键单击分支并选择查看历史记录
- In the History - branchcontrol select the commits you want the diff and select Compare Commits
- 在历史记录 - 分支控件中选择您想要差异的提交并选择比较提交
You get a nice diff overview and you can open the files in compare mode.
你会得到一个很好的差异概述,你可以在比较模式下打开文件。