1

I have two git repositories, say project1 and project2, and project2 is located in a subfolder of project1 :

/project1/.git (with contents of project2/ ignored)
/project1/project2/.git 

I would like to know how I can merge project2 into project1, with the same folder structure, but keeping the history of project2 ?

I there a simple solution for that ?

476rick
  • 2,764
  • 4
  • 29
  • 49
ed5978
  • 107
  • 8
  • 5
    Possible duplicate of [Merge multiple git repositories into one, keeping branches history](http://stackoverflow.com/questions/26502970/merge-multiple-git-repositories-into-one-keeping-branches-history) – dmlittle Sep 25 '16 at 17:36
  • In my case, the two git repos are not at the same folder level – ed5978 Sep 25 '16 at 17:40
  • Are there branches in project2 and if yes, are you concerned with the history of those branches as well? – Leon Sep 25 '16 at 18:03
  • Yes, there are branches in project2. However, only one of them is really of interest for merging project2 into project1. – ed5978 Sep 25 '16 at 19:06
  • This was discussed here: http://stackoverflow.com/q/6426247/2303202 I guess what you need is what is written after EDIT2 there: "git merge -sours --no-commit ... && git read-tree --prefix=... -u ... && git commit" – max630 Sep 27 '16 at 06:57

2 Answers2

2

I would just first move everything inside project2 into a subdirectory and then simply merge thus modified repository of project2 into project1.

Assuming that you want to merge the contents and history of the branch PROJECT2_BRANCH in project2 as a submodule into the branch PROJECT1_BRANCH in project1:

git clone /project1/project2 ~/tmp_project2
cd ~/tmp_project2
git checkout PROJECT2_BRANCH
mkdir project2
# The following line requires bash, it will not work in a POSIX shell.
# It basically "git mv"s into project2 everything except .git and project2
git ls-tree --name-only -z HEAD|{ while read -d '' f; do git mv "$f" project2; done; }
git commit -m "Moved everything into a new subdirectory project2"

git clone /project1 ~/project1_merged_with_project2
cd ~/project1_merged_with_project2
git checkout PROJECT1_BRANCH
git remote add proj2 ~/tmp_project2
git pull proj2 PROJECT2_BRANCH

git remote remove proj2
rm -rf ~/tmp_project2
Leon
  • 31,443
  • 4
  • 72
  • 97
0

Finally, I moved project2 into a temporary folder, then :

git subtree add -P project2 ~/tmp_project2 PROJECT2_BRANCH

did the job very well.

ed5978
  • 107
  • 8