如何在冲突的 Git rebase 中获得“他们的”更改?

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

How to get "their" changes in the middle of conflicting Git rebase?

gitrebasegit-rebase

提问by Ondra ?i?ka

I have conflicting branches, branch2 branched from branch1.

我有冲突的分支,分支 2 从分支 1 分支出来。

Let's say when rebasing branch2on current branch1, while resolving conflicts, I decide to take some(not all) of "their" (i.e. branch1) files as-is. How do I do that?

假设在branch2基于 current 时branch1,在解决冲突的同时,我决定按原样获取一些(不是全部)“他们的”(即branch1)文件。我怎么做?

I tried:

我试过:

git checkout branch1:foo/bar.java
fatal: reference is not a tree: TS-modules-tmp:foo/bar.java

git checkout refs/heads/branch1:foo/bar.java
fatal: reference is not a tree: refs/heads/TS-modules-tmp:foo/bar.java

回答by iGEL

You want to use:

你想使用:

git checkout --ours foo/bar.java
git add foo/bar.java

If you rebase a branch feature_xagainst master(i.e. running git rebase masterwhile on branch feature_x), during rebasing oursrefers to masterand theirsto feature_x.

如果衍合分支feature_xmaster(即运行git rebase master时的分支feature_x),垫底期间ours是指mastertheirsfeature_x

As pointed out in the git-rebase docs:

正如git-rebase 文档中所指出的:

Note that a rebase merge works by replaying each commit from the working branch on top of the branch. Because of this, when a merge conflict happens, the side reported as ours is the so-far rebased series, starting with <upstream>, and theirs is the working branch. In other words, the sides are swapped.

请注意,rebase 合并通过在分支顶部重放来自工作分支的每个提交来工作。因此,当发生合并冲突时,报告为我们的一方是迄今为止重新调整的系列,从 <upstream> 开始,而他们的则是工作分支。换句话说,双方互换。

For further details read this thread.

有关更多详细信息,请阅读此线程

回答by Adrian Cornish

If you want to pull a particular file from another branch just do

如果您想从另一个分支中提取特定文件,只需执行

git checkout branch1 -- filenamefoo.txt

This will pull a version of the file from one branch into the current tree

这会将文件的一个版本从一个分支拉到当前树中