在从远程分支合并之前如何检查真实的 git diff?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4944376/
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 check real git diff before merging from remote branch?
提问by abhiomkar
I want to check the real diff between remote branch and local branch. How can I do that?
我想检查远程分支和本地分支之间的真正差异。我怎样才能做到这一点?
Issuing the below command partially works, but it shows the diff of the new changes of my local branch too.
发出以下命令部分有效,但它也显示了我本地分支的新更改的差异。
git diff remote/branch
采纳答案by Evgen Bodunov
From the documentation:
从文档:
git diff [--options] <commit>...<commit> [--] [<path>…]
This form is to view the changes on the branch containing and up to the second
<commit>
, starting at a common ancestor of both<commit>
. "git diff A...B" is equivalent to "git diff $(git-merge-base A B) B". You can omit any one of<commit>
, which has the same effect as using HEAD instead.
git diff [--options] <commit>...<commit> [--] [<path>…]
此表单用于查看包含和直到第二个分支上的更改
<commit>
,从两者的共同祖先开始<commit>
。“git diff A...B”相当于“git diff $(git-merge-base AB) B”。您可以省略任何一个<commit>
,这与使用 HEAD 具有相同的效果。
did you try this?
你试过这个吗?
回答by rholmes
Since this topic comes up frequently, and can be confusing without a bit of background on how git works, I thought I'd try to explain the simplest case possible, but with sufficient depth that newcomers will have enough of a handle on it to do additional research.
由于这个话题经常出现,并且在没有一点关于 git 工作原理的背景的情况下可能会令人困惑,我想我会尽量解释最简单的情况,但有足够的深度,新人将有足够的把握来做额外的研究。
If you set up your git repository via a normal 'clone' and have the default refspecs, meaning that your remote is named 'origin' and you pull/fetch from the branch 'master', you may sometimes need to see what's in the remote repository before pulling it down.
如果您通过普通的“克隆”设置您的 git 存储库并具有默认的 refspecs,这意味着您的遥控器名为“origin”并且您从分支“master”拉取/获取,您有时可能需要查看遥控器中的内容在将其拉下之前存储库。
Since the "git pull" does an automatic merge (unless there are conflicts), it can be nice to see what's "incoming" next. If you're not familiar with how git works, and how refspecs in particular are managed, this can be a bit non-intuitive.
由于“git pull”执行自动合并(除非存在冲突),因此很高兴看到接下来的“传入”是什么。如果您不熟悉 git 的工作原理,以及特别是如何管理 refspecs,这可能有点不直观。
Suppose someone makes a change in the remote repository (for sake of illustration, adding a line to the remote repository by committing a change and pushing it), and you type:
假设有人在远程存储库中进行了更改(为了说明起见,通过提交更改并推送它向远程存储库添加一行),然后您键入:
$ git diff origin/master
You probably won't see any changes; however if you do the following:
您可能看不到任何变化;但是,如果您执行以下操作:
$ git fetch; git diff ..origin/master
you'll see the difference between what's been committed to your local git repository and what's in the remote repository. You will NOT see any changes which are in your local filesystem or staged in your index.
您将看到提交到本地 git 存储库的内容与远程存储库中的内容之间的区别。您不会看到本地文件系统中或索引中暂存的任何更改。
Ok, why do we do this? origin/master is a refspec(see man pages). In short, this is what we refer to in order to compare against, pull or fetch from, and push to. All of the following are functionally equivalent:
好的,我们为什么要这样做?origin/master 是一个参考规范(参见手册页)。简而言之,这就是我们所指的,以便进行比较、拉取或获取以及推送到。以下所有内容在功能上是等效的:
origin/master
remotes/origin/master
refs/remotes/origin/master
To begin to untangle this, just take a peek at your repository's .git directory structure. A typical layout looks like this:
要开始解决这个问题,只需看一下您存储库的 .git 目录结构。典型的布局如下所示:
.git/refs
.git/refs/heads
.git/refs/heads/master
.git/refs/remotes
.git/refs/remotes/origin
.git/refs/remotes/origin/HEAD
.git/refs/remotes/origin/master
.git/refs/tags
Look at .git/refs/remotes/origin/HEAD; in the default case it will point to the branch you use to pull from and push to. In my case, since I'm on master, the contents of this text file look like this:
查看 .git/refs/remotes/origin/HEAD;在默认情况下,它将指向您用来拉取和推送到的分支。就我而言,由于我在 master 上,此文本文件的内容如下所示:
ref: refs/remotes/origin/master
This tells me that the HEAD of my remote is identified by the refspec 'refs/remotes/origin/master'(which happens to have the aliases mentioned above).
这告诉我,我的遥控器的 HEAD 由 refspec 'refs/remotes/origin/master'(恰好具有上述别名)标识。
This doesn't tell us much; what's the state of the remote repository? Look at the state of the remote master:
这并没有告诉我们太多;远程存储库的状态是什么?查看远程master的状态:
$ cat .git/refs/heads/master
6d0fb0adfdfa5af861931bb06d34100b349f1d63
Ok, it's a SHA1 hash; probably a commit. How does it get put in this file? Well, whenever you do a pull or a fetch, this file is updated with the most recent commit from the remote which was pulled or fetched. This explains why we have to git fetch
prior to performing the diff. Remember, git fetch
just updates your local copy of a remote branch, but doesn't merge it with your working copy. It is completely safe. A git fetch; git merge
is equivalent to a git pull
.
好的,这是一个 SHA1 哈希值;可能是一次提交。它是如何放入这个文件的?好吧,每当您执行拉取或提取操作时,此文件都会更新为从远程拉取或提取的最新提交。这解释了为什么我们必须git fetch
在执行差异之前。请记住,git fetch
只更新远程分支的本地副本,但不会将其与您的工作副本合并。这是完全安全的。Agit fetch; git merge
等价于 a git pull
。
Once you do the fetch, git will be able to see the most recent commit in the remote repository as of the time of the fetch.
执行提取后,git 将能够在提取时看到远程存储库中的最新提交。
You can use various combinations of specifiers to git to see your diffs as you desire (the following examples use the local working copy as the implicit first commit):
您可以使用各种说明符组合来 git 以根据需要查看差异(以下示例使用本地工作副本作为隐式第一次提交):
$ git diff remote/origin
This shows the incoming remote additions as deletions; any additions in your local
repository are shown as additions.
$ git diff ...remote/origin
Shows incoming remote additions as additions; the triple-dot excludes changes
committed to your local repository.
$ git diff ..remote/origin
Shows incoming remote additions as additions; the double-dot includes changes
committed to your local repository as deletions (since they are not yet pushed).
For info on ".." vs "..." see git help diff
as well as the excellent documentation at git-scm revision selection: commit rangesBriefly, for the examples above, double-dot syntax shows all commits reachable from origin/master but not your working copy. Likewise, the triple-dot syntax shows all the commits reachable from either commit (implicit working copy, remote/origin) but not from both.
有关 ".." 与 "..." 的信息,请参阅git-scm 修订选择中git help diff
的优秀文档:提交范围简而言之,对于上面的示例,双点语法显示了从 origin/master 可到达的所有提交,但不能你的工作副本。同样,三点语法显示了从任一提交(隐式工作副本、远程/源)可访问的所有提交,但不能从两者都显示。
I'm going through this step by step because I'm fairly new to git and this is exactly the type of thing that had meconfused... I'm sure that git experts can find flaws with the details... I just hope this answer bridges the gap for some people who find all the various posts a bit terse.
我正在逐步完成这一步,因为我对 git 还很陌生,这正是让我感到困惑的事情类型……我相信 git 专家可以找到细节的缺陷……我只是希望这个答案能够弥补一些发现所有各种帖子都有些简洁的人的差距。
回答by Antoine Pelisse
What you want to do is, as suggested by Evgen Bodunov:
您想要做的是,正如 Evgen Bodunov 所建议的那样:
git diff ...remote/branch
This will diff changes from remote/branch and ignore changes from your current HEAD.
这将区分来自远程/分支的更改并忽略来自当前 HEAD 的更改。