This guide will provide a comprehensive overview of the git prune
command, its use cases, and related commands for pruning in Git.
What does the git prune
command do?
The git prune
command is used to remove unreachable objects from the .git
directory, which are objects that are no longer accessible from any of the references in the repository. These could be old commits, unused blobs, or other data that's no longer referenced in any branch, tag, or other reference. Running git prune
helps to optimize the repository by cleaning up these leftovers, reducing clutter and reclaiming disk space.
Basic usage
To understand git prune
, let’s first look at the basic usage:
git prune
This command is typically used internally by Git, and is run as part of the routine garbage collection protocol.
git prune
options
While the git prune
command itself has limited options, its functionality can be leveraged through other Git commands, like fetch
and branch
.
Using git fetch
with prune
git fetch
combined with the --prune
option is a common way to not only update the local references with the remote but also to clean up any remote-tracking branches that no longer exist on the remote repository:
git fetch --prune
Or for a specific remote:
git fetch origin --prune
This command updates your local repository’s remote-tracking branches to reflect the current state of the remote and removes the local remote-tracking branches that have been deleted on the remote.
Remember, git fetch --prune
only affects remote-tracking branches. To delete non-tracking local branches see this guide on deleting Git branches.
For further information see the official Git documentation.