-
-
Save jpgrace/37f1b1fc2410718bc35f3bc669579862 to your computer and use it in GitHub Desktop.
Moving git repository and all its branches, tags to a new remote repository keeping commits history
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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> | |
git branch -a | grep -v HEAD | perl -ne 'chomp($_); s|^\*?\s*||; if (m|(.+)/(.+)| && not $d{$2}) {print qq(git branch --track $2 $1/$2\n)} else {$d{$_}=1}' | csh -xfs | |
# 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 | |
### Step 5. Remove local branches. | |
git branch | grep -v "master" | xargs git branch -D | |
### Done! Now your local git repo is connected to "new repo" remote | |
### which has all the branches, tags and commits history. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment