Git remotes provide the ability to connect local repositories with shared central repositories for collaboration and distribution. While remotes are integral for most Git workflows, situations arise where removing a remote entirely is necessary for maintenance, migration, or recovering from mistakes.
This comprehensive guide covers all details around viewing, removing, and managing Git remotes directly from the command line interface. Best practices are provided for when remote removal makes sense versus when disabling or detaching them is preferable.
Additionally, alternatives to removal are explored including renaming, disabling, and detaching remote connections.
Viewing Currently Configured Git Remotes
The first step in managing remote connections is verifying existing remotes attached to your local clone along with their associated URLs.
Run git remote -v to list verbose remote information:
origin https://github.com/user/repo.git (fetch) origin https://github.com/user/repo.git (push) upstream https://github.com/original/repo (fetch) upstream https://github.com/original/repo (push)
This contains both read-only fetch and read+write push URLs for each remote. The same remote often appears on multiple lines depending on access permissions.
Remotes like ‘origin‘ refer to your own fork‘s GitHub location, while ‘upstream‘ indicates the source repo you forked from. Custom, developer-defined names are also common.
Removing Git Remotes Entirely
The git remote remove command deletes remotes from the local configuration:
git remote remove upstream
Unlike with git branch -d, remote removal directly modifies the repo‘s .git/config settings without confirmation prompts.
Verify the upstream remote no longer appears:
origin https://github.com/user/repo.git (fetch) origin https://github.com/user/repo.git (push)
For safety, double check remote information after removal with git remote -v in case unexpected issues occur.
Example: Removing Remotes In A Decentralized Workflow
In decentralized Git workflows with multiple shared remotes, removal helps simplify organization around a subset of core contributors.
Consider a repo with 5 active remotes:
origin https://github.com/team/project (fetch) bob https://github.com/bob/project (fetch) alice https://github.com/alice/project (fetch) develop https://dev.company.com/project (fetch) upstream https://git.company.com/project (fetch)
Here origin represents the core team, bob and alice are individual forks, develop is an internal dev server, and upstream is the centralized company git server.
To consolidate, the team decides origin and develop provide sufficient collaboration. Bob and Alice can still contribute via pull requests.
The repo maintainer removes the extra remotes:
git remote remove bob git remote remove alice git remote -v
Now only the essential origin and develop remotes remain.
Removing Remotes With Tracking Branches
Sometimes remote removal fails due to existing git branches tracking files from that source:
error: remote upstream has dependent branches hint: Delete them first with: git branch -d -r upstream/dev git branch -d -r upstream/feat/api
Resolve this by deleting the related tracking branches, either locally with git branch -d <branch> or remotely via git push origin :<branch>.
Then attempt the remote removal again.
Force Removing Git Remotes
As an alternative, branch tracking relationships can be ignored with:
git remote remove -f upstream
However this leaves unusable tracking branches behind and is not generally recommended.
Cleaning up stale remote-tracking branches is covered later in this guide.
Impact On Collaborative Git Workflows
In workflows like Gitflow which utilize separate remotes for long-running release branches, deleting those remotes can disrupt collaboration and forfeit historical information.
When removing remotes:<
- Disable pushes first to see impact before fully deleting
- Preserve read access temporarily if needed for historical purposes
- Confirm no active feature branches rely on remote tracking
On the other hand, GitHub Flow‘s single remote makes removal simpler. Just ensure your existing clone pushes to the new remote URL correctly before deleting the old origin.
Common Reasons To Remove Git Remotes
Some of the most common reasons for complete remote removal include:
Repository Migration
When migrating projects between version control systems or remote hosting providers like switching a GitHub repo to GitLab, removing old origins helps prevent fetch/push issues in the new location.
Security Compromises
If a server‘s access credentials become compromised, removing and recreating the remote with new tokens is advisable to prevent vulnerabilities.
Centralized Workflow Simplification
Teams transitioning from decentralized workflows may want to remove extraneous upstream repos and consolidate around a single origin.
Repository Copies Created For Experiments
Temporary feature branch or proof-of-concept repos that are merged into the main project can be removed if no longer necessary.
Decentralized Remote Cleanup
As demonstrated in the example above, removing less-critical remotes helps streamline decentralized collaboration.
Reducing Remote Chaos
In 2021, the average GitHub repository contained 140 forks, a form of remote. Maintainers may remove less active forks over time to reduce remote clutter.
| Average forks per GitHub repo | 140 |
| Average stars per GitHub repo | 2 |
| Average pull requests per repo | 6 |
(Statistics from 2021 GitHub Octoverse report)
When To Disable Remotes Instead
In certain cases where remotes provide value on occasion but shouldn‘t be interacted with regularly, disabling them temporarily may make more sense than permanent removal:
# Disable remote interactions git remote set-url --push disabled upstreamgit remote -v
git remote set-url --push upstream https://github.com/original/repo
With disabled push URLs, attempts to git push will fail while still allowing pulls. This avoids the need to fully delete and reconfigure remotes later.
| Action | Remove | Disable |
|---|---|---|
| Git Push | Fails | Fails |
| Git Pull | Fails | Works |
| Git Fetch | Fails | Works |
Comparing remote removal vs disabling effects
Alternatives To Remote Removal
In addition to disabling Git remotes, other alternatives to removing them include:
Detaching Head From Remote Tracking
The git branch --unset-upstream command detaches the current local branch from its assigned remote tracking branch without removing the remote itself:
git branch --unset-upstream git remote -v
This preserves read access to the remote but blocks automated pushes.
Renaming Existing Remotes
To modify a remote name like changing ‘origin‘ to ‘company‘, utilize git remote rename:
git remote rename origin company git remote
This preserves the configured URLs while allowing a refreshed reference term.
Cleaning Up After Removing Remotes
When removing remotes, additional clean up around stale tracking branches and configuration remnants is advised.
Deleting Local Remote Tracking Branches
If remote removal fails due to existing tracking branches, cleaning them up afterwards avoids lingering outdated references:
git fetch --prunegit branch -vv | grep ‘origin/.*: gone]‘ | awk ‘{print $1}‘ | xargs git branch -d
The branch deletion specifically filters for tracking references to the removed remote.
Pruning Configuration With gc
After any significant changes to remotes, running maintenance helps limit storage from unneeded lingering objects:
git remote prune origin git gc
This performs garbage collection to remove loose refs and optimizes the local repo.
Handling Remote Removal Failures
In rare cases, remote removal fails even after cleaning up tracking branches and the .git directory correctly. This may produce errors like:
fatal: No such remote ‘origin‘
This indicates the remote configuration still expects that server‘s availability.
Fix this by editing your Git repo‘s config file directly:
git config -e
As the configuration is now consistent the issues should resolve.
A git fsck check can also help diagnose other similar inconsistencies.
Automating Remote Removal
Instead of manual remote deletion, Git hooks can enable automatic removal triggers.
For example, installing the following pre-push hook script removes the outdated remote automatically whenever the repo is pushed to:
#!/bin/sh
git remote remove outdated
Configured properly, hooks guarantee remote removal consistency without reliance on developers remembering additional steps.
Conclusion
While deleting Git remotes permanently removes remote connections and should be used carefully, valid situations arise where removal is the best approach over alternatives. Following best practices around handling existing remote branches, config file consistency, and post-removal clean up assists in making removal smooth and effective.
Properly removing outdated, compromised, or unnecessary Git remotes contributes to improved security, performance, and long-term maintenance for critical source code repositories.


