git fetch -p

Published on Monday, August 24, 2020

This is just a quick post about git fetch -p. If you work with git a lot (in particular using the terminal, like a proper user should), then you may notice that for projects where you create a lot of branches to divide work in your team, your git branch output looks sort of like this:

caleb@work MINGW64 /c/git/Some_Project (develop)
$ git branch -r
  remotes/origin/develop
  remotes/origin/effort/BRANCH_A
  remotes/origin/effort/BRANCH_B
  remotes/origin/effort/BRANCH_C
  remotes/origin/effort/BRANCH_D
  remotes/origin/effort/BRANCH_E
  remotes/origin/effort/BRANCH_F
  remotes/origin/effort/BRANCH_G
  remotes/origin/feature/BRANCH_H
  remotes/origin/master
  remotes/origin/story/BRANCH_I
  remotes/origin/story/BRANCH_J
  remotes/origin/story/BRANCH_K
  remotes/origin/story/BRANCH_L
  remotes/origin/story/BRANCH_M
  remotes/origin/story/BRANCH_N
  remotes/origin/story/BRANCH_O
  remotes/origin/story/BRANCH_P
  remotes/origin/story/BRANCH_Q
  remotes/origin/story/BRANCH_R
  remotes/origin/story/BRANCH_S
  remotes/origin/story/BRANCH_T
  remotes/origin/story/BRANCH_U
  remotes/origin/story/BRANCH_V
  remotes/origin/story/BRANCH_W
  remotes/origin/story/BRANCH_X
  remotes/origin/story/BRANCH_Y
  remotes/origin/story/BRANCH_Z
  remotes/origin/story/BRANCH_AA
  remotes/origin/story/BRANCH_AB
  remotes/origin/story/BRANCH_AC
  remotes/origin/story/BRANCH_AD
  remotes/origin/story/BRANCH_AE
  remotes/origin/story/BRANCH_AF
  remotes/origin/story/BRANCH_AG

That's a lot of remote branches! And most of these don't exist on remote! What's up? Git is internally tracking these branches with remote branches, even though those branches don't exist remotely anymore. Simple enough to clean this up, we run git fetch -p, which

Before fetching, remove any remote-tracking references that no longer exist on the remote. Tags are not subject to pruning if they are fetched only because of the default tag auto-following or due to a --tags option. However, if tags are fetched due to an explicit refspec (either on the command line or in the remote configuration, for example if the remote was cloned with the --mirror option), then they are also subject to pruning. Supplying --prune-tags is a shorthand for providing the tag refspec.

So now we run git fetch -p,

caleb@work MINGW64 /c/git/Some_Project (develop)
$ git fetch -p
From https://tfs.unos.corp/Default/Project/_git/Some_Project
 - [deleted]         (none)     -> origin/effort/BRANCH_E
 - [deleted]         (none)     -> origin/effort/BRANCH_F
 - [deleted]         (none)     -> origin/effort/BRANCH_G
 - [deleted]         (none)     -> origin/effort/BRANCH_H
 - [deleted]         (none)     -> origin/effort/BRANCH_I
 - [deleted]         (none)     -> origin/effort/BRANCH_J
 - [deleted]         (none)     -> origin/feature/BRANCH_K
 - [deleted]         (none)     -> origin/story/BRANCH_L
 - [deleted]         (none)     -> origin/story/BRANCH_M
 - [deleted]         (none)     -> origin/story/BRANCH_N
 - [deleted]         (none)     -> origin/story/BRANCH_O
 - [deleted]         (none)     -> origin/story/BRANCH_P
 - [deleted]         (none)     -> origin/story/BRANCH_Q
 - [deleted]         (none)     -> origin/story/BRANCH_R
 - [deleted]         (none)     -> origin/story/BRANCH_S
 - [deleted]         (none)     -> origin/story/BRANCH_T
 - [deleted]         (none)     -> origin/story/BRANCH_U
 - [deleted]         (none)     -> origin/story/BRANCH_V
 - [deleted]         (none)     -> origin/story/BRANCH_W
 - [deleted]         (none)     -> origin/story/BRANCH_X
 - [deleted]         (none)     -> origin/story/BRANCH_Y
 - [deleted]         (none)     -> origin/story/BRANCH_Z
 - [deleted]         (none)     -> origin/story/BRANCH_AA
 - [deleted]         (none)     -> origin/story/BRANCH_AB
 - [deleted]         (none)     -> origin/story/BRANCH_AC
 - [deleted]         (none)     -> origin/story/BRANCH_AD
 - [deleted]         (none)     -> origin/story/BRANCH_AE
 - [deleted]         (none)     -> origin/story/BRANCH_AF
 - [deleted]         (none)     -> origin/story/BRANCH_AG

Great, it looks like we deleted a ton of branches! Let's check:

caleb@work MINGW64 /c/git/Some_Project (develop)
$ git branch -r
 remotes/origin/effort/BRANCH_A
 remotes/origin/effort/BRANCH_B
 remotes/origin/effort/BRANCH_C
 remotes/origin/effort/BRANCH_D

Ahh, much better.