3

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?

tybro0103
  • 48,327
  • 33
  • 144
  • 170

2 Answers2

10

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!

tybro0103
  • 48,327
  • 33
  • 144
  • 170
  • But that is only temporary in your workarea, and next time you switch branches you will have to repeat all that. – ScottJ Nov 05 '09 at 21:01
  • 2
    well I of course then do a commit to save the changes in my current branch – tybro0103 Nov 06 '09 at 19:51
  • 1
    Thats nice! Also see http://jasonrudolph.com/blog/2009/02/25/git-tip-how-to-merge-specific-files-from-another-branch/ for a complete writeup! – oae Jul 09 '13 at 10:04
  • Can you also add policies? Like `-X ours` – Roelant Oct 31 '22 at 14:19
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
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • @DotNetWise you can see `keepMine.sh` defined and used (on Windows) in http://stackoverflow.com/a/930495/6309 – VonC Sep 16 '13 at 15:51
  • Thanks. Just for the record, it could simply be `keepMine.bat` located in your `PATH` folders – DATEx2 Sep 16 '13 at 16:01