How to change the fork that a repository is linked to
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.
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
?
The easiest way would be using command-line git remote
, from within your local clone of 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:
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.