How do I move my remote git repo to another remote

2019-01-17 17:28发布

问题:

I want to move my remote git repository and all its branches to a new remote repository.

old remote = git@github.com:thunderrabbit/thunderrabbit.github.com.git

new remote = git@newhub.example.net:tr/tr.newrepo.git

回答1:

So what none of these other answers explains too well is that if you want to move all of your remote repository's branches to a new remote using Git's push mechanism, then you need local branch versions of each of your remote's branches.

You can usegit branch to create local branches. That will create a branch reference under your .git/refs/heads/ directory, where all of your local branch references are stored.

Then you can use git push with the --all and --tags option flags:

git push <new-remote> --all  # Push all branches under .git/refs/heads
git push <new-remote> --tags # Push all tags under .git/refs/tags

Note that --all and --tags can't be used together, so that's why you have to push twice.

Documentation

Here's the relevant git push documentation:

--all

Instead of naming each ref to push, specifies that all refs under refs/heads/ be pushed.

--tags

All refs under refs/tags are pushed, in addition to refspecs explicitly listed on the command line.

--mirror

Note also that --mirror can be used to push both branch and tag references at once, but the problem with this flag is that it pushes all references in .git/refs/, not just .git/refs/heads and .git/refs/tags, which may not be what you want to push to your remote.

For example, --mirror can push your remote tracking branches from your old remote(s) that are under .git/refs/remotes/<remote>/, as well as other references such as .git/refs/original/, which is a by-product of git filter-branch.



回答2:

In terminal on your local machine:

cd ~
git clone <old-remote> unique_local_name
cd unique_local_name

for remote in `git branch -r | grep -v master `; \
do git checkout --track $remote ; done

git remote add neworigin <new-remote>
git push --all neworigin


回答3:

Whole idea is to for each old remote branch do:

  • checkout
  • pull
  • push to the new remote (do not forget about tags!)

Like that:

#!/bin/bash

new_remote_link=git@newhub.example.net:tr/tr.newrepo.git
new_remote=new_remote
old_remote_link=git@github.com:thunderrabbit/thunderrabbit.github.com.git
old_remote=origin

git remote add ${old_remote} ${old_remote_link}

git pull ${old_remote}

BRANCHES=`git ls-remote --heads ${old_remote}  | sed 's?.*refs/heads/??'`

git remote add ${new_remote} ${new_remote_link}

for branch in ${BRANCHES}; do
    git checkout ${branch}
    git pull ${old_remote} ${branch}
    git push ${new_remote} ${branch} --tags
    printf "\nlatest %s commit\n" ${branch}
    git log --pretty=format:"(%cr) %h: %s%n%n" -n1
done


回答4:

You can simply change the URL for your origin repository:

git clone <old-remote-url> unique_local_name
cd unique_local_name
git pull --all

git remote set-url origin <new-remote-url>
git push --all