Moving Git repository content to another repository preserving history

GitMergeRepositoryMigrate

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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 - Git

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 git@github.com: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 - Git

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 - Git

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

Solution 5 - Git

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 6 - Git

Mirroring a repository

As per @Dan-Cohn answer Mirror-push is your friend here. This is my go to for migrating repos:

1.Open Git Bash.

2.Create a bare clone of the repository.

$ git clone --bare https://github.com/exampleuser/old-repository.git

3.Mirror-push to the new repository.

$ cd old-repository.git
$ git push --mirror https://github.com/exampleuser/new-repository.git

4.Remove the temporary local repository you created in step 1.

$ cd ..
$ rm -rf old-repository.git

Reference and Credit: https://help.github.com/en/articles/duplicating-a-repository

Solution 7 - Git

I used the below method to migrate my GIT Stash to GitLab by maintaining all branches and commit history.

Clone the old repository to local.

git clone --bare <STASH-URL>

Create an empty repository in GitLab.

git push --mirror <GitLab-URL>

Solution 8 - Git

It looks like you're close. Assuming that it's not just a typo in your submission, step 3 should be cd repo2 instead of repo1. And step 6 should be git pull not push. Reworked list:

  1. git clone repo1
  2. git clone repo2
  3. cd repo2
  4. git remote rm origin
  5. git remote add repo1
  6. git pull
  7. git remote rm repo1
  8. git remote add newremote

Solution 9 - Git

What I did:

  1. Clone repository to a folder
  2. cd existing-project
  3. open here a git terminal
  4. git remote set-url origin <NEW_GIT_REPO>
  5. git push -u origin --all
  6. git push origin --tags

Solution 10 - Git

If you are migrating from a github repo to another github repo

I would recommend migrating with: git clone --bare <URL> as opposed to: git clone --mirror because if you mirror a github repo, it will not only clone the source code related things but also clone remaining pull requests and github references, causing an ! [remote rejected] error when pushing.

I am talking about this problem

The procedure I recommend:

  1. git clone --bare <Old Repo Url>
  2. cd <path to cloned repo>
  3. git push --mirror <New Repo Url>

Successfully migrated all branches, history and source code to github new repo without any errors!

Solution 11 - Git

For those using GitHub, you can import another repository (including history) via the web UI:

https://github.com/new/import

enter image description here

Solution 12 - Git

I see a lot of solutions here. The one that is a quick one liner that has worked for me in the past is to use the --mirror option. Go into the repo you want to copy first. Then assuming you have a new repo already set up, use the clone link and that's it. It will copy over all history over to your new repo.

cd repoOne

git --mirror linktonewrepo.git

Solution 13 - Git

There are a lot of complicated answers, here; however, if you are not concerned with branch preservation, all you need to do is reset the remote origin, set the upstream, and push.

This worked to preserve all the commit history for me.

cd <location of local repo.>
git remote set-url origin <url>
git push -u origin master

Solution 14 - Git

I am not very sure if what i did was right, but it worked anyways. I renamed repo2 folder present inside repo1, and committed the changes. In my case it was just one repo which i wanted to merge , so this approach worked. Later on, some path changes were done.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarioView Question on Stackoverflow
Solution 1 - GitChronialView Answer on Stackoverflow
Solution 2 - GitSergey OnishchenkoView Answer on Stackoverflow
Solution 3 - GitDan CohnView Answer on Stackoverflow
Solution 4 - GitraddevusView Answer on Stackoverflow
Solution 5 - Git MartinView Answer on Stackoverflow
Solution 6 - GitDalSoftView Answer on Stackoverflow
Solution 7 - GitRoopkumar AkubathiniView Answer on Stackoverflow
Solution 8 - GitEric PalaceView Answer on Stackoverflow
Solution 9 - GitTamás LévaiView Answer on Stackoverflow
Solution 10 - GitminchaejView Answer on Stackoverflow
Solution 11 - GitNoam ManosView Answer on Stackoverflow
Solution 12 - GitBeeWhoopView Answer on Stackoverflow
Solution 13 - GitThomasView Answer on Stackoverflow
Solution 14 - GitSrTanView Answer on Stackoverflow