“我的代码死活推不上去了!——原来远程仓库地址早就搬家了!” 这是我去年帮实习生调试时遇到的真实场景。别担心,修改Git远程仓库地址就像给手机换SIM卡一样简单,今天我就手把手教你三种姿势自由切换,从此再不怕仓库迁移或平台跳槽!
一、查看当前远程仓库:先确认“敌情”
git remote -v
执行后会显示类似这样的信息:
origin https://github.com/old-account/old-repo.git (fetch)
origin https://github.com/old-account/old-repo.git (push)
这就像查看手机当前运营商——是移动还是联通,先搞清楚现状才能修改。
🛠️ 高阶技巧:
- 用
git remote show origin
还能看到更多详细信息 - 如果显示
fatal: not a git repository
,说明你压根不在Git项目中
二、修改远程地址:三种姿势任你选
姿势1️⃣ 直接set-url(推荐)
git remote set-url origin https://github.com/new-account/new-repo.git
这就像直接给手机换SIM卡,是最干净的修改方式。适合:
- 仓库从GitHu