Mario
Mario

Reputation: 3203

Moving Git repository content to another repository preserving history

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.

Upvotes: 316

Views: 366086

Answers (16)

0x53olution
0x53olution

Reputation: 391

Copy old repo to a new one with many branches

fetch all of the remote branches and tags from the existing repository to our local

git fetch origin

check for any missing branches

git branch -a
Output:
remotes/origin/develop
remotes/origin/master

now create local copy

git checkout -b develop origin/develop

or if you have more then a couple

git branch -r | grep -v '\->' | while read remote; do git checkout --track "$remote"; done
Now it should look like
git branch -a
Output:
develop
master

setup a new remote repo and add the new remote url to your project

git remote add new-origin [email protected]:<user>/<project>.git

push everything to the new repo

git push --all new-origin git push --tags new-origin

now delete the old origin path

git remote rm origin

rename the new one to origin

git remote rename new-origin origin

or by hand in the editor of your choice

git config --local -e

Upvotes: 2

Utkarsh
Utkarsh

Reputation: 23

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

Upvotes: 1

DeanAttali
DeanAttali

Reputation: 26363

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!

Upvotes: 1

BeeWhoop
BeeWhoop

Reputation: 1

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

Upvotes: -2

DalSoft
DalSoft

Reputation: 11107

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

Upvotes: 27

Noam Manos
Noam Manos

Reputation: 17010

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

https://github.com/new/import

enter image description here

Upvotes: 8

minchaej
minchaej

Reputation: 1844

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!

Upvotes: 6

Tam&#225;s L&#233;vai
Tam&#225;s L&#233;vai

Reputation: 425

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

Upvotes: 3

Eric Palace
Eric Palace

Reputation: 322

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

Upvotes: 1

SrTan
SrTan

Reputation: 261

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.

Upvotes: -2

Dan Cohn
Dan Cohn

Reputation: 601

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.

Upvotes: 60

 Martin
Martin

Reputation: 488

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

Upvotes: 25

Roopkumar Akubathini
Roopkumar Akubathini

Reputation: 39

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>

Upvotes: 2

Chronial
Chronial

Reputation: 70833

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.

Upvotes: 475

raddevus
raddevus

Reputation: 9087

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

Upvotes: 33

Sergey Onishchenko
Sergey Onishchenko

Reputation: 7881

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

Upvotes: 168

Related Questions