首页 文章

将本地Git仓库推送到新的远程包括所有分支和标签

提问于
浏览
422

我有一个本地Git仓库,我想推送到一个新的远程仓库(在Beanstalk上设置全新的仓库,如果这很重要) . 我的本地仓库有一些分支和标签,我想保留我的所有历史记录 . 看起来我基本上只需要执行git push,但只上传master分支 . 如何推送所有内容,以便在遥控器上获得我当地仓库的完整副本?

11 回答

  • 80

    要推送all your branches,请使用(将REMOTE替换为遥控器的名称,例如"origin"):

    git push REMOTE '*:*'
    git push REMOTE --all
    

    要推all your tags

    git push REMOTE --tags
    

    最后,我认为您可以在一个命令中执行以下操作:

    git push REMOTE --mirror
    

    但是,除了--mirror之外,还会推送你的遥控器,所以这可能不是你想要的 .

  • 1

    在像我这样的情况下,你获得了一个回购,现在正在将远程原点切换到另一个回购,一个新的空回购......

    所以你有你的repo和里面的所有分支,但是你仍然需要检查那些分支以获得 git push --all 命令以实际推送它们 .

    你应该在推动之前执行此操作:

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

    其次是

    git push --all
    
  • 4

    这是对我所处的情况更好的另一种看法 . 它解决了你有多个遥控器的问题,想要将远程 source 中的所有分支克隆到远程 destination 但不必全部检查它们预先 .

    (我在Daniel的解决方案中遇到的问题是,如果我之前已经检查过它,它将拒绝从 source 远程检出跟踪分支,即它在推送之前不会更新我的本地分支)

    git push destination +refs/remotes/source/*:refs/heads/*
    

    注意:如果您不使用直接CLI,则必须转义星号:git push destination refs / remotes / source / \ *:refs / heads / \ * @mattalxndr

    这会将远程 source 中的所有分支推送到 destination 中的头部分支,可能会执行非快进推送 . 您仍然需要单独推送标签 .

  • 6

    git-push 的联机帮助页值得一读 . 结合this website我在_1437385中写了以下内容:

    [remote "origin"]
        url = …
        fetch = …
        push = :
        push = refs/tags/*
    

    push = : 表示"push any 'matching' branches (i.e. branches that already exist in the remote repository and have a local counterpart)",而 push = refs/tags/* 表示"push all tags" .

    所以现在我只需要运行 git push 来推送所有匹配的分支和所有标签 .

    是的,这不是OP想要的(所有分支都必须已经存在于远程端),但是对于那些在搜索“我如何在同一时间推送分支和标签”时发现这个问题的人可能会有所帮助时间” .

  • 0

    如果目的地为空,这是我找到的最简洁的方法 . 切换到空文件夹,然后:

    # Note the period for cwd >>>>>>>>>>>>>>>>>>>>>>>> v
    git clone --bare https://your-source-repo/repo.git .
    git push --mirror https://your-destination-repo/repo.git
    

    适当地用 https://... 代替 file:///your/repo 等 .

  • 7

    在我的情况下,有用的是什么 .

    git push origin --all
    
  • 11

    我发现上面的答案仍然有一些不清楚的东西,这会误导用户 . 首先,确定 git push new_origin --allgit push new_origin --mirror 不能复制所有原始分支,它只是将您当地存在的分支复制到您的new_origin .

    以下是我测试的两种有用的方法:

    1,复制克隆裸仓库 . git clone --bare origin_url ,然后输入文件夹,和 git push new_origin_url --mirror . 通过这种方式,你也可以使用 git clone --mirror origin_url--bare--mirror 将下载一个裸仓库,不包括工作空间 . 请参考this

    2,如果你有一个使用 git clone 的git仓库,这意味着你有裸仓库和git工作区,你可以使用 git remote add new_origin new_origin_url ,然后 git push new_origin +refs/remotes/origin/\*:refs/heads/\* ,然后 git push new_origin --tags

    通过这种方式,你将得到一个额外的头部分支,这是没有意义的 .

  • 2

    推送分支和标签(但不是遥控器):

    git push origin 'refs/tags/*' 'refs/heads/*'
    

    这相当于为 git push 组合 --tags--all 选项,git似乎不允许这样做 .

  • 668

    基于@Daniel回答我做了:

    for remote in \`git branch | grep -v master\`
    do 
        git push -u origin $remote
    done
    
  • 115

    我发现这些似乎都不适合我 . 随意将其烧死,但由于某种原因无法让其他选项正常工作 .

    预期的结果是一个“克隆”到另一个远程(即从Github到另一个提供者)的repo:

    • 所有分支都在新远程上创建

    • 所有分支历史记录都在新远程上创建

    • (我试过的每个解决方案都错过了这个)

    • 所有标签都是在新遥控器上创建的

    • 来源移动(给定)

    • 非破坏性(暂停--mirror选项)

    我看到的主要问题是所有远程分支都没有在新的远程中重新创建 . 如果一个命令,新遥控器没有分支历史记录(即执行 git checkout branch; git log 不会显示预期的分支提交) .

    我注意到 git checkout -b branchnamegit checkout branchname 不同(后者是我需要的) . 我注意到 git checkout --track branchname 似乎没有拉动分支历史记录 .

    My Solution (基于powershell):

    Function Git-FetchRemoteBranches {
    $originalbranch = (git symbolic-ref HEAD).split("/")[-1]
    
    Foreach ($entry in (git branch -r)) {
    
    If ($entry -like "*->*") {
      $branch = $entry.split("->")[2].split("/")[1]
    }
      else {$branch = $entry.split("/")[1]}
    
    Write-Host "--Trying git checkout " -NoNewline
    Write-Host "$branch" -Foreground Yellow
    
    git checkout $branch
    
    Remove-Variable branch -Force
    
    ""}
    
    #Switch back to original branch, if needed
    If ( ((git symbolic-ref HEAD).split("/")[-1]) -ne $originalbranch) {
    "Switching back to original branch"
    git checkout $originalbranch
    Remove-Variable originalbranch -Force
    }
    }
    
    git clone http://remoterepo
    cd remoterepo
    Git-FetchRemoteBranches
    git remote add newremote
    git push newremote --all
    git push newremote --tags #Not sure if neeeded, but added for good measure
    
  • 0

    Mirroring a repository

    创建存储库的裸克隆 .

    git clone --bare https://github.com/exampleuser/old-repository.git
    

    镜像推送到新存储库 .

    cd old-repository.git
    git push --mirror https://github.com/exampleuser/new-repository.git
    

    删除您在步骤1中创建的临时本地存储库 .

    cd ..
    rm -rf old-repository.git
    

    Mirroring a repository that contains Git Large File Storage objects

    创建存储库的裸克隆 . 将示例用户名替换为拥有存储库的个人或组织的名称,并将示例存储库名称替换为您要复制的存储库的名称 .

    git clone --bare https://github.com/exampleuser/old-repository.git
    

    导航到刚刚克隆的存储库 .

    cd old-repository.git
    

    拉入存储库的Git Large File Storage对象 .

    git lfs fetch --all
    

    镜像推送到新存储库 .

    git push --mirror https://github.com/exampleuser/new-repository.git
    

    将存储库的Git Large File Storage对象推送到镜像 .

    git lfs push --all https://github.com/exampleuser/new-repository.git
    

    删除您在步骤1中创建的临时本地存储库 .

    cd ..
    rm -rf old-repository.git
    

    以上说明来自Github帮助:https://help.github.com/articles/duplicating-a-repository/

相关问题