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

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

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

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

我考虑过的方法:

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

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


当前回答

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

下面的步骤假设您有两个分支目标分支和源分支,并且要合并的目录目录位于源分支中。另外,假设您有其他目录(如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.

其他回答

我编写了自己的脚本“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

如果您没有太多的文件被更改,这将使您没有额外的提交。

1.临时复制分支$git结帐-b temp_branch

2.重置为上次所需提交$git reset--hard HEAD~n,其中n是需要返回的提交次数

3.从原始分支签出每个文件$git签出原点/original_branch文件名.ext

现在,如果需要,您可以提交并强制推送(覆盖远程)。

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“'合并'这些更改”

我想要的是:在一个新的分支中,以交互方式从一个分支(它有几个混乱的提交)中选择一个大块的提交。

如果diff中有任何二进制文件,gitdiff+gitapply将无法工作。

我的方法:

# New branch from a clean starting point, e.g. master
git checkout new-clean-branch origin/master

# Get all changes from the messy branch
# (quote the star so your shell doesn't expand it)
git checkout messy-branch -- '*'

# Unstage ("un-add") everything
git restore --staged .

# Interactively add hunks to be staged for commit
git add -p

我喜欢前面的“git交互式合并”答案,但有一个更简单。让Git使用交互式和到以下内容的重基组合为您实现这一点:

      A---C1---o---C2---o---o feature
     /
----o---o---o---o master

因此,您需要从“feature”分支(分支点“A”)中获取C1和C2,但目前没有其他分支。

# git branch temp feature
# git checkout master
# git rebase -i --onto HEAD A temp

正如前面的回答一样,这会让您进入交互式编辑器,在其中选择C1和C2的“pick”行(如上所述)。保存并退出,然后它将继续重新创建数据库,并为您提供分支“temp”和主机+C1+C2的HEAD:

      A---C1---o---C2---o---o feature
     /
----o---o---o---o-master--C1---C2 [HEAD, temp]

然后,您只需将master更新为HEAD并删除临时分支即可:

# git branch -f master HEAD
# git branch -d temp