git fetch –prune: Clean Up Branches Deleted on the Remote
Running git fetch --prune removes the origin/… branches that were deleted on the remote from your local list. Use it when you have merged a pull request and deleted the remote branch, but an old origin/feature/… still appears in git branch -r or git branch -a. I ran the git commands in a test environment and include the output from before and after each operation as it appeared.
The test environment was Ubuntu 24.04 with Git 2.43.0. For the remote repository, I used a bare repository (remote.git) placed in the same folder as two clones. The clone that creates and deletes branches is dev, and the clone that cleans up its local list is work. So that no environment-specific paths appear in the output, the origin URL is the relative path ../remote.git. (The From line shown during fetch displays ../remote, without the .git suffix.) The author name and email are example values (Neko <neko@example.com>). I did not test interactions with hosting services such as GitHub, or behavior on Windows or macOS.
- 1. Before pruning: deleted remote branches remain as origin/…
- 2. Before deleting: check what will be removed
- 3. git fetch –prune removes origin/… entries that no longer exist on the remote
- 4. Local branches are not deleted, but show as gone
- 5. Set fetch.prune to clean up automatically on every fetch
- 6. Tags are not removed by –prune alone
Before pruning: deleted remote branches remain as origin/…
In dev, I pushed five branches (main, feature/login, feature/search, fix/typo, docs/usage) and the tag v0.1. After that, I cloned into work, switched to feature/login once, and created a local branch with the same name. Here is the work branch list at this point. The remotes/origin/… entries are the remote-tracking branches that work remembers from the remote.
$ git branch -a
feature/login
* main
remotes/origin/HEAD -> origin/main
remotes/origin/docs/usage
remotes/origin/feature/login
remotes/origin/feature/search
remotes/origin/fix/typo
remotes/origin/main
Next, in dev, I deleted the remote feature/login and fix/typo branches.
$ git push origin --delete feature/login fix/typo
To ../remote.git
- [deleted] feature/login
- [deleted] fix/typo
I ran git fetch in work with no options. Nothing was displayed, and the origin/feature/login and origin/fix/typo entries I had expected to disappear remained.
$ git fetch
$ git branch -r
origin/HEAD -> origin/main
origin/docs/usage
origin/feature/login
origin/feature/search
origin/fix/typo
origin/main
You can ask the remote directly which branches actually exist with git ls-remote --heads origin. Only three remain: docs/usage, feature/search, and main.
$ git ls-remote --heads origin
b7f0d979013a7f41672acaacdb4e476000ed878f refs/heads/docs/usage
4a80483b96945b382218637ebd74261dbea104f6 refs/heads/feature/search
e5639665d087edf66b4a5c0d6760c5eec4b83069 refs/heads/main
According to the PRUNING section of the git fetch reference, Git keeps data unless you explicitly discard it. Local references that point to branches deleted on the remote are therefore kept as they are.
Before deleting: check what will be removed
You can check the targets with --dry-run before deleting anything. git remote prune --dry-run origin uses the would prune form, and git fetch --prune --dry-run prints lines in the same format as without --dry-run. Both showed the two entries that would be removed. After either command, the output of git branch -r was unchanged.
$ git remote prune --dry-run origin
Pruning origin
URL: ../remote.git
* [would prune] origin/feature/login
* [would prune] origin/fix/typo
$ git fetch --prune --dry-run
From ../remote
- [deleted] (none) -> origin/feature/login
- [deleted] (none) -> origin/fix/typo
git fetch –prune removes origin/… entries that no longer exist on the remote
When I fetched with --prune, [deleted] lines appeared, and the two entries that no longer existed on the remote disappeared from the list.
$ git fetch --prune
From ../remote
- [deleted] (none) -> origin/feature/login
- [deleted] (none) -> origin/fix/typo
$ git branch -r
origin/HEAD -> origin/main
origin/docs/usage
origin/feature/search
origin/main
When I ran git fetch --prune again, nothing was displayed, because there was nothing left to remove.
According to the git fetch reference, --prune (short form -p) removes remote-tracking branches that no longer exist on the remote, before fetching.
If you only want to remove them without fetching, you can use git remote prune origin. I deleted the remote docs/usage branch in dev, then ran this command in work. origin/docs/usage was removed.
$ git remote prune origin
Pruning origin
URL: ../remote.git
* [pruned] origin/docs/usage
$ git branch -r
origin/HEAD -> origin/main
origin/feature/search
origin/main
The git remote reference describes this as the same as git fetch --prune origin, except that it does not fetch new references.
Local branches are not deleted, but show as gone
--prune removes only the origin/… entries. The local feature/login branch I created remained. In git branch -vv, its upstream field shows [origin/feature/login: gone].
$ git branch -vv
feature/login a311519 [origin/feature/login: gone] ログイン画面を追加
* main e563966 [origin/main] 最初のコミット
To identify branches whose upstream no longer exists, you can use a format that includes %(upstream:track). As described in the git for-each-ref reference, branches whose upstream cannot be found show [gone].
$ git branch --format="%(refname:short) %(upstream:track)"
feature/login [gone]
main
Once you have confirmed you no longer need a local branch, delete it yourself. Because feature/login was not merged into main, -d refused to delete it, and -D succeeded. I cover the difference between -d and -D in detail in the article on renaming and deleting branches in Git.
$ git branch -d feature/login
error: the branch 'feature/login' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature/login'
$ git branch -D feature/login
Deleted branch feature/login (was a311519).
According to the git branch reference, -D is short for --delete --force and deletes a branch whether or not it has been merged. Before deleting a [gone] branch, make sure it does not contain unpushed work.
Set fetch.prune to clean up automatically on every fetch
To avoid typing --prune each time, you can set fetch.prune to true. According to the description of fetch.prune in the git config reference, this setting makes git fetch behave as if --prune were always given. To choose the behavior per remote, use remote.<name>.prune (for example, remote.origin.prune).
In dev, I also deleted the remote feature/search branch. Then, in work, I set the option with --global and ran git fetch without --prune. origin/feature/search was removed.
$ git config --global fetch.prune true
$ git config --show-scope --get fetch.prune
global true
$ git fetch
From ../remote
- [deleted] (none) -> origin/feature/search
$ git branch -r
origin/HEAD -> origin/main
origin/main
With --global, the setting is written to the per-user configuration file (~/.gitconfig), not inside the repository. Without it, the setting is written to that repository's .git/config.
Tags are not removed by –prune alone
Tags that came along automatically during a fetch are not removed by --prune. I deleted the remote tag v0.1 in dev, then ran git fetch --prune in work, and v0.1 remained. Adding --prune-tags removed tags that no longer exist on the remote.
$ git fetch --prune
$ git tag
v0.1
$ git fetch --prune --prune-tags
From ../remote
- [deleted] (none) -> v0.1
$ git tag
However, the git fetch reference advises using --prune-tags with more care than --prune. This is because it removes any local tag that does not exist on the remote, regardless of whether the tag was originally fetched from the remote or created locally. If you want to clean up tags automatically as well, set fetch.pruneTags to true in addition to fetch.prune. Use it with the same caution.



