简单的例子。这是“主人”:
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/