Git merge subtree problems

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

Git merge subtree problems

Postby Annirak » Tue Jun 21, 2011 7:39 pm UTC

I have two branches, one was used for development and one is used for integration. Extensive development has been done in the development branch, and I want to merge a portion of it back in. The specific portion that I want to merge is all contained in one subtree of the development branch.

My directory structure is like this:

Code: Select all
[Branch A]              [Branch B]
    |                       |
    +--Dir1                 +--Dir1
    +--Dir2                 +--Dir2
        |                   |   |
        +--DirA             |   +--DirA
            |               |       |
            +--File1        |       +--File1
            +--File2        |       +--File2
                            |       +--File3
                            |       +--File4
                            +--Dir3

I want to merge Branch B/Dir2/DirA into Branch A/Dir2/DirA. I want File1 and File2 to be merged and File3 and File4 should be created in Branch A. I don't want to pick up Dir3, or any changes in Dir1.

I've tried the steps outlined by kernel.org for merging subtrees, but they fail when I do the git read-tree with:
Code: Select all
error: Entry 'Dir1/DirA/File1' overlaps with 'Dir1/DirA/File1'.  Cannot bind.


I could cherrypick the files to merge, but that seems unnecessarily convoluted for what should be a common and straight-forward problem.

Thanks,
Annirak
[X]Wife
[X]Car
[X]Rain
Now all I need is a movie to set off that pet peeve.
Annirak
 
Posts: 53
Joined: Tue Jun 26, 2007 6:55 pm UTC

Re: Git merge subtree problems

Postby Aaeriele » Wed Jun 22, 2011 12:21 am UTC

Annirak wrote:unnecessarily convoluted for what should be a common and straight-forward problem.


It's not actually as straightforward as you think, due to how Git works. Git doesn't track per-file history - it tracks per-repository history, which happens to be mappable onto files. Thus, Git's merging functionality is not designed to merge only "some" files from a given commit, because doing so would fundamentally alter the commit (and thus the cryptographic hashes used by Git as keys to link commits together).

The most straightforward way to do this if you're only merging in one direction (and that would allow you to merge the same set of directories again in the future without undue conflicts) would be to actually merge everything, and then modify the merge commit on your integration branch to not include the parts you didn't want to merge. (The reason for this is that it's easier to *add* changes than to ignore them, so it's easier to effectively ignore-via-negation.)

Something like this:

Code: Select all
git checkout BranchA
git merge --no-commit BranchB
git rm Dir3
git commit -m "Merge everything except Dir3"
Vaniver wrote:Harvard is a hedge fund that runs the most prestigious dating agency in the world, and incidentally employs famous scientists to do research.

afuzzyduck wrote:ITS MEANT TO BE FLUTTERSHY BUT I JUST SEE AAERIELE! CURSE YOU FORA!
User avatar
Aaeriele
 
Posts: 2027
Joined: Tue Feb 23, 2010 3:30 am UTC
Location: San Francisco, CA

Re: Git merge subtree problems

Postby Annirak » Wed Jun 22, 2011 7:10 pm UTC

I came up with another solution:
Code: Select all
git log Branch_B -- dir2/dirA | grep ^commit | cut -d ' ' -f 2 | xargs git merge --no-commit

But, then it turned out that the commits I was trying to replicate had been made in enormous blobs. I have to do the merge manually because the branch I wanted to merge from is in bad shape.
[X]Wife
[X]Car
[X]Rain
Now all I need is a movie to set off that pet peeve.
Annirak
 
Posts: 53
Joined: Tue Jun 26, 2007 6:55 pm UTC


Return to Coding

Who is online

Users browsing this forum: No registered users and 8 guests