Simple example. This is 'master':
root
- index.html
- readme.md
This is a branch called 'dev':
root
src
- index.jade
dist
- 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:
root
dist
- 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!
This can be accomplished using the
subtree
merge strategy, or thesubtree[=<path>]
option to therecursive
merge strategy.From the
git-merge
documentation, the description of thesubtree
strategy:And the description of the
subtree[=<path>]
option to therecursive
merge strategy:In your case, if
master
is the current branch the following command will merge the contents of thedist/
directory from thedev
branch into the root directory (you will have the opportunity to resolve conflicts should there be any):If, instead, you wish to merge changes into the
dist/
directory, checkout thedev
branch then run:The
subtree
strategy figures out how to "shift" the trees. You can then checkoutmaster
and fast-forward to the new merge commit on thedev
branch.