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

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

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

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

我考虑过的方法:

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

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


当前回答

要有选择地将文件从一个分支合并到另一个分支,请运行

git merge --no-ff --no-commit branchX

其中branchX是要从中合并到当前分支的分支。

--no commit选项将暂存已被Git合并的文件,而无需实际提交。这将使您有机会根据需要修改合并的文件,然后自己提交。

根据您希望合并文件的方式,有四种情况:

1) 你想要真正的合并。

在这种情况下,您接受合并文件的方式是Git自动合并文件,然后提交它们。

2) 有些文件您不想合并。

例如,您希望保留当前分支中的版本,而忽略要从中合并的分支的版本。

要选择当前分支中的版本,请运行:

git checkout HEAD file1

这将在当前分支中检索file1的版本,并覆盖Git自动合并的file1。

3) 如果您希望branchX中的版本(而不是真正的合并)。

Run:

git checkout branchX file1

这将在branchX中检索file1的版本,并覆盖Git自动合并的file1。

4) 最后一种情况是,如果只选择文件1中的特定合并。

在这种情况下,您可以直接编辑修改后的file1,将其更新为您希望的file1版本,然后提交。

如果Git无法自动合并文件,它将报告该文件为“未合并”,并生成一个副本,您需要手动解决冲突。


为了用示例进一步解释,假设您希望将branchX合并到当前分支中:

git merge --no-ff --no-commit branchX

然后运行gitstatus命令查看修改文件的状态。

例如:

git status

# On branch master
# Changes to be committed:
#
#       modified:   file1
#       modified:   file2
#       modified:   file3
# Unmerged paths:
#   (use "git add/rm <file>..." as appropriate to mark resolution)
#
#       both modified:      file4
#

其中,file1、file2和file3是git已成功自动合并的文件。

这意味着所有这三个文件的master和branchX中的更改都已合并在一起,没有任何冲突。

您可以通过运行gitdiff-cached来检查合并是如何完成的;

git diff --cached file1
git diff --cached file2
git diff --cached file3

如果您发现不需要合并,那么可以

直接编辑文件拯救git提交

如果您不想合并文件1并希望在当前分支中保留版本

Run

git checkout HEAD file1

如果您不想合并file2,只想在branchX中使用版本

Run

git checkout branchX file2

如果希望自动合并file3,请不要执行任何操作。

Git目前已经将其合并。

上面的文件4是Git失败的合并。这意味着在同一行上发生的两个分支中都有更改。这是您需要手动解决冲突的地方。您可以通过直接编辑文件或为希望file4成为的分支中的版本运行签出命令来放弃合并的文件。

最后,不要忘记gitcommit。

其他回答

我不喜欢上述方法。使用cherry pick可以很好地选择一个改变,但如果你想带来所有的改变,除了一些不好的改变,这是一种痛苦。这是我的方法。

没有可以传递给gitmerge的--interactive参数。

以下是备选方案:

您在分支“特性”中进行了一些更改,您希望以一种不拖泥带水的方式将其中的一些(但不是全部)提交给“主”(即,您不希望逐一挑选和提交)

git checkout feature
git checkout -b temp
git rebase -i master

# Above will drop you in an editor and pick the changes you want ala:
pick 7266df7 First change
pick 1b3f7df Another change
pick 5bbf56f Last change

# Rebase b44c147..5bbf56f onto b44c147
#
# Commands:
# pick = use commit
# edit = use commit, but stop for amending
# squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

git checkout master
git pull . temp
git branch -d temp

所以,只需将其包装在shell脚本中,将master更改为$to,并将feature更改为$from,就可以了:

#!/bin/bash
# git-interactive-merge
from=$1
to=$2
git checkout $from
git checkout -b ${from}_tmp
git rebase -i $to
# Above will drop you in an editor and pick the changes you want
git checkout $to
git pull . ${from}_tmp
git branch -d ${from}_tmp

您可以使用读取树将给定的远程树读取或合并到当前索引中,例如:

git remote add foo git@example.com/foo.git
git fetch foo
git read-tree --prefix=my-folder/ -u foo/master:trunk/their-folder

要执行合并,请改用-m。

另请参阅:如何在Git中合并子目录?

还有另一条路要走:

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.

我编写了自己的脚本“pmerge”来部分合并目录。这是一项正在进行的工作,我仍然在学习Git和Bash脚本。

该命令使用gitmerge--no提交,然后取消应用与提供的路径不匹配的更改。

用法:git pmerge分支路径示例:gitmerge-devel-src/

我还没有对它进行广泛的测试。工作目录应该没有任何未提交的更改和未跟踪的文件。

#!/bin/bash

E_BADARGS=65

if [ $# -ne 2 ]
then
    echo "Usage: `basename $0` branch path"
    exit $E_BADARGS
fi

git merge $1 --no-commit
IFS=$'\n'

# List of changes due to merge | replace nulls with newlines | strip lines to just filenames | ensure lines are unique
for f in $(git status --porcelain -z -uno | tr '\000' '\n' | sed -e 's/^[[:graph:]][[:space:]]\{1,\}//' | uniq); do
    [[ $f == $2* ]] && continue
    if git reset $f >/dev/null 2>&1; then
        # Reset failed... file was previously unversioned
        echo Deleting $f
        rm $f
    else
        echo Reverting $f
        git checkout -- $f >/dev/null 2>&1
    fi
done
unset IFS

奇怪的是,git仍然没有这么方便的工具“开箱即用”。我在通过从当前版本分支中修复一些错误来更新一些旧版本分支(仍然有很多软件用户)时大量使用它。在这种情况下,通常需要从主干中的文件中快速获取一些代码行,而忽略许多其他更改(这些更改不应该进入旧版本)。。。当然,在这种情况下需要交互式三路合并,git checkout--patch<branch><file path>不能用于这种选择性合并。

您可以轻松完成:

只需将此行添加到global.gitconfig或local.git/config文件的[alias]部分:

[alias]
    mergetool-file = "!sh -c 'git show $1:$2 > $2.theirs; git show $(git merge-base $1 $(git rev-parse HEAD)):$2 > $2.base; /C/BCompare3/BCompare.exe $2.theirs $2 $2.base $2; rm -f $2.theirs; rm -f $2.base;' -"

这意味着您使用Beyond Compare。如果需要,只需更换您选择的软件即可。或者,如果不需要交互式选择性合并,可以将其更改为三向自动合并:

[alias]
    mergetool-file = "!sh -c 'git show $1:$2 > $2.theirs; git show $(git merge-base $1 $(git rev-parse HEAD)):$2 > $2.base; git merge-file $2 $2.base $2.theirs; rm -f $2.theirs; rm -f $2.base;' -"

然后这样使用:

git mergetool-file <source branch> <file path>

这将为您提供其他分支中任何文件的真正选择性树形合并机会。