GIT切换远程地址仓库

有时候,我们想把一个项目从github切换到其他git服务器,免不了就要切换远程仓库地址。那这时候有什么办法解决呢?据目前自己的了解,有三种办法。
一、修改命令
git remote set-url origin url
二、先删后加
git remote rm origingit remote add origin git@github.com:sheng/demo.git
三、修改config文件
如果你的项目有加入版本控制,那可以到项目根目录下,查看隐藏文件夹, 发现.git文件夹,找到其中的config文件,就可以修改其中的git remote origin地址了。
切换完了之后我们发现无法提交代码,报错如下:

git push origin master
To git@github.com:qzmly100/repository-.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@github.com:qzmly100/repository-.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解决方案:
远程分支上存在本地分支中不存在的提交,往往是多人协作开发过程中遇到的问题,可以先fetch再merge,也就是pull,把远程分支上的提交合并到本地分支之后再push。先抓取并合并远程仓库全部内容(git pull origin master),再推送本地仓库数据(git push origin master)。
如果你确定远程分支上那些提交都不需要了,那么直接git push origin master -f,强行让本地分支覆盖远程分支


把本地仓库与远程仓库建立关联
git branch --set-upstream master origin/master

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容