281

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.

6

15 Answers 15

442

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.

11
  • 1
    Chronial, the above comands didnt work! it didnt copied the history.
    – Mario
    Jul 1, 2013 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 1, 2013 at 19:19
  • 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
    – Hervian
    Oct 25, 2017 at 14:53
  • 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 6, 2019 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 8, 2019 at 10:34
146

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
5
  • 12
    Can confirm that this copies the entire history and tags. Very nice. Thanks.
    – Alex
    Oct 5, 2017 at 13:40
  • 5
    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, 2019 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 Apr 3, 2019 at 22:43
  • 1
    To fetch all branches to local and then add the remote: stackoverflow.com/a/21189710/9247792
    – marcuse
    Apr 11, 2021 at 13:58
  • this seems not to be working if you use LFS already..
    – YaP
    Oct 18, 2021 at 13:05
57

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.

3
  • 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. Apr 19, 2018 at 23:22
  • Useful for mentioning the remote branches and the command to obtain them
    – Paul
    Mar 30, 2021 at 14:51
  • Is it possible to mirror clone from one remote to another remote? without using local machine disk?
    – Pradeep
    Feb 1 at 8:22
24

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

4
  • 2
    This is the easiest way to let the original repository. It also copies all branches.
    – Guillermo
    Oct 11, 2018 at 9:42
  • 2
    This also copies all of the tags.
    – raven
    Mar 28, 2020 at 8:27
  • 1
    This is the best way to deep clone from one git to other git Nov 10, 2021 at 17:40
  • 1
    great answer, it works as expected.
    – Kris
    Apr 5, 2022 at 17:03
23

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
1
  • 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, 2021 at 7: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

4
  • 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, 2022 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, 2022 at 23:41
  • 1
    The question was how to duplicate a repo and preserve history. Your looking for --allow-unrelated-histories johno.com/move-directory-between-repos-with-git-history If this doesn't help please ask a separate question.
    – DalSoft
    Nov 2, 2022 at 0: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. Nov 4, 2022 at 4:59
5

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!

5

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

https://github.com/new/import

enter image description here

1
  • 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, 2022 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
0
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
5
  • 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, 2013 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? Jun 28, 2013 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, 2013 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. Jun 28, 2013 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, 2013 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!

0

Some of these methods dint work out for me as the new repo where I wanted to shift contents had one initial commit already made with some changes when setting up new repo (files like README etc.)

Steps that worked for me:

1.In github remote of repo1 create a new branch from master(say snapshot).

2.Mirror repo1 in local

 git clone --mirror **url-of-repo1**

3.Navigate to repo1 and then set new remote to the repo2:

git remote remove origin
git remote add origin **url-of-repo2**

4.Push all tracks and tags to new remote. master will get rejected but rest all are will be fine:

git push --all
git push --tags

5.Clear out mirrored repo from local:

rm -rf repo1.git 

6.Clone the repo2 now in local, then navigate inside:

git clone **url-of-repo2.git**
cd repo2

7.Checkout to snapshot branch then:

git checkout --track origin/snapshot 

8.Checkout to master and then merge with snapshot:

git merge snapshot  --allow-unrelated-histories

9.Fix conflicts and add commit for modifications to complete merge:

git add README.md 
git commit -m "merge track to master"

10.Check logs to see all commits present properly. Then push to remote:

git push origin master
-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.

-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

2
  • 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 1, 2022 at 3:58
  • unknown option: --mirror usage: git [--version] [--help] [-C <path>] [-c <name>=<value>] [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path] [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare] [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>] <command> [<args>]
    – ekkis
    Oct 31, 2022 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.