git 如何更改存储库链接到的分叉
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11619593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the fork that a repository is linked to
提问by zebra
I have a repo called at MAIN/repo.git
and I've forked it to FORK/repo.git
. I have both of these repos cloned onto my computer for different purposes.
我有一个 repo 调用,MAIN/repo.git
我已经将它分叉到FORK/repo.git
. 我出于不同的目的将这两个存储库克隆到我的计算机上。
Using Github for Windows, a bug seems to have switched FORK/repo.git
over to MAIN/repo.git
, as when I do git remote show origin
, the Fetch URL and Push URL are set to the main repo. How can I switch this back so the corresponding folder on my local machine points to FORK/repo.git
, instead of MAIN/repo.git
?
使用 Github for Windows,一个错误似乎已经切换FORK/repo.git
到MAIN/repo.git
,就像我做的那样git remote show origin
,Fetch URL 和 Push URL 被设置为主存储库。如何将其切换回来,以便本地计算机上的相应文件夹指向FORK/repo.git
, 而不是MAIN/repo.git
?
回答by VonC
The easiest way would be using command-line git remote
, from within your local clone of FORK
:
最简单的方法是使用命令行git remote
,从您的本地克隆中FORK
:
git remote rm origin
git remote add origin https://github.com/user/FORK.git
Or, in one command, as illustrated in this GitHub article:
或者,在一个命令中,如这篇GitHub 文章中所示:
git remote set-url origin https://github.com/user/FORK.git
A better practice is to:
更好的做法是:
- keep a remote referencing the original repo
- make your work in new branches (which will have upstream branches tracking your fork)
- 保持远程引用原始存储库
- 在新分支中进行工作(这将使上游分支跟踪您的分支)
So:
所以:
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
Whenever you need to update your fork based on the recent evolution of the original repo:
每当您需要根据原始存储库的最新演变更新您的分叉时:
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.