How to merge files from one branch's directory into another branch?

Simple example. This is ‘master’:

    - index.html

This is a branch called ‘dev’:

  • Commit select changes to another branch then resume work on current branch?
  • Git workflow: forking a project and maintaing a local modified copy, but keep up to date
  • Delete git master branch on a private server (not GitHub)
  • Git octopus merge order of multiple branches
  • Git - How to see if a branch has EVER been merged into another branch?
  • Git: Pulled the wrong branch, then pushed the merge. How to undo?
  • root
            - index.jade
            - index.html

    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/

    But it produces this result:

            - index.html
        - index.html

    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!

  • How can I limit git log (or svn log) to revisions that regard one particular file?
  • Preserve git remotes
  • What does diff.mnemonicprefix do?
  • Git change branch when file of same name is present
  • fatal: remote origin already exists - GitHub
  • vim syntax highlighting for git commit messages - custom commentchar
  • One Solution collect form web for “How to merge files from one branch's directory into another branch?”

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

    From the git-merge documentation, the description of the subtree strategy:


    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.

    And the description of the subtree[=<path>] option to the recursive merge strategy:


    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.

    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

    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

    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.

    Git Baby is a git and github fan, let's start git clone.