Is it possible to merge only the changes for a sub-directory from a local git branch to a remote git branch or is it "all or nothing"?

For example, I have:

branch-a
 - content-1
 - dir-1
   - content-2

and

branch-b
 - content-1
 - dir-1
   - `content-2

I only want to merge the contents of branch-a dir-1 with the contents of branch-b dir-1.

share|improve this question
1  
I think this is a duplicate of: stackoverflow.com/questions/449541/… – Karl Voigtland Jul 31 '09 at 21:20
2  
Not a duplicate. – Alex R Mar 26 '15 at 11:12

create git repo contains both branch-a and branch-b

git checkout branch-a
git diff branch-b dir-1 > a.diff
patch -R -p1 < a.diff
share|improve this answer
13  
This answer needs more information. What of this is actual code vs comments? – qodeninja Oct 4 '13 at 21:35

Just as an alternative to the SO question "How do you merge selective files with git-merge?", I just found this GitHub thread which could be more adapted for merging a whole subdirectory, based on git read-tree:

  • My repository => cookbooks
    My repository target directory => cookbooks/cassandra

  • Remote repository => infochimps
    Remote repository source I want merged into cookbooks/cassandra => infochimps/cookbooks/cassandra

Here are the commands I used to merge them

  • Add the repository and fetch it
git remote add -f infochimps git://github.com/infochimps/cluster_chef.git
  • Perform the merge
git merge -s ours --no-commit infochimps/master
  • Merge only infochimps/cookbooks/cassandra into cassandra
git read-tree --prefix=cassandra/ -u infochimps/master:cookbooks/cassandra
  • Commit the change
 git commit -m 'merging in infochimps cassandra'

Addendum

It's bizarre,[edit me] — but the read-tree step can possibly fail like this:

error: Entry 'infochimps/cookbooks/cassandra/README' overlaps with 'cookbooks/cassandra/README'. Cannot bind.

... even when both files are identical. This might help:

git rm -r cassandra
git read-tree --prefix=cassandra/ -u infochimps/master:cookbooks/cassandra

But off course, verify manually that this does what you want.

share|improve this answer
    
Awesome, this helped me out so much! – Henrik Hartz Mar 13 '11 at 20:00
3  
Thanks, this is awesome! Is the colon-syntax, i.e. infochimps/master:cookbooks/cassandra documented somewhere? I could not find it in git help read-tree ... – Martin Sep 7 '12 at 13:11
3  
@Martin git-scm.com/docs/git-rev-parse#_specifying_revisions look for <rev>:<path>, e.g. HEAD:README, :README, master:./README – VonC Sep 7 '12 at 13:43
6  
The git read-tree step fails for me: error: Entry 'foo/bar/baz.php' overlaps with 'bar/baz.php'. Cannot bind. – Weston Ruter Apr 19 '13 at 19:07
1  
@VonC but no, I need the history. That answer does not account for the case where there are local modifications to the files within the tree. So it needs to merge. – Weston Ruter Apr 19 '13 at 22:36

For my example, assume you have a branch 'source' and a branch 'destination' which both reflect upstream versions of themselves (or not, if local only) and are pulled to the latest code. Let's say I want the subdirectory in the repo called newFeature which only exists in the 'source' branch.

git checkout destination
git checkout source newFeature/
git commit -am "Merged the new feature from source to destination branch."
git pull --rebase
git push

Significantly less convoluted than everything else I've seen and this worked perfectly for me, found here.

Note that this isn't a 'real merge' so you won't have the commit information about newFeature in the destination branch, just the modifications to the files in that subdirectory. But since you're presumably going to merge the entire branch back over later, or discard it, that might not be an issue.

share|improve this answer
    
Worked perfectly – VladRia Jun 20 '17 at 9:07
    
Does it preserve the history? – Bibrak Nov 1 '17 at 21:52

I got this from a forum thread at Eclipse and it worked like a charm:

git checkout source-branch
git checkout target-branch <directories-or-files-you-do-**NOT**-want> 
git commit
git checkout target-branch
git merge source-branch
share|improve this answer
1  
I tried this and ended up with a directory from the source-branch in the target-branch that I did not want. Despite having it specified with the set of dirs I did not want on that 2nd command. – marathon Feb 21 '17 at 9:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.