如何将文件从一个分支的目录合并到另一个分支? [英] How to merge files from one branch's directory into another branch?

查看:438
本文介绍了如何将文件从一个分支的目录合并到另一个分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的例子。这是'master':

Simple example. This is 'master':

root
    - index.html
    - readme.md

这是一个名为 dev的分支:

This is a branch called 'dev':

root
    src
        - index.jade
    dist
        - index.html

我想将'dev'分支的'dist'文件夹中的index.html文件(或所有文件,实际上)带走并替换或合并在我的master分支的根目录中。我试过了,是从主人那里来的:

I'd like to take the index.html file (or all files, really) in the 'dist' folder of the 'dev' branch and replace or merge it with the one in the root directory of my master branch. I've tried, from master:

git checkout dev dist/

但它会产生以下结果:

root
    dist
        - index.html
    - index.html

显然不是什么我想要。 git是否可以执行我想做的事情,还是只需要编写一个脚本?谢谢!

Clearly not what I want. Is git capable of doing what I want it to do or will I just have to whip up a script? Thanks!

推荐答案

这可以使用子树合并策略来完成,或递归合并策略的 subtree [=< path>] 选项。

This can be accomplished using the subtree merge strategy, or the subtree[=<path>] option to the recursive merge strategy.

git-merge 文档子树策略的说明:


子树

subtree

这是一种修改的递归策略。合并树A和B时,如果B对应于A的子树,则首先调整B以匹配A的树结构,而不是读取相同级别的树。

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>]

subtree[=<path>]

此选项是 subtree 策略的更高级形式,该策略会猜测合并时如何移动两棵树以相互匹配。相反,将指定的路径添加前缀(或从开头剥离)以使两棵树的形状匹配。

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 是当前分支,以下命令将从 dev合并 dist / 目录的内容分支到根目录(如果有冲突,您将有机会解决冲突):

In your case, if master is the current branch the following command will merge the contents of the dist/ directory from the dev branch into the root directory (you will have the opportunity to resolve conflicts should there be any):

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

如果相反,您希望将更改合并到 dist / 目录,签出 dev 分支,然后运行:

If, instead, you wish to merge changes into the dist/ directory, checkout the dev branch then run:

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

subtree 策略指出了如何转移树。然后,您可以签出 master 并快速转发到 dev 分支上的新合并提交。

The subtree strategy figures out how to "shift" the trees. You can then checkout master and fast-forward to the new merge commit on the dev branch.

这篇关于如何将文件从一个分支的目录合并到另一个分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆