git remote branch cleanup

Many times in development, you pull in remote branches and over a period of time they become stale and get deleted on the server. But your local repository still keeps a reference of those. You need to clean stale / deleted remote branches on your local system

A simple command git remote prune helps.

Check which branches are dpresent on local and getting tracked

[rutu@ruturaj-vartak git-bisect-clone-2]$ git branch -av
* master                     43e538a merged
  test-branch                7c06ab4 added p
  remotes/origin/HEAD        -> origin/master
  remotes/origin/master      43e538a merged
  remotes/origin/test-branch 7c06ab4 added p

The above command git branch -av shows my branch test-branch is present on local and also present on remote

The following command shows which branches can be un-tracked/deleted on local (as they’ve been deleted on remote)

[rutu@ruturaj-vartak git-bisect-clone-2]$ git remote prune -n origin
Pruning origin
URL: file:///tmp/git-bisect-demo/
 * [would prune] origin/test-branch

The option -n is a dry-run option, showing which branches can be deleted/pruned on your local – as in this case test-branch. So we can go ahead and actually prune/cleanup

[rutu@ruturaj-vartak git-bisect-clone-2]$ git remote prune  origin
Pruning origin
URL: file:///tmp/git-bisect-demo/
 * [pruned] origin/test-branch

This cleans up my repository, a git branch -av will show my local status of branches.

[rutu@ruturaj-vartak git-bisect-clone-2]$ git branch -av
* master                43e538a merged
  test-branch           7c06ab4 added p
  remotes/origin/HEAD   -> origin/master
  remotes/origin/master 43e538a merged

The remote branch origin/test-branch has been knocked off locally, but the local branch test-branch remains. If wanted, we can clean-up the local as well

[rutu@ruturaj-vartak git-bisect-clone-2]$ git branch -d test-branch
Deleted branch test-branch (was 7c06ab4).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.