How to Use git prune

November 6, 2023


The git prune command deletes all the files in a Git repository that are unreachable from the current branch. prune helps clean up the repository when it gets cluttered with files you no longer need or when you finish working on a project. Therefore, this command reduces the repository size.

In this tutorial, you will learn how to use the git prune command.

How to use git prune - a tutorial.


git prune Syntax

The git prune command runs the git fsck --unreachable command using all the references available in refs/ and then removes the unreachable ones. The git fsck command is often used to diagnose potential issues and to see which objects may be candidates for removal using git prune.

Note: The git gc command is an alternative for git prune, as it identifies and removes unreferenced or "dangling" objects that are no longer reachable from any branch, tag, or reference. The git gc command stands for garbage collection, and it optimizes and cleans up the repository.

The git prune command has the following syntax:

git prune [options]

The available options control the command's behavior, and they are discussed in the section below.

git prune Options

The options are not mandatory. Running git prune without specifying any options causes it to perform its basic function, that is, to remove unreferenced objects from a Git repository.

The options allow you to customize the command's behavior to suit your needs or to provide additional information and control over the pruning process. The following table shows a list of options that git prune accepts:

Short optionLong optionDescription
-n--dry-runPerforms a dry run of the pruning operation to display which objects will be pruned without removing them. The option is useful for previewing the effects of the prune operation.
-v--verboseProvides a more detailed output about the objects being pruned, including object IDs.
-f--forceForces the pruning operation even if risky. This option can override safety checks that might prevent certain objects from being pruned.
--progressDisplays a progress indicator during the pruning process, showing the progress as objects are removed. This option is useful for large repositories, allowing you to track the operation's status.
--expire=[time]Sets a time-based expiration for pruning. Objects older than the specified [time] are removed. This option helps you clean up old objects from your repository.
--Instructs Git to treat everything that follows as arguments, even if they might look like options. This option is helpful in cases where an argument could be mistaken for an option.
[head]Instructs Git to keep objects reachable from the listed [head]s, in addition to objects reachable from any of your references.

How to Use git prune Command

This section provides practical, hands-on examples for using the git prune command.

Basic git prune Usage

The basic git prune usage is to remove objects in your Git repository that are no longer reachable from any of the references (e.g., branches, tags). Unreachable objects accumulate over time as you make commits, create branches, and then delete some branches.

Running the following command removes all those unreferenced objects:

git prune

The command cleans up any unreferenced objects, freeing up disk space and cleaning up your repository. There is no output, but if you want to see which objects are pruned, add the --verbose option:

Running the git prune command with the verbose option.

Git deletes all unreachable objects and outputs a list of removed objects.

Perform Dry Run

The dry run feature lets you see which objects the pruning action removes without removing them. Run the git prune command with the --dry-run option to output a list of unreachable objects that the action would prune:

git prune --dry-run
Performing a dry run of git prune.

The command outputs a list of unreachable objects without deleting them. The list includes the object ID and its type.

Force Prune Unreachable Objects

To make sure all unreachable objects are removed from the repository without asking for confirmation, specify the --force option:

git prune --force

Be cautious when using the --force option, as pruning objects permanently removes them from the repository.

Prune Remote Branches

If you collaborate on a project with a remote repository, your repository may accumulate unnecessary remote tracking branches over time. To remove unreachable objects associated with those remote branches, use the git remote prune command with the remote repository name.

For example:

git remote prune origin

The command removes all references to branches on the origin remote that no longer exist on the remote repository. It also prunes unreachable objects associated with those branches.

Pruning with an Expiration Date

The --expire [time] option allows users to specify the minimum age of loose objects that will be deleted. Git stores loose objects in separate files rather than in a packed file. They are created when you first clone a repository or when you run git checkout or git switch to switch to a different branch.

Use the --expire option to specify a time or date from which objects are eligible for pruning. You can specify [time] in different ways:

  • A relative time, such as 1.week or 6.months. Git deletes loose objects older than the specified time relative to the current time.
  • An absolute time, such as 2023-11-03 13:25:37 PST. Git deletes loose objects older than the specified time.
  • A special value, such as now or never. The value now makes Git delete all loose objects immediately, regardless of their age. The value never instructs Git not to delete any loose objects, even if they are old.

For example, to remove objects that are older than 14 months and see the operation results, run the following command:

git prune --expire=14.months --verbose
Pruning Git objects older than a specific time.

This command prunes objects that haven't been reachable for at least 14 months and outputs the list of pruned objects.


This tutorial showed how to use the git prune command to delete all unreachable objects from your repository, allowing you to clean up the repository. Git prune helps remove clutter after you finish working on a project and saves disk space.

For more Git tutorials, learn about different branching strategies or see how to delete a Git branch remotely and locally.

Was this article helpful?
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
How to Set Up SSH and Clone Repository Using SSH in Git
November 2, 2023

This article shows how to set up SSH, a secure network protocol, and use it to clone a Git repository to a local machine. SSH is a network protocol that boosts data transmission security.
Read more
How to Cherry-Pick from Another Branch in Git
October 2, 2023

Learn to use cherry-picking in Git and selectively incorporate changes from one branch into another. This tutorial shows how to use cherry-pick and explains the pros and cons of this Git feature.
Read more
How to Merge a Git Branch into Master
October 2, 2023

Merging is an essential Git procedure that brings multiple lines of development together. This tutorial shows how to merge a local branch into the master branch.
Read more
How to Compare Two Git Branches
August 23, 2023

This tutorial shows several methods for comparing two Git branches. Comparing branches is important before merging to ensure all conflicts and bugs are eliminated before changing the main codebase.
Read more