git 把代码推送到另外一个仓库

工作中, 我们会遇到这样的场景, 从 github 上拉了一些项目代码下来, 修改到一半需要回家了, 但不想 push 上 github, 也不想用硬盘拷贝, 这时可以先 push 到私有仓库中, 回家继续修改之后, 再推送到 github 仓库中.

把 github 仓库的代码推送到另外一个仓库(OSC)


假设现在我从 github 上 clone 了 dubbo 的仓库下来, 然后我改了东西, 现在我要推送到 osc 开源中国的私有 git 仓库中, 以下是完整操作:

# 把origin改名为github, 方便识别
$ git remote rename origin github
$ git remote -v
github git@github.com:alibaba/dubbo.git (fetch)
github git@github.com:alibaba/dubbo.git (push)

# 增加osc的remote
$ git remote add osc git@git.oschina.net:niko2014/test-push-another-repo.git
$ git remote -v
github git@github.com:alibaba/dubbo.git (fetch)
github git@github.com:alibaba/dubbo.git (push)
osc git@git.oschina.net:niko2014/test-push-another-repo.git (fetch)
osc git@git.oschina.net:niko2014/test-push-another-repo.git (push)

# 先在 osc 创建一个空的 repo
$ git push osc master
Counting objects: 37880, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (13163/13163), done.
Writing objects: 100% (37880/37880), 5.61 MiB | 1.73 MiB/s, done.
Total 37880 (delta 12824), reused 37879 (delta 12823)
remote: Resolving deltas: 100% (12824/12824), done.
To git@git.oschina.net:niko2014/test-push-another-repo.git
* [new branch] master -> master

# 现在你可以在多个 repo 之间各种 pull push 了
$ git push osc master
$ git push github master
$ git pull osc master
$ git pull github master

有了多个 remote , 不仅可以完成 github -> osc , 还可以将 osc -> github .

常见问题


推送分支和远程 tag 名相同 - refspec matches more than one.


错误:

$ git push --set-upstream osc dubbo-2.4.11
error: src refspec dubbo-2.4.11 matches more than one.
error: failed to push some refs to 'git@git.oschina.net:niko2014/dubbo_n.git'

解决:

$ git push  osc  HEAD:dubbo-2.4.11
Counting objects: 2696, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (1105/1105), done.
Writing objects: 100% (2280/2280), 233.51 KiB | 0 bytes/s, done.
Total 2280 (delta 861), reused 2165 (delta 754)
To git@git.oschina.net:niko2014/dubbo_n.git
* [new branch] HEAD -> dubbo-2.4.11

# 或者使用
# git push origin refs/heads/xxx:refs/heads/xxx

不过, 最好不要用相同的 tag 和 branch 名字.

参考: http://stackoverflow.com/questions/9378760/git-push-local-branch-with-same-name-as-remote-tag

tag 和 branch 重名问题


相同的 tag 和 branch 名字容易出现各种问题, 不推荐使用. 比如推送分支 dubbo-2.4.11 时的提示 :

refname 'dubbo-2.4.11' is ambiguous

解决方法一: 改 branch

改变分支名, 删掉旧的分支.

# 新的分支名
git checkout -b dubbo-2.4.11-branch
# Delete the specified branch. This is a “safe” operation in that Git prevents you from deleting the branch if it has unmerged changes.
git branch -d dubbo-2.4.11
# 或者
# Force delete the specified branch, even if it has unmerged changes. This is the command to use if you want to permanently throw away all of the commits associated with a particular line of development.
git branch -D dubbo-2.4.11

解决方法二: 改 tag

使用 git tag [tag] [commit] 先重命名保存原来的 tag , 然后删掉.

# 将 dubbo-2.4.11 的 commit 使用 tag 为 dubbo-2.4.11-tag
git tag dubbo-2.4.11-tag dubbo-2.4.11
# 删除原来的 dubbo-2.4.11 的 tag
git tag -d xxx

参考: http://stackoverflow.com/questions/12224773/git-refname-master-is-ambiguous

参考


http://stackoverflow.com/questions/5181845/git-push-existing-repo-to-a-new-and-different-remote-repo-server
https://www.atlassian.com/git/tutorials/using-branches/git-branch