【发布时间】:2012-10-18 18:40:19
【问题描述】:
我有一个存储库,我想将其推送到 Bitbucket 和 GitHub。 由两者托管我的存储库至关重要。
有没有办法在 Git 中做到这一点?
【问题讨论】:
-
这似乎很有用。特别是当一个存储库关闭时。目前 bitbucket 已关闭,我基本上无法完成工作。
我有一个存储库,我想将其推送到 Bitbucket 和 GitHub。 由两者托管我的存储库至关重要。
有没有办法在 Git 中做到这一点?
【问题讨论】:
您可以通过 git 使用多个远程存储库。但我相信你必须分别推入你的 2 个遥控器。
例如,如果你的项目当前指向 github,你可以将你当前的远程仓库重命名为github:
$ git remote rename origin github
然后您可以添加另一个远程存储库,例如bitbucket:
$ git remote add bitbucket git@bitbucket.org:your_user/your_repo.git
现在为了将更改推送到 github 或 bitbucket 上的相应分支,您可以这样做:
$ git push github HEAD
$ git push bitbucket HEAD
同样的规则适用于拉取:您需要指定要从哪个遥控器拉取:
$ git pull github your_branch
$ git pull bitbucket your_branch
【讨论】:
.ssh/config 文件是实现此设置的关键。添加Host github.com 和Host bitbucket.org 以识别要使用的IdentityFile。然后,您将不会在尝试在 bitbucket 和 github 之间来回交换时遇到权限被拒绝错误。希望这对某人有所帮助。
是的,你可以这样做。您不必推送两次,只需推送一次即可推送到两个远程存储库。 我之前遇到过同样的问题,所以在这里写了如何做。 Git: Push to / Pull from Both Github and Bitbucket
【讨论】:
一些EASY解决方案。
这是最容易理解的,但维护起来最费力。
我们首先添加我们的新遥控器:
$ cd myproject
$ git remote add bitbucket ssh://git@bitbucket.org/user/myproject.git
$ git push bitbucket master
直截了当,不是吗?当然,每次我们提交任何更改时,我们都需要推送到我们原来的“原点”和新的远程“bitbucket”:
$ git push origin master
$ git push bitbucket master
开销不是很大,但我相信它会随着时间的推移而增加。或者你可以创建一个`alias gpob="git push origin master && git push bitbucket master"。
通过这个方法,我们将向我们现有的远程“原点”添加一个额外的 URL:
$ cd myproject
$ git remote set-url --add origin ssh://git@bitbucket.org/user/myproject.git
$ git push origin master
Everything up-to-date
Everything up-to-date
省力!
当然,一线希望有云,在这种情况下,虽然我们可以同时推送到多个 URL,但我们只能从原始“源”获取(您可以更改这一点,但这超出了范围这篇文章)。
最后,查看将从哪个遥控器获取:
$ git remote -v show
我也是blogged about it。
【讨论】:
我也有类似的情况,但有一点不同。就像我有一个现有的 BitBucket 分支,过去几个月我在其中管理或更新我的代码表单。
现在我的要求是在 GitHub 中上传我的项目,并且只有一个“主”分支。
第 1 步 -
这是我现有的 BitBucket 项目 repo 信息。
$ git remote -v show
origin https://<USERNAME>@bitbucket.org/<USERNAME>/<PROJECT_NAME>.git (fetch)
origin https://<USERNAME>@bitbucket.org/<USERNAME>/<PROJECT_NAME>.git (push)
第 2 步 -
添加一个远程 github repo url -
$ git remote set-url origin --add https://github.com/<USERNAME>/<PROJECT_NAME>.git
现在,它也有 github 信息 ($ git remote -v show)。
origin https://<USERNAME>@bitbucket.org/<USERNAME>/<PROJECT_NAME>.git (fetch)
origin https://<USERNAME>@bitbucket.org/<USERNAME>/<PROJECT_NAME>.git (push)
origin https://github.com/<USERNAME>/<PROJECT_NAME>.git (push)
第 3 步 -
重命名存储库以便更好地理解 -
$ git remote add bitbucket https://<USERNAME>@bitbucket.org/<USERNAME>/<PROJECT_NAME>.git
$ git remote add github https://github.com/<USERNAME>/<PROJECT_NAME>.git
现在,信息已更新 ($ git remote -v show)。
bitbucket https://<USERNAME>@bitbucket.org/<USERNAME>/<PROJECT_NAME> (fetch)
bitbucket https://<USERNAME>@bitbucket.org/<USERNAME>/<PROJECT_NAME> (push)
github https://github.com/<USERNAME>/<PROJECT_NAME>.git (fetch)
github https://github.com/<USERNAME>/<PROJECT_NAME>.git (push)
origin https://<USERNAME>@bitbucket.org/<USERNAME>/<PROJECT_NAME> (fetch)
origin https://<USERNAME>@bitbucket.org/<USERNAME>/<PROJECT_NAME> (push)
origin https://github.com/<USERNAME>/<PROJECT_NAME>.git (push)
第 4 步 -
是时候在 GitHub 中提交/推送整个项目了。
$ git add --all && git commit -m "first commit"
$ git push -u origin master
结果我得到了这个错误 -
Everything up-to-date
Branch 'master' set up to track remote branch 'master' from 'origin'.
remote: Repository not found.
fatal: repository 'https://github.com/<USERNAME>/<PROJECT_NAME>.git/' not found
第 5 步(SSH 密钥设置)-
经过几个小时的调查,我发现这是 SSH 密钥问题。
所以我为 BitBucket 和 GitHub 生成 SSH 密钥,并将这些密钥添加到我各自的帐户中。
第 6 步(设置 SSH repo url)-
将 BitBucket 和 GitHub 的 https:// 网址更改为 ssh。
$ git remote set-url bitbucket git@bitbucket.org:<USERNAME>/<PROJECT_NAME>.git
$ git remote set-url github git@github.com:<USERNAME>/<PROJECT_NAME>.git
删除原点以更改原点 repo url。
$ git remote rm origin
添加第一个来源(BitBucket)-
$ git remote add origin git@bitbucket.org:<USERNAME>/<PROJECT_NAME>.git
添加第二个来源(GitHub)-
$ git remote set-url origin --add git@github.com:<USERNAME>/<PROJECT_NAME>.git
所有 repo url 更改为ssh。
$ git remote -v show
bitbucket git@bitbucket.org:<USERNAME>/<PROJECT_NAME>.git (fetch)
bitbucket git@bitbucket.org:<USERNAME>/<PROJECT_NAME>.git (push)
github git@github.com:<USERNAME>/<PROJECT_NAME>.git (fetch)
github git@github.com:<USERNAME>/<PROJECT_NAME>.git (push)
origin git@bitbucket.org:<USERNAME>/<PROJECT_NAME>.git (fetch)
origin git@bitbucket.org:<USERNAME>/<PROJECT_NAME>.git (push)
origin git@github.com:<USERNAME>/<PROJECT_NAME>.git (push)
第 7 步 -
我已经添加并提交了代码,所以我只需要推送。
$ git push -u origin master
您确定要继续连接(是/否/[指纹])? 是的
最后,整个项目在 master 分支推送到 GitHub。
将代码推送到两个分支 -
$ git push
仅将代码推送到 GitHub 或 BitBucket -
$ git push github master 或 $ git push bitbucket master
更改分支-
$ git checkout <BRANCH_NAME>
实时保存信息 -
第 5 步和第 6 步很有用如果您使用的是 http:// repo url,请将其更改为 ssh 以便更好地维护 repo。
如果有人在步骤 1 / $ git remote -v show 之后找到 https:// repo url,最好使用 步骤 5 和 6
如果有人已经拥有ssh repo,请忽略第 5 步和第 6 步。
【讨论】: