我有一个本地Git存储库,我想将其推送到新的远程存储库(如果需要的话,可以在Beanstalk上设置全新的存储库)。
我的本地存储区有几个分支和标签,我希望保留所有历史记录。
看来我基本上只需要做一个git push
,但是只上传master
分支。
如何推送所有内容,以便在远程计算机上获得本地存储的完整副本?
我有一个本地Git存储库,我想将其推送到新的远程存储库(如果需要的话,可以在Beanstalk上设置全新的存储库)。
我的本地存储区有几个分支和标签,我希望保留所有历史记录。
看来我基本上只需要做一个git push
,但是只上传master
分支。
如何推送所有内容,以便在远程计算机上获得本地存储的完整副本?
Answers:
要推送所有分支,请使用其中一个(将REMOTE替换为远程名称,例如“ origin”):
git push REMOTE '*:*'
git push REMOTE --all
推送所有标签:
git push REMOTE --tags
最后,我认为您可以使用以下命令在一个命令中完成所有操作:
git push REMOTE --mirror
但是,此外--mirror
,还会推动您的遥控器,因此这可能并不是您想要的。
--all
而不是*:*
看起来更友好
git push REMOTE --all
返回No refs in common and none specified;
什么都不做,而git push REMOTE "*:*
实际上却将所有分支推到了远端。
git clone --mirror old-remote-url; cd repo.git; git push --mirror new-remote-url
。
在像我这样的情况下,您购买了一个回购协议,现在正在将远程源切换到另一个回购协议,即一个新的空仓库...
因此,您具有仓库和所有分支,但是您仍然需要检出这些分支,以使git push --all
命令也能实际推送这些分支。
您应在执行以下操作之前执行此操作:
for remote in `git branch -r | grep -v master `; do git checkout --track $remote ; done
其次是
git push --all
git push '*:*'
推了所有分支。git push -all
刚推高手。我正在将回购从github传输到bitbucket。
--track remotes/$remote
而不是--track $remote
。这是完整的命令行:for remote in `git branch -r | grep -v master `; do git checkout --track remotes/$remote ; done
这是同一件事的另一种选择,对于我当时所处的情况更有效。它解决了您拥有多个远程站点的问题,希望将所有分支克隆source
到远程站点destination
,而不必事先检查所有分支。
(我的Daniel解决方案存在的问题是,source
如果我以前已经签出过它,它将拒绝从远程签出跟踪分支,即,在推送之前它不会更新本地分支)
git push destination +refs/remotes/source/*:refs/heads/*
注意:如果您不使用直接CLI,则必须转义星号:
git push destination +refs/remotes/source/\*:refs/heads/\*
这会将所有远程分支推source
到中的head分支destination
,可能会进行非快进推送。您仍然必须单独推送标签。
remote
到另一个克隆。谢谢!
git push destination +refs/remotes/source/\*:refs/heads/\*
--prune
选项的一个小例子。
--mirror
大家推荐的通常参数大不相同。非常适合于您只想使两个遥控器保持自动化或审核目的的情况。
如果目的地为空,这是我找到的最简洁的方法。切换到一个空文件夹,然后:
# 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
等中的。
的联机帮助页git-push
值得一读。结合这个网站,我在自己的文章中写了以下内容.git/config
:
[remote "origin"]
url = …
fetch = …
push = :
push = refs/tags/*
的 push = :
手段“推动任何‘匹配’分支(即已经存在于远程仓库,并有当地合作即分行)”,而push = refs/tags/*
表示“推动所有标签”。
所以现在我只需要跑步 git push
即可推送所有匹配的分支和所有标签。
是的,这并不是OP想要的(要推送的所有分支必须已经存在于远程端),但是对于那些在搜索“我如何同时推送分支和标签时发现此问题”的人可能会有所帮助时间”。
镜像存储库
创建存储库的裸克隆。
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大文件存储对象的存储库
创建存储库的裸克隆。将示例用户名替换为拥有存储库的人员或组织的名称,然后将示例存储库名称替换为要复制的存储库的名称。
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/
我发现上述答案仍有一些不清楚的地方,这会误导用户。首先,确保git push new_origin --all
并且git 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
下载一个裸仓库,不包括工作区。请参考这个
2,如果您使用来创建git repo git clone
,这意味着您有裸露的repo和git工作区,则可以使用git remote add new_origin new_origin_url
,然后使用,git push new_origin +refs/remotes/origin/\*:refs/heads/\*
然后git push new_origin --tags
这样,您将获得一个多余的head分支,这毫无意义。
我发现这些似乎都不适合我。随意将其杀死,但由于某些原因无法使其他选项正常工作。
预期结果是回购“克隆”到另一个远程服务器(例如,从Github到另一个提供商):
我看到的主要问题是没有在新的遥控器中重新创建所有远程分支。如果有命令,则新的远程控制器没有分支历史记录(即执行操作git checkout branch; git log
不会显示预期的分支提交)。
我注意到git checkout -b branchname
与git 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
下面的命令将推送所有分支(包括您从未检出但存在于git repo中git branch -a
的分支,您可以通过查看它们)
git push origin '*:*'
注意:当您迁移版本控制服务时(即从Gitlab迁移到GitHub),此命令非常方便。
我正在从一种版本控制服务切换到另一种版本控制服务的过程中,需要克隆所有存储库,包括所有分支,标签和历史记录。
为了达到上述目的,我接下来要做的是:
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