Recently, the warehouse has been rectified, and it is planned to use the warehouse management mode of mono repo to manage the company code.
Therefore, it is necessary to merge the original scattered warehouses, but the development requires that the submission history of the original warehouse be retained.
Scenario:
I have two warehouses, repoA and repoB. I want to merge the master of repoA into the subdirectory of repoB: project/repoA, and keep the submission history of repoA.
I've tried a lot of methods to sum up the speed.
1.git official document gives the method, but I didn't run successfully.
But I solved a problem when I tried this, because I often encounter it later.
fatal: refusing to merge unrelated histories
Solution: add: - allow unrelated histories after the command
git merge master --allow-unrelated-histories
2. The method provided in this blog post is OK if I try it:
cd repoB git remote add -f repoA_remote /fullpath/to/repoA git merge --strategy ours --no-commit repoA_remote/master mkdir -p project/repoA git read-tree --prefix=project/repoA/ -u repoA_remote/master git commit --message 'complete repoA The new directory is project/repoA'
But there is a problem that most people will mention,
Git log -- during project / repoA, only my merge submission is displayed, and the historical submission of repoA is not displayed.
The most complete version is: https://alexharv074.github.io/puppet/2017/10/04/merge-a-git-repository-and-its-history-into-a-subdirectory-of-a-second-git-repository.html
There is a paragraph about modifying the index, which may be to fix my problem above, but I didn't run the script successfully, so I gave up.
3. git patch method
Now export the patch from the original warehouse and add the relative path:
Reference documents: Merge 2 warehouses and keep complete git historical information - Zhihu (zhihu.com)
(1) export the patch first and perform it in the original warehouse (repoA)
--root is followed by the starting point you want to export. It can be HEAD or branch: master
git format-patch --root HEAD --no-stat --no-renames --full-index -o patches --src-prefix=a/project/repoA/ --dst-prefix=b/project/repoA/
(2) Apply patch (path of patch)
git apply ~/patch/patch/*.patch # The path of the patch can be modified by itself
It seems to get stuck when applying patch, but git log -- project/repoA is OK
4. git subtree
In fact, it is a more friendly version of method 2. git has a subtree tool, which can complete warehouse consolidation with one command. However, the problem is the same as method 2. Download address:
(1) Add git subtree. From GitHub SH Download: https://github.com/git/git Script path: git / contrib / subtree / git subtree sh
(2) Make a soft link in the GIT core path. The GIT core path of my machine is: / usr / libexec / git core
cd /usr/libexec/git-core ln -s /usr/local/share/git-subtree.sh git-subtree
Run git subtree again and help will come out
(3) One command to complete the migration: (enter in repoB)
git subtree add -P project/repoA /path/to/repoA.git master
5. git pull
Finally, I referred to the answers in this article and finally achieved the effect I wanted.
How do you merge two Git repositories? - Stack Overflow
(1) First modify the submission history in the local repoA, and all submissions will be added with the relative path of project/repoA
PREFIX=project/repoA #adjust this git filter-branch --index-filter ' git ls-files -s | sed "s,\t,&'"$PREFIX"'/," | GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info && mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE ' HEAD
(2) Merge local repoA into repoB
Enter in reopB:
git pull localpath-of-repoA --allow-unrelated-histories