我正在一个新项目中使用Git,该项目有两个并行但目前处于试验阶段的开发分支:

master:导入现有的代码库,再加上一些我通常确信的修改exp1:实验分支#1exp2:实验分支#2

exp1和exp2表示两种非常不同的体系结构方法。在我进一步相处之前,我无法知道哪一个(如果有的话)会起作用。当我在一个分支中取得进展时,我有时会对另一个分支进行有用的编辑,并希望仅合并这些编辑。

将一个开发分支中的选择性更改合并到另一个分支中,同时保留其他所有内容的最佳方法是什么?

我考虑过的方法:

gitmerge——无需提交,然后手动取消大量编辑,我不想在分支之间共享这些编辑。手动将通用文件复制到临时目录中,然后git checkout移动到另一个分支,然后更多地手动将临时目录复制到工作树中。在上述基础上的变化。暂时放弃exp分支,使用另外两个本地存储库进行实验。这使得手动复制文件更加简单。

所有这三种方法都显得乏味且容易出错。我希望有更好的方法;类似于过滤器路径参数,可以使gitmerge更有选择性。


当前回答

这是我合并选定文件的工作流。

# Make a new branch (this will be temporary)
git checkout -b newbranch

# Grab the changes
git merge --no-commit  featurebranch

# Unstage those changes
git reset HEAD
(You can now see the files from the merge are unstaged)

# Now you can chose which files are to be merged.
git add -p

# Remember to "git add" any new files you wish to keep
git commit

其他回答

我将专注于我感兴趣的这个问题的子集:我有两个分支,我想将一个文件伪合并到另一个文件中。

(我说“伪合并”是因为我不需要或不希望合并提交;我只想以我认为合适的方式合并文件两个版本的贡献。)

我的方法基于https://stackoverflow.com/a/39916536/341994.不幸的是,这个问题是重复的(在我看来,这是错误的:它不是这个问题的重复,回答和重复是错误的,这是回答者在那里做的)。但这个答案有一些问题,所以我已经现代化并清理了方法。我使用恢复而不是签出和重置,并且我不必提交任何不需要的东西。

好吧,假设我有三个文件:

$ ls
a   b   f

但我只想伪合并其中一个,a,来自另一个分支。让我们看看他们,看看情况会是什么样子。这是我的版本:

$ cat a
line one
line two
line three
line four
line five

以下是其他分支机构的版本:

$ git show otherbranch:a
line one
line two edited
line three
line four
line five
line six

现在这里的技巧是,我们将使用索引作为草稿(毕竟,这是它的用途)。因此,我们首先(步骤1)确保将我们的版本复制到索引中:

$ git add a

现在(步骤2)我们可以使用restore从其他分支获取版本(现在,restore比checkout更好,因为它可以让我们更清楚地说话):

$ git restore --source otherbranch a

乍一看,这看起来很糟糕。我们现在已经用其他分支的版本完全覆盖了我们的a,如您所见:

$ cat a
line one
line two edited
line three
line four
line five
line six

但别担心!如您所见,以前版本的仍在索引中:

$ git diff a
diff --git a/a b/a
index abf51fa..333614b 100644
--- a/a
+++ b/a
@@ -1,6 +1,7 @@
 line one
-line two
+line two edited
 line three
 line four
 line five
+line six

很好,现在我们准备好了关键步骤(步骤3)。我们将文件从工作树添加到索引中进行交互式补丁。

我们可以说git add-p a来启动交互式补丁程序,在这种情况下,我们一次只能得到一个大块。但在这种情况下,只有一个帅哥,我无论如何都想编辑它,所以我说:

$ git add --e a

结果是我们在编辑器中打开了一个diff补丁文件!它看起来像这样:

 line one
-line two
+line two edited
 line three
 line four
 line five
+line six

通过仔细编辑,我们现在可以决定我们要接受哪些部分,不接受哪些部分。让我们接受“第六行”,而不是“第二行已编辑”。所以我们编辑如下:

 line one
 line two
 line three
 line four
 line five
+line six

我们关闭编辑器,补丁应用于a的索引版本。但我们还没有完全完成!a的另一个分支版本仍然位于工作树中:

$ cat a
line one
line two edited
line three
line four
line five
line six

我们喜欢的版本在索引中,记得吗?为了实现这一点,(步骤4)我们只需简单地调用gitrestore(同样,这是现代的方式;restore比reset更好,可以应用于单个文件):

$ git restore a

现在我们的a是正确的,我们都完成了:

$ cat a
line one
line two
line three
line four
line five
line six

我们现在可以承诺,但我们不必;我们已经完成了既定目标。

如果您只需要合并一个特定的目录并保留所有其他内容,同时保留历史记录,那么您可以尝试这样做。。。在实验之前,从主对象创建一个新的目标分支。

下面的步骤假设您有两个分支目标分支和源分支,并且要合并的目录目录位于源分支中。另外,假设您有其他目录(如dir)保留在目标中,您不想更改并保留历史记录。此外,假设要合并的目录中存在合并冲突。

git checkout target-branch
git merge --no-ff --no-commit -X theirs source-branch
# the option "-X theirs", will pick theirs when there is a conflict. 
# the options "--no--ff --no-commit" prevent a commit after a merge, and give you an opportunity to fix other directories you want to retain, before you commit this merge.

# the above, would have messed up the other directories that you want to retain.
# so you need to reset them for every directory that you want to retain.
git reset HEAD dir-to-retain
# verify everything and commit.

tl;博士

git checkout source_branch -- path/to/file
# resolve conflicts if any
git commit -am '...'

我遇到了和你上面提到的完全相同的问题。但我在解释答案时发现这一点更清楚。

摘要:

检查要合并的分支的路径,$git签出source_branch--<paths>。。。提示:它也可以像链接文章中看到的那样,不使用“--”。或者选择性地合并大块$git checkout-p source_branch--<paths>。。。

或者,使用reset,然后添加选项-p,

    $ git reset <paths>...
    $ git add -p <paths>...

最后提交$gitcommit-m“'合并'这些更改”

对我来说,git reset-soft分支是有选择地从另一个分支中选择更改的最简单方法,因为这个命令将所有diff更改放在我的工作树中,我可以轻松地选择或还原我需要的更改。

通过这种方式,我可以完全控制提交的文件。

还有另一条路要走:

git checkout -p

它是git checkout和git add-p的混合体,可能正是您想要的:

   -p, --patch
       Interactively select hunks in the difference between the <tree-ish>
       (or the index, if unspecified) and the working tree. The chosen
       hunks are then applied in reverse to the working tree (and if a
       <tree-ish> was specified, the index).

       This means that you can use git checkout -p to selectively discard
       edits from your current working tree. See the “Interactive Mode”
       section of git-add(1) to learn how to operate the --patch mode.