【问题标题】:How do I move my remote git repo to another remote git repo?如何将我的远程 git 存储库移动到另一个远程 git 存储库?
【发布时间】:2013-01-04 03:06:41
【问题描述】:

我想将我的远程 git 存储库及其所有分支移动到新的远程存储库。

旧遥控器 = git@github.com:thunderrabbit/thunderrabbit.github.com.git

新遥控器 = git@newhub.example.net:tr/tr.newrepo.git

【问题讨论】:

  • 我知道这是一个自我回答的问题,但问题本身的质量仍然很低。也许尝试添加一些您尝试过的想法或您在提出your answer 之前查看过的文档。
  • 仅供参考,umläute's answer 不太正确,请参阅my comment

标签: git git-remote


【解决方案1】:

在本地机器上的终端中:

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

【讨论】:

  • 最后一行 (git push --all gitlab) 是错字吗?不应该是新的origin,不是gitlab吗?
  • 啊,是的,看起来像。谢谢!
  • FYI,如果要推送标签,还需要使用git push --tags(不能与--all同时使用)。此外,您也可以使用git branch,而不是使用git checkout,这可能会更快,因为您不会在工作副本中切换文件。
【解决方案2】:

所以这些其他答案都没有很好地解释的是,如果你想 使用 Git 的 push 将所有远程存储库的分支移动到新的远程 机制,那么您需要每个遥控器的本地分支版本 分支。

您可以使用git branch 创建本地分支。这将创建一个分支 在您的.git/refs/heads/ 目录下引用,您本地的所有 存储分支引用。

然后您可以将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

注意--all--tags不能一起使用,所以你必须 按两次。

文档

这是相关的git push documentation

--all

而不是命名要推送的每个 ref,而是指定所有 ref 下 refs/heads/被推送。

--tags

除了显式引用规范之外,refs/tags 下的所有引用都被推送 列在命令行中。

--mirror

还请注意,--mirror 可用于将分支和标签引用推送到 一次,但这个标志的问题在于它会将 所有引用 推入 .git/refs/,不只是.git/refs/heads.git/refs/tags,这可能不是 您想要推送到遥控器的内容。

例如,--mirror 可以从旧的推送您的远程跟踪分支 .git/refs/remotes/&lt;remote&gt;/ 下的遥控器以及其他 引用,例如 .git/refs/original/,它是 git filter-branch 的副产品。

【讨论】:

    【解决方案3】:

    整个想法是为每个旧的远程分支做:

    • 结帐
    • 推送到新的遥控器(不要忘记标签!)

    这样:

    #!/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】:

      您可以简单地更改您的 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
      

      【讨论】:

      • 这不太正确,如果您没有每个远程分支的本地分支版本,那么它们将不会被推送到新的远程。只有.git/refs/heads/ 下的本地分支才会被推送。
      猜你喜欢
      • 1970-01-01
      • 2020-11-20
      • 1970-01-01
      • 2011-09-12
      • 2013-04-13
      • 2016-10-15
      • 2012-05-09
      相关资源
      最近更新 更多