Last Updated On By Khizer Ali
The ‘git prune command’ is an internal housekeeping utility that cleans up un-reachable or “orphaned” Git objects. Un-reachable objects are those that are inaccessible by any refs. Any commit that cannot access through a branch or tag is considered un-reachable.
One of the better things about Git is that it’s cautious about deleting data, making it pretty hard to lose commits or valuable data in git!
A tiny downside of this is that you sometimes see stale data that you don’t need anymore. One of the best examples of this is references to remote branches that have already been deleted.
Let’s say that one of your teammates removes a branch on your shared remote repository; it will still display the branch for you unless you explicitly instruct git to scrub up.
“prune” is available as a choice for the git fetch and git remote commands. (Do not confuse this with the stand-alone git prune command – this is used during garbage collection and is not what we were talking about here.)
The easiest way to use git prune is to provide it as an option when fetching:
$ git fetch --prune origin
In cases where you’d like to perform a prune and not fetch remote data, you can use it with the git remote command:
$ git remote prune origin
The result will be identical in both cases: stale references to remote branches that do not exist anymore on the desired remote repository will be removed. By the way: you never have to bother about your local branches, since ‘prune’ will never affect those.
If you want to have ‘prune’ executed with every fetch operation, you can configure git accordingly:
$ git config --global fetch.prune true