Moving Git repository content to another repository preserving history
Solution 1
I think the commands you are looking for are:
cd repo2
git checkout master
git remote add r1remote **url-of-repo1**
git fetch r1remote
git merge r1remote/master --allow-unrelated-histories
git remote rm r1remote
After that repo2/master
will contain everything from repo2/master
and repo1/master
, and will also have the history of both of them.
Solution 2
Perfectly described here https://www.smashingmagazine.com/2014/05/moving-git-repository-new-server/
First, we have to fetch all of the remote branches and tags from the existing repository to our local index:
git fetch origin
We can check for any missing branches that we need to create a local copy of:
git branch -a
Let’s use the SSH-cloned URL of our new repository to create a new remote in our existing local repository:
git remote add new-origin [email protected]:manakor/manascope.git
Now we are ready to push all local branches and tags to the new remote named new-origin:
git push --all new-origin
git push --tags new-origin
Let’s make new-origin the default remote:
git remote rm origin
Rename new-origin to just origin, so that it becomes the default remote:
git remote rename new-origin origin
Solution 3
If you're looking to preserve the existing branches and commit history, here's one way that worked for me.
git clone --mirror https://github.com/account/repo.git cloned-repo
cd cloned-repo
git push --mirror {URL of new (empty) repo}
# at this point only remote cloned-repo is correct, local has auto-generated repo structure with folders such as "branches" or "refs"
cd ..
rm -rf cloned-repo
git clone {URL of new (empty) repo}
# only now will you see the expected user-generated contents in local cloned-repo folder
# note: all non-master branches are avaialable, but git branch will not show them until you git checkout each of them
# to automatically checkout all remote branches use this loop:
for b in `git branch -r | grep -v -- '->'`; do git branch --track ${b##origin/} $b; done
Now, suppose you want to keep the source and destination repos in sync for a period of time. For example, there's still activity within the current remote repo that you want to bring over to the new/replacement repo.
git clone -o old https://github.com/account/repo.git my-repo
cd my-repo
git remote add new {URL of new repo}
To pull down the latest updates (assuming you have no local changes):
git checkout {branch(es) of interest}
git pull old
git push --all new
NB: I have yet to use submodules, so I don't know what additional steps might be required if you have them.
Solution 4
Simplest approach if the code is already tracked by Git then set new repository as your "origin" to push to.
cd existing-project
git remote set-url origin https://clone-url.git
git push -u origin --all
git push origin --tags
Solution 5
This worked to move my local repo (including history) to my remote github.com repo. After creating the new empty repo at GitHub.com I use the URL in step three below and it works great.
git clone --mirror <url_of_old_repo>
cd <name_of_old_repo>
git remote add new-origin <url_of_new_repo>
git push new-origin --mirror
I found this at: https://gist.github.com/niksumeiko/8972566
Related videos on Youtube
Mario
Updated on February 24, 2022Comments
-
Mario about 2 years
I am trying to move only the contents of one repository (
repo1
) to another existing repository (repo2
) using the following commands:git clone repo1 git clone repo2 cd repo1 git remote rm origin git remote add repo1 git push
But it's not working. I reviewed a similar post, but I only found one moving the folder, not the contents.
-
Chronial almost 11 yearsDo you want to have the contents of repo1 as a branch on repo2, or as part of master, so the folders of both repos coexist in your working dir?
-
Mario almost 11 yearsI want to move it as a part of master.
-
Idemax almost 10 yearsThis tutorial is perfect! smashingmagazine.com/2014/05/19/…
-
OhadR almost 5 yearsPossible duplicate of How to move files from one git repo to another (not a clone), preserving history
-
Liam over 2 yearsnot a duplicate, that's a different question
-
Noam Manos over 2 yearsFor GitHub users, you can import another repo via github.com/new/import
-
-
Mario almost 11 yearsThanks Eric, but in step 3 i want to remove the link of the original repository to avoid making any remote changes.. as i want to move contents from repo1 to repo2.
-
Eric Palace almost 11 yearsAlright. I've edited my answer to try to reflect that. Just to make sure, you're trying to make a repository, repo2, that is a copy of repo1 but does not keep either repo1 or origin as remotes?
-
Mario almost 11 yearsNo, I am not trying to make a new repo "repo2". Actually, i have an existing repo2 which have other contents init. Now i want to move all of repo1 contents into repo2 with the history. I will keep the repo1 empty as it is in the server.
-
Eric Palace almost 11 yearsHave you considered not using git? It seems like you could probably just copy the files (via a gui or scp or ftp) and manually add them to your git repo. It might not be as efficient, but it's likely simpler.
-
Mario almost 11 yearsI already tried it but it left the history. I want to move all of its history as well.
-
Mario almost 11 yearsThanks Chronial, i just want to make sure that the repo1 has also one branch and what if i want to move that branch also to repo2?
-
Chronial almost 11 yearswell, does repo2 also have a second branch? Or what should happen to that branch?
-
Mario almost 11 yearsChronial, the above comands didnt work! it didnt copied the history.
-
Mario almost 11 yearsOK, now it worked. I used the following command;cd repo2, > git remote rm origin > git remote add origin url-of-repo1, > git fetch r1remote > git push origin master
-
Mario almost 11 yearsBut it now left the branch. I also want to move its branch. Any idea?
-
Mario almost 11 yearsI got that part too. You just have to checkout to branch and push it from there. Thanks :)
-
Chronial almost 10 years@ram that’s a typo – should be r1remote. fixed.
-
Alex over 6 yearsCan confirm that this copies the entire history and tags. Very nice. Thanks.
-
Hervian over 6 yearsNotice that the behavior seem to have changed - now an error is thrown unless an option is provided to the merge command, stating that it is ok to merge 2 unrelated histories. See stackoverflow.com/a/37938036/6095334
-
Peter Krauss about 6 yearsI need to destroy de original repo1, and I am supposing that with this recipe here have no problem with the life of
url-of-repo1
... But, if I used another recipe? Cloning with modern git with--mirror
(as in the recipe of the link) will be the same? help.github.com/articles/duplicating-a-repository/… -
rickfoosusa about 6 yearsThis worked simply and easily for me. I think it should be the checked answer. If you have 60 branches, this is the way to go.
-
Guillermo over 5 yearsThis is the easiest way to let the original repository. It also copies all branches.
-
caot over 5 yearsIf the new-origin has no any commit, it works well. However, if there is any commit in the new-origin, the way mentioned here won't word as expected.
-
Gary Sheppard about 5 yearsExcellent answer. RE the comments on branches, for each branch you want to migrate, I think you have to 1) create and checkout the branch in your new repo and then 2) run the answer's
git merge
command, substituting the name of the branch you're migrating. Maybe this could be scripted, but I only had a few branches to migrate, so I didn't bother scripting it. -
Yodiz about 5 yearsIs it possible to add repo2 as a folder in repo1? If you are merging multiple repositories it would make it easier.
-
Chronial about 5 years@Yodiz the easiest way to achieve that is to just move everything in repo2 into a subfolder and commit that. Then follow the steps in the answer.
-
Raad Altaie about 5 yearsyou need to swtich branchs with
git checkout BranchName
then push branch to remote repo again withgit push --all new-origin
but thanks a lot -
abhijithda almost 5 years@Chronial Is there a way to extract/merge code of
repo1
into a specific directory ofrepo2
? Currently, it's extracting all the contents in the root ofrepo2
? -
Chronial almost 5 years@abhijithda The simplest solution would be to just move everything inside
repo1
into a subfolder (insiderepo1
) before you do the merge. -
raven about 4 yearsThis also copies all of the tags.
-
Paul about 3 yearsUseful for mentioning the remote branches and the command to obtain them
-
marcuse about 3 yearsTo fetch all branches to local and then add the remote: stackoverflow.com/a/21189710/9247792
-
Yohanelly over 2 yearsI made the mistake of removing original-repo remote and adding the new-repo remote, without copying the commit history. So all the changes I made on the original repo were missing in the new repo. This worked like a charm, new repo has all my changes. Cheers mate!
-
YaP over 2 yearsthis seems not to be working if you use LFS already..
-
Nischal Revooru over 2 yearsThis is the best way to deep clone from one git to other git
-
aaossa about 2 yearsIt seems like your command does not exist, are you missing the actual command and
--mirror
is just a flag of some command? -
Kris about 2 yearsgreat answer, it works as expected.