Git merge from a specific folder only

2019-02-07 19:04发布

问题:

I've created a rails website for client X. I now have a client, Y, who wants a website that does the exact same thing as client X, but with a different skin.

I made a git branch from clientXcode and called it clientYcode. I then did all the changes to the views to make it look different, and lala, the same website with a different skin.

Now here's what I don't understand about git: I've made many changes to clientXcode in the views, models, and controllers; and now I want to merge those changes into clientYcode, excluding any view changes. Since views, models, and controllers each have their own folder in rails I was hoping to be able to do something like:

git merge client_x_code 'app/controllers/*', 'app/models/*'

QUESTION 1: Is something like that possible with git? If so, how would I do it?

QUESTION 2: Was branching the best solution to make a copy of my project?

回答1:

Well I found the easiest solution to my problem...

git checkout clientYcode
git checkout clientXcode "app/controllers/"
git checkout clientXcode "app/models/"

And that does what I want!



回答2:

The simplest course of action is to merge everything, except the content of the directory you want to keep.
You can do that by adding a merge driver in that directory, as the How do I tell git to always select my local version for conflicted merges on a specific file? question details.

With that merge driver in place, Branching is a good solution in this case.


Extract:

We have a .gitattributes file defined in the dirWithCopyMerge directory (defined only in the branch where the merge will occurs: myBranch), and we have a .git\config file which now contains a merge driver.

[merge "keepMine"]
        name = always keep mine during merge
        driver = keepMine.sh %O %A %B