When you want to detach
You forked a repo, pushed your work, and now want to publish independently. Or your team is migrating to a new Git host. Or you cloned a vendor's repo and want to make it your own. In all these cases, you need to detach the local repo from its origin.
Listing remotes
git remote -v
# origin [email protected]:upstream/project.git (fetch)
# origin [email protected]:upstream/project.git (push)
Removing a remote
git remote remove origin
git remote -v # confirm gone
This deletes the remote name and its tracking refs. Local commits are unaffected. Remote branches under refs/remotes/origin/ are removed.
Adding a new origin
git remote add origin [email protected]:yourname/project.git
git push -u origin --all
git push -u origin --tags
The repo now points at your fork; pushes go there.
Renaming instead
Sometimes detachment is partial - keep the old remote, add a new one:
git remote rename origin upstream
git remote add origin [email protected]:yourname/project.git
git fetch --all
git remote -v
This is the standard fork setup: origin is your fork, upstream is the source.
Updating a remote URL without removing
git remote set-url origin [email protected]:team/project.git
git remote set-url --push origin [email protected]:team/project.git
Useful when migrating to a new host - same remote name, new URL.
Removing tracking refs after detachment
If refs/remotes/origin/... linger after a remove (rare):
git for-each-ref refs/remotes/origin --format='%(refname)' | \
xargs -n 1 git update-ref -d
Detaching from upstream entirely
For a "clean break" - new repo, no shared history visible to consumers:
# Option 1: keep history but rewrite the parent
git checkout --orphan new-main
git add -A
git commit -m "Initial commit (rebased history)"
# Option 2: keep history, just publish to a new remote
git remote remove origin
git remote add origin <new-url>
git push -u origin --all
Pushing to multiple remotes
Sometimes you do not want to detach - you want to mirror. Configure multiple push URLs:
git remote set-url --add --push origin [email protected]:user/repo.git
git remote set-url --add --push origin [email protected]:user/repo.git
git push origin main # pushes to both
Checking after the fact
git config --get-regexp '^remote\\.'
git remote show origin
remote show connects to the remote and reports tracking branches, push refs, and stale refs - useful for verifying configuration.
Common pitfalls
- Forgetting to push tags to the new remote.
- Not updating CI configuration that hard-codes the old URL.
- Submodules pointing at the old remote - update
.gitmodulestoo.
Detachment is mechanical. The hard work is coordinating with the team and tooling, not running the commands.