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

时间:2011-07-28 20:33:36

标签: git

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

15 个答案:

答案 0 :(得分:750)

要推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 :(得分:126)

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

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

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

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

其次是

git push --all

答案 2 :(得分:84)

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

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

source
  

注意:   如果您不使用直接CLI,则必须转义星号:

     

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

     

这会将远程git push destination +refs/remotes/source/\*:refs/heads/\*中的所有分支推送到source中的头分支,可能会执行非快进推送。你仍然需要单独推送标签。

答案 3 :(得分:12)

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

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

push = :表示“推送任何'匹配'分支(即远程存储库中已存在并具有本地对应分支的分支)”,而push = refs/tags/*表示“推送所有标记”。

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

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

答案 4 :(得分:8)

就我而言,有效的是。

git push origin --all

答案 5 :(得分:7)

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

# 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等。

答案 6 :(得分:4)

我发现上面的答案仍然有一些不清楚的东西,这会误导用户。首先,确定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/\*,然后使用{{ 1}}

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

答案 7 :(得分:4)

我正在从一种版本控制服务切换到另一种版本控制服务,需要克隆所有存储库,包括所有分支,标签和历史记录。

要实现上述目标,我接下来要做的是:

  • 手动签出本地存储库的所有分支(签出全部脚本如下所示),
  • git push origin '*:*'

.sh脚本,用于检出到本地存储库的所有分支:

for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; do
   git branch --track ${branch#remotes/origin/} $branch
done

答案 8 :(得分:2)

基于@Daniel回答我做了:

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

答案 9 :(得分:2)

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

void

这相当于组合git push origin 'refs/tags/*' 'refs/heads/*' 的{​​{1}}和--tags选项,git似乎不允许这样做。

答案 10 :(得分:2)

镜像存储库

创建存储库的裸克隆。

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

镜像包含Git Large File Storage对象的存储库

创建存储库的裸克隆。将示例用户名替换为拥有存储库的人员或组织的名称,然后将示例存储库名称替换为要复制的存储库的名称。

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

导航到您刚刚克隆的存储库。

cd old-repository.git

拉入存储库的Git大文件存储对象。

git lfs fetch --all

镜像推送到新存储库。

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

将存储库的Git大文件存储对象推到镜像中。

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/

答案 11 :(得分:2)

以下命令将推送所有分支(,包括您从未检出但存在于git repo中的分支,您可以通过git branch -a 看到它们)

git push origin '*:*'

注意:迁移版本控制时,此命令很方便 服务(即从 Gitlab 迁移到 GitHub

答案 12 :(得分:1)

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

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

  • 所有分支都是在新的远程
  • 上创建的
  • 所有分支历史记录都是在新远程上创建的
    • (我试过的每个解决方案都错过了这个)
  • 所有标签都是在新的远程
  • 上创建的
  • 来源移动(给定)
  • 非破坏性(暂停--mirror选项)

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

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

我的解决方案(基于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

答案 13 :(得分:1)

我最喜欢(也是最简单)的方式

git clone --mirror  OLD_GIT_URL
cd  NEW_CREATED_FOLDER
git remote add NEW-REMOTE NEW_GIT_URL
git push  NEW-REMOTE --mirror 

答案 14 :(得分:0)

每次我谷歌如何做这件事时,我最终都会阅读同一个线程,但它并没有让我到达我需要的地方,所以希望这对我未来的自己和其他人也有帮助。

我开始了一个新的本地项目,我想将它推送到我的存储库 (BitBucket)。这是我所做的:

  1. 导航到我的本地项目根目录
  2. 开始:git init
  3. 添加所有文件:git add .
  4. 提交:git commit -m "Initial commit"
  5. 转到我的存储库 (BitBucket)
  6. 创建新存储库:new_project
  7. 回到我的本地项目
  8. 添加遥控器:git remote add origin git@bitbucket.org:AndrewFox/new_project.git
  9. 推送提交:git push origin master -f

-f 标志是强制推送,否则会识别出两个 repo 不同而失败。

相关问题