How to Prune Remote Branches in Git

Stewart Nguyen Feb 02, 2024
How to Prune Remote Branches in Git

This article will show how to prune (clean up) remote-tracking branches deleted in remote repositories.

For example, Alice and Bob are working on a branch feature/shared-branch. Bob creates a pull request, merges feature/shared-branch, and deletes it.

She executes git pull origin feature/shared-branch on Alice’s side.

$ git branch -a
* feature/shared-branch
  main
  remotes/origin/feature/shared-branch
  remotes/origin/main
$ git pull origin feature/shared-branch
fatal: couldn't find remote ref feature/shared-branch

Although remotes/origin/feature/shared-branch appears under git branch -a, performing git pull origin feature/shared-branch will still trigger an error as feature/shared-branch has already been deleted in the remote repository.

To overcome the issue, Alice should clean up the reference of feature/shared-branch, it is remotes/origin/feature/shared-branch. She might run git remote prune origin.

$ git remote prune origin
Pruning origin
URL: git@github.com:stwarts/git-demo.git
 * [pruned] origin/feature/shared-branch

git remote prune origin performs a checkup. Remote-tracking branches that don’t exist in the remote repository will be removed.

feature/shared-branch was deleted by Bob. Its remote-tracking branches remotes/origin/feature/shared-branch will be removed in Alice machine after she executes git remote prune origin.

Related Article - Git Prune