zebra
zebra

Reputation: 6531

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?

Upvotes: 77

Views: 66472

Answers (2)

VonC
VonC

Reputation: 1323115

2023: gh repo fork allows since gh v2.37.0 to set default repo when forking repo.

Now, If the repository is a fork, its parent repository will be set as the default remote repository. See issue 6827 and PR 7768, plus gh repo set-default.


2012: 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.

Upvotes: 157

There is an easier solution if you don't mind using the GitHub CLI: https://cli.github.com/

It has a command name repo fork: https://cli.github.com/manual/gh_repo_fork

It will allow you to fork a repo you cloned that isn't yours and switch the origin remote to your fork, just run gh repo fork and select "Yes" when it asks you if you wanna update origin.

There will be a new remote named upstream which points to the original repository before the fork, if you need that.

Upvotes: 1

Related Questions