git - 如何将一个分支目录中的文件 merge 到另一个分支?

标签 git git-merge branching-and-merging git-checkout

简单的例子。这是“主人”:

root
    - index.html
    - readme.md

这是一个名为“dev”的分支:

root
    src
        - index.jade
    dist
        - index.html

我想获取“dev”分支的“dist”文件夹中的 index.html 文件(或者实际上是所有文件),并将其替换或 merge 到我的 master 分支根目录中的文件。我试过了,来自大师:

git checkout dev dist/

但它会产生这样的结果:

root
    dist
        - index.html
    - index.html

显然不是我想要的。 git 能做我想让它做的事,还是我只需要编写一个脚本?谢谢!

最佳答案

这可以使用 subtree 来完成 merge 策略,或 subtree[=<path>] recursive 的选项 merge 策略。

来自 git-merge documentation , subtree 的描述策略:

subtree

This is a modified recursive strategy. When merging trees A and B, if B corresponds to a subtree of A, B is first adjusted to match the tree structure of A, instead of reading the trees at the same level. This adjustment is also done to the common ancestor tree.

以及 subtree[=<path>] 的描述recursive 的选项 merge 策略:

subtree[=<path>]

This option is a more advanced form of subtree strategy, where the strategy makes a guess on how two trees must be shifted to match with each other when merging. Instead, the specified path is prefixed (or stripped from the beginning) to make the shape of two trees to match.

在你的例子中,如果 master是当前分支以下命令将 merge dist/ 的内容来自 dev 的目录分支到根目录(如果有任何冲突,您将有机会解决):

git merge --no-ff -s recursive -X subtree=dist dev

相反,如果您希望将更改 merge 到 dist/目录,检查 dev分支然后运行:

git merge --no-ff -s recursive -X subtree=dist master

subtree策略计算出如何“移动”树木。然后您可以 checkout master并快进到 dev 上的新 merge 提交分支机构。

关于git - 如何将一个分支目录中的文件 merge 到另一个分支?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29686681/

相关文章:

ruby-on-rails - 致命的 : Not a git repository . .. 错误信息

git - tortoisegit 支持 git lfs 吗?

java - 从 Android Git 创建 Eclipse 项目

Git:定期 merge/重新设置长期存在的错误修复分支以掌握

git stash pop : avoiding stash drop and un-staging steps

git - 当您创建 git 分支然后删除以前的提交时会发生什么?

Git 分支并切换回 master 是领先的吗?

git rebase --continue 与新提交

windows - 如何从上游分支 merge ?

Git - 两个分支上具有不同版本的相同文件, merge 时不想覆盖