git-如何更改存储库链接的派生

我有一个名为FORK/repo.git的存储库,并将它分叉到MAIN/repo.git。我将这两个存储库都克隆到了计算机上,以用于不同的用途。

在Windows上使用Github,似乎有一个错误将FORK/repo.git切换到了MAIN/repo.git,因为当我执行git remote show origin时,Fetch URL和Push URL被设置为主仓库。 如何将其切换回本地计算机上的相应文件夹,使其指向FORK/repo.git,而不是MAIN/repo.git

zebra asked 2020-08-02T15:11:55Z
1个解决方案
82 votes

最简单的方法是在您的FORK本地克隆中使用命令行FORK

git remote rm origin
git remote add origin https://github.com/user/FORK.git

或者,在一个命令中,如此GitHub文章中所示:

git remote set-url origin https://github.com/user/FORK.git

更好的做法是:

  • 保持远程引用原始存储库
  • 在新分支中进行工作(上游分支将跟踪您的叉子)

所以:

git remote rename origin upstream
git branch -avv # existing branches like master are linked to upstream/xxx

git remote add origin https://github.com/user/FORK.git
git checkout -b newFeatureBranch

每当您需要根据原始存储库的最新发展来更新fork时:

git checkout master
git pull # it pulls from upstream!
git checkout newFeatureBranch
git rebase master # safe if you are alone working on that branch
git push --force # ditto. It pushes to origin, which is your fork.
VonC answered 2020-08-02T15:12:40Z
translate from https://stackoverflow.com:/questions/11619593/how-to-change-the-fork-that-a-repository-is-linked-to