“我们的"的确切含义是什么?和“他们的"在 git 中? [英] What is the precise meaning of "ours" and "theirs" in git?

查看:23
本文介绍了“我们的"的确切含义是什么?和“他们的"在 git 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能听起来太基本了,但我已经搜索了答案,现在比以前更困惑了.

This might sound like too basic of a question, but I have searched for answers and I am more confused now than before.

将我的分支合并到另一个分支时,git 中的我们的"和他们的"是什么意思?两个分支都是我们的".

What does "ours" and "theirs" mean in git when merging my branch into my other branch? Both branches are "ours".

在合并冲突中,我们的"总是显示两个版本中的上一个吗?

In a merge conflict is "ours" always the upper of the two versions displayed?

ours"是否总是指合并开始时 HEAD 指向的分支?如果是这样,那么为什么不使用像current branch's"这样的明确的所有格引用,而不是使用像ours"这样的所有格代词,它在指称上是模棱两可的(因为两个分支在技术上都是我们的)?

Does "ours" always refer to the branch that HEAD was pointing to when the merge began? If so then why not use a clear possessive reference like "current branch's" instead of using a possessive pronoun like "ours" that is referentially ambiguous (since both branches are technically ours)?

或者只是使用分支名称(而不是说我们的",而只是说本地大师的"之类的)?

Or just use the branch name (instead of saying "ours" just say "local master's" or such)?

对我来说最令人困惑的部分是我是否在特定分支的 .gitattributes 文件中指定.假设在 test 分支 中,我有以下 .gitattributes 文件:

The most confusing part to me is if I specify in a specific branch's .gitattributes file. Lets say in test branch I have the following .gitattributes file:

config.xml merge=ours

现在我结帐并将 HEAD 指向 ma​​ster,然后在 test 中合并.既然ma​​ster是我们的,而且test的.gitattributes没有check out,那会不会有影响?如果它确实有效果,既然 ma​​ster 现在是我们的",那么会发生什么?

Now I checkout and point HEAD to master then merge in test. Since master is ours, and test's .gitattributes is not checked out, will it even have an effect? If it does have an effect, since master is now "ours", then what will happen?

推荐答案

我怀疑您在这里感到困惑,因为它从根本上令人困惑.更糟糕的是,当您进行 rebase 时,我们/他们的所有东西都会转换角色(倒退).

I suspect you're confused here because it's fundamentally confusing. To make things worse, the whole ours/theirs stuff switches roles (becomes backwards) when you are doing a rebase.

最终,在 git merge 期间,ours"分支指的是您要合并的分支:

Ultimately, during a git merge, the "ours" branch refers to the branch you're merging into:

git checkout merge-into-ours

theirs"分支指的是您要合并的(单个)分支:

and the "theirs" branch refers to the (single) branch you're merging:

git merge from-theirs

这里我们的"和他们的"是有道理的,尽管他们的"可能是你的,但他们的"不是你在运行 使用的git合并.

and here "ours" and "theirs" makes some sense, as even though "theirs" is probably yours anyway, "theirs" is not the one you were on when you ran git merge.

虽然使用实际的分支名称可能很酷,但在更复杂的情况下它会崩溃.例如,您可以这样做:

While using the actual branch name might be pretty cool, it falls apart in more complex cases. For instance, instead of the above, you might do:

git checkout ours
git merge 1234567

通过原始提交 ID 合并的位置.更糟糕的是,您甚至可以这样做:

where you're merging by raw commit-ID. Worse, you can even do this:

git checkout 7777777    # detach HEAD
git merge 1234567       # do a test merge

在这种情况下,没有涉及分支名称!

in which case there are no branch names involved!

我认为这里没什么帮助,但实际上,在 gitrevisions 语法,在冲突合并期间,您可以通过编号引用索引中的单个路径

I think it's little help here, but in fact, in gitrevisions syntax, you can refer to an individual path in the index by number, during a conflicted merge

git show :1:README
git show :2:README
git show :3:README

第 1 阶段是文件的共同祖先,第 2 阶段是目标分支版本,第 3 阶段是您要从中合并的版本.

Stage #1 is the common ancestor of the files, stage #2 is the target-branch version, and stage #3 is the version you are merging from.

我们的"和他们的"概念在 rebase 期间交换的原因是 rebase 的工作原理是在匿名分支(分离的 HEAD 模式)中进行一系列挑选.目标分支是匿名分支,merge-from 分支是你的原始(re-rebase)分支:所以--ours"意味着匿名一个rebase正在构建,而--theirs"意味着我们的分支正在rebase".

The reason the "ours" and "theirs" notions get swapped around during rebase is that rebase works by doing a series of cherry-picks, into an anonymous branch (detached HEAD mode). The target branch is the anonymous branch, and the merge-from branch is your original (pre-rebase) branch: so "--ours" means the anonymous one rebase is building while "--theirs" means "our branch being rebased".

至于 gitattributes 条目:它可能有一个效果:我们的"在内部实际上意味着使用第 2 阶段".但正如您所注意到的,当时它实际上并没有到位,所以它不应该在这里产生影响......好吧,除非你在开始之前将它复制到工作树中.

As for the gitattributes entry: it could have an effect: "ours" really means "use stage #2" internally. But as you note, it's not actually in place at the time, so it should not have an effect here ... well, not unless you copy it into the work tree before you start.

另外,顺便说一句,这适用于我们和他们的所有用途,但有些是在整个文件级别(-s ours 用于合并策略;git checkout --我们的 在合并冲突期间),有些是逐个的(-X ours-X theirs-s递归合并).这可能无助于解决任何混淆.

Also, by the way, this applies to all uses of ours and theirs, but some are on a whole file level (-s ours for a merge strategy; git checkout --ours during a merge conflict) and some are on a piece-by-piece basis (-X ours or -X theirs during a -s recursive merge). Which probably does not help with any of the confusion.

不过,我从来没有想出更好的名字.并且:参见 VonC 对另一个问题的回答,其中 git mergetool 为这些引入了更多名称,称它们为本地"和远程"!

I've never come up with a better name for these, though. And: see VonC's answer to another question, where git mergetool introduces yet more names for these, calling them "local" and "remote"!

这篇关于“我们的"的确切含义是什么?和“他们的"在 git 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆