jumperchen
jumperchen

Reputation: 1473

How to merge a git sub folder from another sub folder?

Here is our git folders.

folder1/xxx/a/b/c

folder2/yyy/a/b/c

In the past, we can use SVN to merge the folder1/xxx from the folder2/yyy.

Is it possible to do the same thing in Git?

Upvotes: 0

Views: 697

Answers (1)

pielgrzym
pielgrzym

Reputation: 1677

The simplest solution would be to set up both directories as submodules/subtrees (they already have some separate histories/svn repositories, right?). Then you just add to folder1 repository a remote that points to folder2 repository and you just merge the changes like it was any other remote:

# go to folder1 repository
cd /path/to/folder1repo
# or `cd folder1` if you are familiar with inline submodule editing caveats
# add a remote pointing to folder2 repo
git remote add folder2 /path/to/repo/folder2
# fetch it
git fetch folder2
# merge folder2 into folder1
git merge folder2/master

The only caveat is that you actually have a submodule and you have to commit to the superproject every time you merge those folders (since, say, folder1 will change and superproject must point to a different commit):

cd /path/to/superproject
git add folder1
git commit -m "Merged folder2 submodule into folder1"

As an alternative to a submodule you could use subtree merge of both forlders' repos, the setup is a bit more complex, but then you just do pull to update all such subtrees :)

Here is a nice subtree tutorial: http://nuclearsquid.com/writings/subtree-merging-and-you/

BTW. You can create a remote to an entirely different project and you can merge it's code with yours using simple merge :) git usage scenarios are infinite :)

Upvotes: 1

Related Questions