-
-
Save niksumeiko/8972566 to your computer and use it in GitHub Desktop.
#!/bin/bash | |
# Sometimes you need to move your existing git repository | |
# to a new remote repository (/new remote origin). | |
# Here are a simple and quick steps that does exactly this. | |
# | |
# Let's assume we call "old repo" the repository you wish | |
# to move, and "new repo" the one you wish to move to. | |
# | |
### Step 1. Make sure you have a local copy of all "old repo" | |
### branches and tags. | |
# Fetch all of the remote branches and tags: | |
git fetch origin | |
# View all "old repo" local and remote branches: | |
git branch -a | |
# If some of the remotes/ branches doesn't have a local copy, | |
# checkout to create a local copy of the missing ones: | |
git checkout -b <branch> origin/<branch> | |
# Now we have to have all remote branches locally. | |
### Step 2. Add a "new repo" as a new remote origin: | |
git remote add new-origin [email protected]:user/repo.git | |
### Step 3. Push all local branches and tags to a "new repo". | |
# Push all local branches (note we're pushing to new-origin): | |
git push --all new-origin | |
# Push all tags: | |
git push --tags new-origin | |
### Step 4. Remove "old repo" origin and its dependencies. | |
# View existing remotes (you'll see 2 remotes for both fetch and push) | |
git remote -v | |
# Remove "old repo" remote: | |
git remote rm origin | |
# Rename "new repo" remote into just 'origin': | |
git remote rename new-origin origin | |
### Done! Now your local git repo is connected to "new repo" remote | |
### which has all the branches, tags and commits history. |
@monil1334, other developers that already have an existing repo, shall only update their origin to a new url:
# Set a "new repo" url to remote origin:
git remote set-url [email protected]:user/repo.git
Now every developer can continue to pull/push as it used to be. A code will be retrieved and sent to a new repository.
Thanks for the clear instructions!
if I already have a mirror pushed. and there are some updates to the original repository, will the same method update all the branches ?
Personally I think that it's much better to do it this way:
git clone --mirror URL git remote add NEW-REMOTE URL git push NEW-REMOTE --mirror
This works only if you have all commits pushed into the old repo. In my case, I was working on a public repo with some local commits and once I found it good, had to create a new repo of my own (but including my local commits and branches on top of the old repo). The sequence of steps mentioned by the OP helped me with that.
Works like a charm!
I want Transfer git repositories from Bonobo.Git.Server to GitHub
Than you so much
@niksumeiko Hi! I need to migrate from GH Enterprise to GH Enterprise Cloud. Is there a possibility to migrate all the closed Pull Requests as well? I can't find any GH REST API that helps(for obvious reasons I guess). Do you guys feel that if the code is merged and the base branches are already deleted why do we need to migrate those closed PR#s?
//Ashish
@niksumeiko Hi! I need to migrate from GH Enterprise to GH Enterprise Cloud. Is there a possibility to migrate all the closed Pull Requests as well? I can't find any GH REST API that helps(for obvious reasons I guess). Do you guys feel that if the code is merged and the base branches are already deleted why do we need to migrate those closed PR#s?
@astiw2, there's totally a way to do GH Enterprise migration (incl. all pull requests, issues…), but not via the git
command line. Please consult with GH enterprise support people. They serve us wonderful. We're also on their Enterprise tyre.
Personally I think that it's much better to do it this way:
git clone --mirror URL git remote add NEW-REMOTE URL git push NEW-REMOTE --mirror
@prashathsenthil You have to run these commands as below
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
Thanks very much, I do it in this way , migrate all the branches in the repository from gerrit to gitlab successfuuly.
can this work for Gitlab? I have a branch on a Company Gitlab project and I would like to see my contributions on my GitHub?
Is there a method to pull from git and migrate elsewhere such as svn for instance on a local repo with history? I've searched these internet pages a few months now and not much has come up.
git clone --bare {repo} cd {project}.git git push --mirror {new-repo}
This is the way.
Please no one do this. And unlike above comment, should use mirror the whole time.
git clone --mirror <URL to OLD repo location>
cd <New directory where OLD repo was cloned>
git remote set-url origin <URL to NEW repo location>
git push --mirror origin
@niksumeiko one question is that how would the other developers that already have existing git repo linked with the old repo URL ? how can they migrate their origin ?