Moving Git repository content to another repository preserving history

185,407

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

Share:
185,407

Related videos on Youtube

Mario
Author by

Mario

Updated on February 24, 2022

Comments

  • Mario
    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.

  • Mario
    Mario almost 11 years
    Thanks 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
    Eric Palace almost 11 years
    Alright. 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
    Mario almost 11 years
    No, 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
    Eric Palace almost 11 years
    Have 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
    Mario almost 11 years
    I already tried it but it left the history. I want to move all of its history as well.
  • Mario
    Mario almost 11 years
    Thanks 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
    Chronial almost 11 years
    well, does repo2 also have a second branch? Or what should happen to that branch?
  • Mario
    Mario almost 11 years
    Chronial, the above comands didnt work! it didnt copied the history.
  • Mario
    Mario almost 11 years
    OK, 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
    Mario almost 11 years
    But it now left the branch. I also want to move its branch. Any idea?
  • Mario
    Mario almost 11 years
    I got that part too. You just have to checkout to branch and push it from there. Thanks :)
  • Chronial
    Chronial almost 10 years
    @ram that’s a typo – should be r1remote. fixed.
  • Alex
    Alex over 6 years
    Can confirm that this copies the entire history and tags. Very nice. Thanks.
  • Hervian
    Hervian over 6 years
    Notice 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
    Peter Krauss about 6 years
    I 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
    rickfoosusa about 6 years
    This 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
    Guillermo over 5 years
    This is the easiest way to let the original repository. It also copies all branches.
  • caot
    caot over 5 years
    If 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
    Gary Sheppard about 5 years
    Excellent 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
    Yodiz about 5 years
    Is it possible to add repo2 as a folder in repo1? If you are merging multiple repositories it would make it easier.
  • Chronial
    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
    Raad Altaie about 5 years
    you need to swtich branchs with git checkout BranchName then push branch to remote repo again with git push --all new-origin but thanks a lot
  • abhijithda
    abhijithda almost 5 years
    @Chronial Is there a way to extract/merge code of repo1 into a specific directory of repo2? Currently, it's extracting all the contents in the root of repo2?
  • Chronial
    Chronial almost 5 years
    @abhijithda The simplest solution would be to just move everything inside repo1 into a subfolder (inside repo1) before you do the merge.
  • raven
    raven about 4 years
    This also copies all of the tags.
  • Paul
    Paul about 3 years
    Useful for mentioning the remote branches and the command to obtain them
  • marcuse
    marcuse about 3 years
    To fetch all branches to local and then add the remote: stackoverflow.com/a/21189710/9247792
  • Yohanelly
    Yohanelly over 2 years
    I 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
    YaP over 2 years
    this seems not to be working if you use LFS already..
  • Nischal Revooru
    Nischal Revooru over 2 years
    This is the best way to deep clone from one git to other git
  • aaossa
    aaossa about 2 years
    It seems like your command does not exist, are you missing the actual command and --mirror is just a flag of some command?
  • Kris
    Kris about 2 years
    great answer, it works as expected.