Home > Enterprise >  Git: How to move repository in another repository without losing the history?
Git: How to move repository in another repository without losing the history?

Time:12-12

I would like to move those two repositorys into the first one, how could I do this without losing the history of commits? Thanks! This is what Im trying to do

I tried to search info about the issue but all I found is how to clone the repositorys, but with cloning I will loose the history if I just clone them into the new repo and then delete the old one =/ what I want to do is to move them somehow

CodePudding user response:

Say you have a local clone of the old repo. On this local, add the new repo as a new remote:

git remote add new-repo url-to-new-repo

Then, for local branches that you have, you can do

git push new-repo local-branch

And for branches that are in the old repo but you do not have locally:

git push new-repo old-repo/some-branch:refs/heads/some-branch

old-repo might be origin, of course.

CodePudding user response:

My strategy in this case would be to use a git merge --allow-unrelated-histories. The merged repo will have two unrelated initial commits, which might seem weird when you look at the history, but the histories of the two repos will be contained in the merged repo, and you don't have to rewrite any history, just merge them.

Now, you said you want to move the files from one repo into a subdirectory. Do that first, before the merge. Then merge with unrelated histories, and push the results.

Here's a complete annotated recipe to do that:

# Start in a fresh sandbox - this is important, I'm assuming later you've done so,
# and it's easy to throw away and start fresh if anything goes wrong.
git clone URL/repo1.git
cd repo1
# Add a second remote in this sandbox, pointing to the second repo
git remote add r2 URL/repo2.git
git fetch r2
# Make and checkout a branch r2-main tracking the second remote's main
git checkout -b r2-main r2/main
# in this branch, move everything to folder repo2
mkdir repo2
git mv $(git ls-tree --name-only HEAD .) repo2
# commit (but don't push!)
git commit -m'Moving repo2 contents to folder repo2/ ahead of big merge'
# Go back to repo1's main - this local branch tracking it was created by the initial clone.
git checkout main
# And do the big merge
git merge --allow-unrelated-histories -Xfind-renames=100% r2-main
# finally, if everything looks good, you can push to origin
git push

A note about -Xfind-renames=100%: by default, Git is trying to identify renamed files, and if you have filed that share 50% or more of their contents between your two repos, it'll assume that was a rename. That is often a useful thing, but here you know your histories are unrelated so we don't want that. I didn't find a way to block that detection for identical files (even -Xno-renames did not block it in my tests) but -Xfind-renames=100% says they have to be 100% identical to be considered renames, so it should avoid the problem in most cases. That option is often not necessary, but it prevents surprises so I prefer including it in the recipe.

  • Related