In modern development workflows using GitHub or GitLab, feature branches are typically deleted on the remote host as soon as their corresponding pull request (PR) is merged.
However, when you run git branch -a in your local terminal, you will notice that the references pointing to those deleted remote branches (e.g., remotes/origin/feature-xxx) remain in your branch list indefinitely.
This occurs because Git does not automatically sync deletions from remote configurations to your local cache by default.
In this guide, we’ll explain how to safely clean up these obsolete remote-tracking branches and configure Git to keep your workspace tidy automatically.
1. The Cause: Stale Remote-Tracking References
When you execute a standard git fetch or git pull command, Git grabs new branch data added to the remote repository.
However, Git does not automatically subtract (delete) references to branches that have been removed from the remote. These orphaned tracking markers persist on your local machine, cluttering tab completions and search listings.
Clearing these stale local markers is called Pruning.
2. Pruning with git fetch --prune
The most direct way to sweep away obsolete remote-tracking references is by adding the --prune (or short -p) flag to your fetch command:
# Fetch updates and prune deleted tracking branches
git fetch --prune
# Or
git fetch -p
When run, Git outputs a summary of deleted references:
From github.com:user/repository
- [deleted] (none) -> origin/feature-login
- [deleted] (none) -> origin/feature-auth
Running git branch -a again will confirm that the stale remotes/origin/ entries have been cleared.
3. Automating Pruning Globally (Recommended)
To avoid typing --prune manually with every fetch, configure Git to prune tracking references automatically on every fetch or pull:
# Enable pruning automatically on all git fetches globally
git config --global fetch.prune true
Once enabled, any execution of git fetch or git pull will run pruning in the background.
4. Cleaning Local Working Branches (git branch -d)
It is important to note that git fetch --prune only removes references to remote branches (origin/xxx). Your own local branch workspace (xxx) remains untouched.
To delete local working branches that have been successfully merged, use:
# Delete a merged local branch safely
git branch -d feature-login
# Force delete an unmerged local branch
git branch -D feature-login
Conclusion
Orphaned tracking entries can build up quickly, adding clutter to your command-line environment.
- Use
git fetch -pto prune stale references. - Automate the operation by setting
fetch.prune trueglobally. - Delete local working branches manually using
git branch -d.
Keep your workspace clean to maintain a faster, more responsive terminal workflow.
