276

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.

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
Mario
  • 2,803
  • 3
  • 14
  • 11
  • Do 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? – Chronial Jun 28 '13 at 18:28
  • I want to move it as a part of master. – Mario Jun 28 '13 at 18:41
  • 5
    This tutorial is perfect! http://www.smashingmagazine.com/2014/05/19/moving-git-repository-new-server/ – Idemax Jul 10 '14 at 19:25
  • Possible duplicate of [How to move files from one git repo to another (not a clone), preserving history](https://stackoverflow.com/questions/1365541/how-to-move-files-from-one-git-repo-to-another-not-a-clone-preserving-history) – OhadR Jul 24 '19 at 15:06
  • not a duplicate, that's a different question – Liam Sep 10 '21 at 14:42
  • 2
    For GitHub users, you can import another repo via https://github.com/new/import – Noam Manos Oct 27 '21 at 09:43

14 Answers14

432

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.

Chronial
  • 66,706
  • 14
  • 93
  • 99
  • 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? – Mario Jun 28 '13 at 21:32
  • well, does repo2 also have a second branch? Or what should happen to that branch? – Chronial Jun 29 '13 at 05:11
  • 1
    Chronial, the above comands didnt work! it didnt copied the history. – Mario Jul 01 '13 at 17:11
  • 2
    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 Jul 01 '13 at 19:19
  • But it now left the branch. I also want to move its branch. Any idea? – Mario Jul 01 '13 at 19:22
  • I got that part too. You just have to checkout to branch and push it from there. Thanks :) – Mario Jul 01 '13 at 21:46
  • @ram that’s a typo – should be r1remote. fixed. – Chronial May 06 '14 at 14:51
  • 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 https://stackoverflow.com/a/37938036/6095334 – Hervian Oct 25 '17 at 14:53
  • 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?** https://help.github.com/articles/duplicating-a-repository/#mirroring-a-repository – Peter Krauss Apr 01 '18 at 16:37
  • 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. – Gary Sheppard Feb 15 '19 at 15:31
  • Is it possible to add repo2 as a folder in repo1? If you are merging multiple repositories it would make it easier. – Yodiz Mar 11 '19 at 11:49
  • @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. – Chronial Mar 13 '19 at 10:38
  • 3
    @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`? – abhijithda May 06 '19 at 17:52
  • 5
    @abhijithda The simplest solution would be to just move everything inside `repo1` into a subfolder (inside `repo1`) before you do the merge. – Chronial May 08 '19 at 10:34
  • Please note that `--allow-unrelated-histories` was introduced in `git-2.9.0` and older versions of `git` don't need that option (like centos 7 :D). – Samveen Aug 01 '22 at 10:17
  • Can we automate this process? Suppose i want Branch develop from repo A to have the latest code from branch develop from repo B, Is there any way to insure that the develop branch A will have all the latest code from branch develop in repo B ALWAYS? @Chronial – hotshot_02 Sep 01 '22 at 02:33
  • You might come across "Your branch is based on 'origin/master', but the upstream is gone. (use "git branch --unset-upstream" to fixup)" As a solution : git push --set-upstream origin master – codechefvaibhavkashyap Apr 06 '23 at 10:40
136

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
Chronial
  • 66,706
  • 14
  • 93
  • 99
Sergey Onishchenko
  • 6,943
  • 4
  • 44
  • 51
  • 11
    Can confirm that this copies the entire history and tags. Very nice. Thanks. – Alex Oct 05 '17 at 13:40
  • 4
    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. – caot Jan 15 '19 at 16:51
  • 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 – Raad Altaie Apr 03 '19 at 22:43
  • To fetch all branches to local and then add the remote: https://stackoverflow.com/a/21189710/9247792 – marcuse Apr 11 '21 at 13:58
  • this seems not to be working if you use LFS already.. – YaP Oct 18 '21 at 13:05
55

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.

mirekphd
  • 4,799
  • 3
  • 38
  • 59
Dan Cohn
  • 551
  • 4
  • 4
  • 1
    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. – rickfoosusa Apr 19 '18 at 23:22
  • Useful for mentioning the remote branches and the command to obtain them – Paul Mar 30 '21 at 14:51
  • Is it possible to mirror clone from one remote to another remote? without using local machine disk? – Pradeep Feb 01 '23 at 08:22
23

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

raddevus
  • 8,142
  • 7
  • 66
  • 87
22

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
Martin
  • 448
  • 3
  • 10
  • 1
    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! – Yohanelly Sep 30 '21 at 07:45
22

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

DalSoft
  • 10,673
  • 3
  • 42
  • 55
  • the question was how to "move", not "migrate". your answer seems to mirror the repo so that the original repo continues to exist? (I'm not sure what "mirror" does) – ekkis Oct 31 '22 at 22:56
  • as a question: if I wanted to put the old repo into a specific folder of the new repo, how would that be done? – ekkis Oct 31 '22 at 23:41
  • 1
    The question was how to duplicate a repo and preserve history. Your looking for --allow-unrelated-histories https://johno.com/move-directory-between-repos-with-git-history If this doesn't help please ask a separate question. – DalSoft Nov 02 '22 at 00:48
  • All went fine. Except a small issue. The default branch changed from master to the one that had latest changes. My repo was within an org and I had to mail for requesting to change it back. – jeffry copps Nov 04 '22 at 04:59
4

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!

lfurini
  • 3,729
  • 4
  • 30
  • 48
minchaej
  • 1,294
  • 1
  • 7
  • 14
4

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

https://github.com/new/import

enter image description here

Noam Manos
  • 15,216
  • 3
  • 86
  • 85
  • this approach only seems to work when the new repository doesn't already exist. in my case I want to combine several repos into one. so I can't seem to use this :( – ekkis Oct 31 '22 at 22:54
3

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
lfurini
  • 3,729
  • 4
  • 30
  • 48
Tamás Lévai
  • 325
  • 3
  • 6
2

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

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
lfurini
  • 3,729
  • 4
  • 30
  • 48
Eric Palace
  • 322
  • 2
  • 14
  • 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. – Mario Jun 28 '13 at 18:31
  • 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? – Eric Palace Jun 28 '13 at 18:46
  • 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. – Mario Jun 28 '13 at 18:51
  • 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. – Eric Palace Jun 28 '13 at 18:56
  • 1
    I already tried it but it left the history. I want to move all of its history as well. – Mario Jun 28 '13 at 19:00
0

There is one way to copy a git repo, including all history and tags and branches, that is extremely simple! I've seen a lot of complicated answers, but found one answer that's very easy. It's taken from https://dev.to/ismailpe/duplicating-a-github-repo-with-history-3223 , here it is:

  1. Create a new repository (either using git command line, or on github.com)
  2. Using a command line, clone the existing/old repository: git clone --bare https://oldRepoURL
  3. Navigate to the folder that got created cd oldRepoURL
  4. Push the new folder to the new repository git push --mirror https://newRepoURL

That's it. Extremely simple!

DeanAttali
  • 25,268
  • 10
  • 92
  • 118
-2

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.

SrTan
  • 221
  • 3
  • 12
-2

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

BeeWhoop
  • 1
  • 3
  • It seems like your command does not exist, are you missing the actual command and `--mirror` is just a flag of some command? – aaossa Mar 01 '22 at 03:58
  • unknown option: --mirror usage: git [--version] [--help] [-C ] [-c =] [--exec-path[=]] [--html-path] [--man-path] [--info-path] [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare] [--git-dir=] [--work-tree=] [--namespace=] [] – ekkis Oct 31 '22 at 22:57