Consider the following scenario:
I have developed a small experimental project A in its own Git repo. It has now matured, and I'd like A to be part of larger project B, which has its own big repository. I'd now like to add A as a subdirectory of B.
How do I merge A into B, without losing history on any side?
Given command is the best possible solution I suggest.
Similar to @Smar but uses file system paths, set in PRIMARY and SECONDARY:
Then you manually merge.
(adapted from post by Anar Manafov)
If both repositories have same kind of files (like two Rails repositories for different projects), you can fetch data of the secondary repository to your current repository:
and then merge it to current repository:
If your Git version is smaller than 2.9, remove
--allow-unrelated-histories
.After this, conflicts may occur. You can resolve them for example with
git mergetool
.kdiff3
can be used solely with keyboard, so 5 conflict file takes when reading the code just few minutes.Remember to finish the merge:
When you want to merge three or more projects in a single commit, do the steps as described in the other answers (
remote add -f
,merge
). Then, (soft) reset the index to old head (where no merge happened). Add all files (git add -A
) and commit them (message "Merging projects A, B, C, and D into one project). This is now the commit-id of master.Now, create
.git/info/grafts
with following content:Run
git filter-branch -- head^..head head^2..head head^3..head
. If you have more than three branches, just add as muchhead^n..head
as you have branches. To update tags, append--tag-name-filter cat
. Do not always add that, because this might cause a rewrite of some commits. For details see man page of filter-branch, search for "grafts".Now, your last commit has the right parents associated.