简体   繁体   中英

How to combine two git repos into one repo in parent folder?

I have two repos in the same parent folder that I want to combine in the parent folder, and if possible keep history without minimal diffs.

parent/
--code1/.git
--code2/.git

I would like:

parent/.git
--code1
--code2

Is it possible to do this and keep the history for both? Or at least code1 folder? Unfortunately there are some same-named branches in both repos. What would be ideal is to have both histories in.git by commit date, but I haven't found an easy solution (one complication is that git mv shows diffs from before/after commits with all files changed, but we can live with that).

You just want to merge two different repository (first and second) in a new one while keeping both git history:

Create a new git repository and make an initial commit:

mkdir newRepository
git init
touch .gitignore
git add .gitignore
git commit .gitignore -m 'init'

Fetch and merge the first:

git remote add first pathTo/first
git fetch first
git merge first/master

Fetch and merge the second:

git remote add second pathTo/second
git fetch second
git merge second/master

You should be able to utilize the git pull method.

Do the following:

$ git init
$ git pull code1
$ git pull code2

This should create a merge.. Accept the merge (fix conflicts if any)..

To pull branches into the parent git repo, you can do the following:

$ git checkout -b branchName
$ git pull code1 branchName

EDIT: I had missed the part with you still wanting to keep the code1 and code2 folders. This is not really possible when you merge the two repositories, as it is doing what it says: merging them together, into the parent folder.. You would manually have to move source files into code1 and code2 afterwards and make further commits.

I was running into the same issue and came across this post. I eventually solved it by restructuring each repo before merging them together

repo1*content -> repo1\repo1newname*content repo2*content -> repo2\repo2newname*content

each on their own branches cut off of HEAD/main

then I added both repos as remotes to the new repo and pulled from those branches

so it turned out as

newrepo
..\repo1newname..\repo2newname

The restructuring added a renaming commit for each file, so the commit history of each file was retained, just with one extra commit for the renaming and another for the merging.

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM