Skip to content

Instantly share code, notes, and snippets.

@CESARDELATORRE
Created October 10, 2016 19:52
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save CESARDELATORRE/388d69047418c17462b3afeba448f378 to your computer and use it in GitHub Desktop.
Save CESARDELATORRE/388d69047418c17462b3afeba448f378 to your computer and use it in GitHub Desktop.
Move your existing git repository # to a new remote repository
#!/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.
#
### OPTION 1 ###########################################################################################
# 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 git@github.com: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.
### OPTION 2 ###########################################################################################
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
(If you have multiple branches, you'll want to add git fetch before add the new origin)
@abadrahman
Copy link

abadrahman commented Dec 18, 2019

Option 2 was not working for me.
The following works

git clone --bare <url_of_old_repo>
cd <name_of_old_repo>
git push --mirror <url_of_new_repo>

@hmajerus
Copy link

hmajerus commented Mar 1, 2020

100% helpful. Thank you very much!

@johnPaslawski
Copy link

Working perfectly, thanks a lot

@elysium306
Copy link

This was EXACTLY I was looking for!!
Thanks a million

@msmyers60
Copy link

Very helpful other guides did not work, and this guide (Option 1) made sense and worked

@bekatru
Copy link

bekatru commented Jan 16, 2024

Very nice! Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment