简体   繁体   English

如何将我的远程git仓库移动到另一个远程git仓库?

[英]How do I move my remote git repo to another remote git repo?

I want to move my remote git repository and all its branches to a new remote repository. 我想将我的远程git存储库及其所有分支移动到新的远程存储库。

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

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

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

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. 所以这些其他答案中没有一个解释得太好了,如果你想使用Git的push机制将所有远程存储库的分支移动到新的远程, 那么你需要每个远程分支的本地分支版本。

You can use git branch to create local branches. 您可以使用git branch创建本地分支。 That will create a branch reference under your .git/refs/heads/ directory, where all of your local branch references are stored. 这将在.git/refs/heads/目录下创建一个分支引用,其中存储了所有本地分支引用。

Then you can use git push with the --all and --tags option flags: 然后你可以使用git push--all--tags选项标志:

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. 请注意--all--tags不能一起使用,这就是你必须推两次的原因。

Documentation 文档

Here's the relevant git push documentation : 这是相关的git push文档

 --all 

Instead of naming each ref to push, specifies that all refs under refs/heads/ be pushed. 而不是将每个引用命名为push,指定refs/heads/下的所有引用都被推送。

 --tags 

All refs under refs/tags are pushed, in addition to refspecs explicitly listed on the command line. 除了在命令行中明确列出的refspec之外,还会推送refs/tags下的所有引用。

--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. 还要注意--mirror可以用来同时推送分支和标记引用,但是这个标志的问题是它推送.git/refs/ 所有引用 ,而不仅仅是.git/refs/heads.git/refs/tags ,可能不是你想要推送到你的遥控器。

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 . 例如, - --mirror可以从.git/refs/remotes/<remote>/下的旧远程--mirror推送远程跟踪分支,以及其他引用,例如.git/refs/original/ ,是git filter-branch的副产品。

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

You can simply change the URL for your origin repository: 您只需更改origin存储库的URL:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM