Git Delete Stale Branch

less than 1 minute read

Description:

So I had an issue the other day where I ran git branch -va and saw a bunch of branches that existed locally and not on the remote server. Here is what you do to fix this to get them back in sync.

To Resolve:

  1. First list my current branches

    1
    2
    3
    4
    5
    
    me@server.domain.com:/home/myuser/my-repo (main) 
    $ git branch 
    MySprint_1.3
    MySprint_1.6
    * main
    
  2. Then list just the remotes

    1
    2
    3
    4
    
    me@server.domain.com:/home/myuser/my-repo (main) 
    $ git branch -r
    origin/HEAD -> origin/main
    origin/main
    
  3. Now remove stale branches from remote and then delete all the branches local that don’t match up:

    1
    2
    3
    4
    5
    6
    
    me@server.domain.com:/home/myuser/my-repo (main) 
    $ git remote prune origin
    URL: git@ssh.dev.azure.com:v3/organization/project/repo
    * [pruned] origin/Sprint_1.3
    $ git branch -D MySprint_1.3
    # Deleted branch MySprint_1.3 (was 510ce91).