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?
I know it's long after the fact, but I wasn't happy with the other answers I found here, so I wrote this:
If you're trying to simply glue two repositories together, submodules and subtree merges are the wrong tool to use because they don't preserve all of the file history (as people have noted on other answers). See this answer here for the simple and correct way to do this.
This function will clone remote repo into local repo dir, after merging all commits will be saved,
git log
will be show the original commits and proper paths:How to use:
If make a little changes you can even move files/dirs of merged repo into different paths, for example:
Notices
Paths replaces via
sed
, so make sure it moved in proper paths after merging.The
--allow-unrelated-histories
parameter only exists since git >= 2.9.I had a similar challenge, but in my case, we had developed one version of the codebase in repo A, then cloned that into a new repo, repo B, for the new version of the product. After fixing some bugs in repo A, we needed to FI the changes into repo B. Ended up doing the following:
Worked a treat :)
If you want to merge
project-a
intoproject-b
:Taken from: git merge different repositories?
This method worked pretty well for me, it's shorter and in my opinion a lot cleaner.
Note: The
--allow-unrelated-histories
parameter only exists since git >= 2.9. See Git - git merge Documentation / --allow-unrelated-historiesUpdate: Added
--tags
as suggested by @jstadler in order to keep tags.I merge projects slightly manually, which allows me to avoid needing to deal with merge conflicts.
first, copy in the files from the other project however you want them.
next pull in the history
tell git to merge in the history of last fetched thing
now commit however you normally would commit