How can I merge files from one directory to another branch?

A simple example. This is the "master":

root
    - index.html
    - readme.md

      

This is a branch named 'dev':

root
    src
        - index.jade
    dist
        - index.html

      

I would like to take the index.html file (or all files really) in the "dev" folder and replace or merge it with what is in the root directory of my master branch. I tried, from the master:

git checkout dev dist/

      

But it produces this result:

root
    dist
        - index.html
    - index.html

      

It is clear that I do not want to. Is git capable of doing what I want, or do I just need to hack into the script? Thank!

+3


source to share


1 answer


This can be accomplished by using a merge strategy subtree

or a parameter subtree[=<path>]

in a merge strategy recursive

.

The git-merge

documentation
describes the strategy subtree

:

subtree

This is a modified recursive strategy. When combining trees A and B, if B matches subtree A, B is first adjusted to fit the tree structure of A, instead of reading the trees at the same level. This setting is also done for the common ancestor tree.

And the description of the option subtree[=<path>]

for the merge strategy recursive

:

subtree[=<path>]

This parameter is a more advanced form of subtree strategy, where the strategy suggests how two trees should be offset to match each other when merged. Instead, the specified path is prefixed (or stripped from the beginning) to match the shape of the two trees.



In your case, if master

is the current branch, the following command will merge the directory contents dist/

from branch dev

to root (you will have the option to resolve conflicts if any):

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

      

If instead you want to merge the changes into a directory dist/

, check out the branch dev

and run:

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

      

The strategy subtree

determines how to "shift" the trees. Then you can check out master

and fast forward to the new merge commit on the branch dev

.

+2


source







All Articles