我有一个代码库,想把它推送到Bitbucket和GitHub上。对于我来说,让这个库同时托管在两个平台上非常重要。
在Git中有没有办法做到这一点?
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 之间切换时就不会遇到权限被拒绝的错误了。希望这能帮助到某些人。 - Ultimater是的,你可以这样做。你不需要推两次,只需推一次即可将代码推送到两个远程仓库。 我之前也遇到过同样的问题,所以在这里写了如何操作。 Git:同时推送/拉取Github和Bitbucket
几个简单的解决方案。
这是最容易理解的方法,但维护起来需要付出最大的努力。
我们首先添加新的远程仓库:
$ cd myproject
$ git remote add bitbucket ssh://git@bitbucket.org/user/myproject.git
$ git push bitbucket master
很直接明了,对吧?当然,每次我们提交任何更改时,需要将其推送到原始的“origin”和新的远程“bitbucket”:
$ git push origin master
$ git push bitbucket master
虽然不是很大的负担,但我相信随着时间的推移,它会变得越来越烦人。或者你可以创建一个别名 gpob="git push origin master && git push bitbucket master"。
使用此方法,我们将向现有的“origin”远程仓库添加其他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
我也在博客中介绍了这个方法。
我有一个类似的情况,但有所不同。就像我有一个现有的BitBucket分支,过去几个月我一直在管理或更新我的代码。
现在我的要求是将我的项目上传到GitHub,只有一个“master”分支。
步骤1 -
这是我的现有BitBucket项目存储库信息。
$ 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 仓库 URL -
$ git remote set-url origin --add https://github.com/<USERNAME>/<PROJECT_NAME>.git
$ 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)
步骤三 -
为更好地理解重命名存储库 -
$ 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存储库URL) -
将BitBucket和GitHub的https://
URL更改为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
移除原始仓库以更改仓库 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
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
将代码推送到两个分支 -
$ git push
将代码推送到GitHub或BitBucket -
$ git push github master
或 $ git push bitbucket master
更改分支 -
$ git checkout <BRANCH_NAME>
实用的信息 -
步骤5和6非常有用,如果您正在使用http://
仓库URL,则将其更改为ssh
以更好地维护仓库。
或者,如果在第1步/ $ git remote -v show
之后发现https://
仓库URL,则最好使用步骤5和6。
如果任何人已经拥有ssh
仓库,则忽略步骤5和6。