跳到主要内容

更换原项目的Github账号和Repo

alansuhe 账号下已经存在一个名为 yi4-1 的 repo

gh repo create 默认会 尝试创建一个新 repo,如果名字重复就会报错。


解决方案

1️⃣ 不用再创建 repo,直接修改 remote 并 push

因为你只需要把本地项目和 已存在的 alansuhe/yi4-1 repo 关联:

cd ~/.../yi4-1

# 删除旧 remote(指向 Old Repo)
git remote remove origin

# 添加新的 remote 指向 alansuhe
git remote add origin https://github.com/alansuhe/yi4-1.git

# 推送本地分支到 alansuhe
git push -u origin main

如果你的默认分支是 master,把 main 改成 master 即可。


2️⃣ 可选:直接用 gh push(我现在使用的方法)

gh 也可以直接 push 到已存在的 repo:

# 删除旧 remote(指向 Old Repo)
git remote remove origin

# 连同新Repo创建带推送
gh repo create yi4-1 --private --source=. --push --remote=upstream


3️⃣ 总结

  • 如果 alansuhe 下已有 repo,直接修改本地 remote 并 push 就行(方法1)

  • 不会影响 RohanSensei 上的原 repo

补充

如果这个是web项目,并已经部署vercel。 则vercel中可以先在项目 -> Settings -> git中disconnect,再引入新的Repo。 但这个方法似乎就不再触发自动部署。 不行就删掉项目,在重新引入。

更换Repo后Vercel不自动部署的坑

把vercel项目的Github Repo改了以后,如果发现不自动部署了。 可能是项目还残留着之前的user信息。

执行:

git config --local --unset user.email
git config --local --unset user.name

重新提交:

# 将最后一次 commit 的作者重置为现在的正确配置
git commit --amend --reset-author --no-edit

git push origin main --force