Git prune is used to remove the unreachable objects and clear up the local repository.
Git is a powerful version control system that helps developers manage their code efficiently. However, as your project evolves, you may accumulate unnecessary data that clutters your repository. This is where git prune comes in view.
What is git prune?
git prune is a Git command that removes objects that are no longer referenced by any branch or tag. These unreferenced objects, often called “dangling objects”, can accumulate over time as you make changes to your repository. Pruning these objects helps free up disk space and keeps your repository organized.
Syntax:
git prune [options]
Command options:
- -n or –dry-run: is used to just display the removable items in the repository.
- -v or -verbose: provide details of pruning process.
Note: It operates on the hidden data and usually runs in conjunction with Git gc.
What are Git Objects?
Git stores data as objects within its database. These objects include:
- Commits: Representing snapshots of your project at different points in time.
- Trees: Representing directories and their structure.
- Blobs: Representing individual files.
Over time, as you create, modify, and delete files and commits, Git’s internal database can accumulate objects that are no longer referenced by any branch or tag. These objects become “unreachable” or “dangling” and can consume unnecessary disk space.
Why Usegit prune?
- Free Up Disk Space: Over time, your Git repository can become cluttered with unused objects, which consume disk space. Pruning helps to remove these objects and free up space.
- Improve Performance: A cleaner repository with fewer dangling objects can lead to better performance when running Git commands.
- Maintain Organization: Regular pruning helps keep your repository clean and well-organized, making it easier to manage and navigate.
When to Use git prune
git prune is particularly useful in the following scenarios:
- After Deleting Branches: When you delete branches, the commits associated with those branches might still exist in your repository as dangling objects. Pruning helps remove these unreferenced commits.
- After Rewriting History: If you’ve used commands like git rebase or git filter-branch, you may end up with orphaned commits that need to be pruned.
- Regular Maintenance: Periodically running git prune as part of your repository maintenance routine helps keep it clean and efficient
How to Use
Git prune is a command that deletes all the files that are not reachable from the current branch. The prune command is useful when you have a lot of files in your working directory that you don’t want to keep. The primary use of git prune is to clean up your working directory after you have finished working on a project.
What actually git prune does is, suppose you have an object or commit that is no longer reachable from the current branch. Then git prune will delete that object or commit. The basic idea is that git prune is a way to clean up your working directory to make it lighter and easier to work on a project.
Cleaning Up Your Local Repository
To clean up your local working directory, use:
Syntax:
git fetch --prune <remote>
This command deletes objects that are not reachable from the remote repository. If you want to prune the remote repository without fetching it, use:
git remote prune origin
Configuring Automatic Pruning
To configure Git to automatically prune the remote repository when you fetch it, run:
git config --global fetch.prune true
Example of git prune
Consider this scenario, suppose you have a project that you are working on. You have a bunch of files in your working directory, and a commit becomes unreachable from the current branch. Then you want to delete all the objects or commit that are not reachable from the current branch.
1. creating a new repository and initializing it
mkdir git-prune-demo-geeks
cd git-prune-demo-geeks
git init .
echo “Hello World” > hello.txt
git add hello.txt
git commit -m “first commit”
The above command sequence will:
- create a new repository called git-prune-demo-geeks
- change the current directory to git-prune-demo-geeks
- initialize the repository
- create a file hello.txt with the content “Hello World”
- add the file hello.txt to the staging area
- commit the file hello.txt with the message “first commit”
Output:

creating repository
2. Modify the file hello.txt and create a new commit
echo “Hello from the other side” > hello.txt
git add hello.txt
git commit -m “second commit”
Output:

Adding commit
This will add the line “Hello from the other side” to the file hello.txt and commit it with the message “second commit”. To verify the above command sequence, we can use the following command:
git log
Output:

git log output
Here, the git log will display the commit history of the repository.
3. Making commit unreachable from the current branch
git reset –hard HEAD~1
Output:

git reset
Here, HEAD~1 is the commit that is unreachable from the current branch. And now if we try to see the git log output again, we will see that the commit is no longer reachable from the current branch, it will show only the commit that is reachable from the current branch.
git log
Output:

git log
This repository now has a commit that is a detached commit. So if you try to check out the commit, it will not check out the commit but will create a detached branch. The second commit is a detached commit and no longer showing in the git log output.
git checkout HEAD~1
Output:

The HEAD~1 is the commit that is detached from the current branch.
4. Running git prune
After checkout the detached commit, we have to return back to the master via the git checkout command and then run the git prune command. We have to pass certain options to the git prune command so that the output of the command displays what is set to be pruned.
git prune –dry-run –verbose
Output:

git prune
Here, empty output means that nothing is pruned. Because somewhere in the repository, git is keeping the commit reference that is detached from the current branch. So git prune will not delete the commit. And to conclude our scenario, we must have to clear reflog first.
git reflog expire –expire=now –expire-unreachable=now –all
This will forcefully expire the reflog. After removing the reflog we can run the git prune command again.
git prune –dry-run –verbose –expire=now
Output:

Pruned the unreachable commit
This will result in displaying git SHA object references of commit and tree objects that are no longer reachable from the current branch.
Similar Reads
Rashi/Zodiac Chart: Know Your Rashi by Name and Date Of Birth
The Indian astrology system recognizes 12 Rashis or Zodiac Signs, each with distinct characteristics. The Moon sign, determined by the Moon's placement at birth, is crucial in Vedic Astrology. Understanding Zodiac Signs helps individuals discover their personality traits, compatibility, and what the
13 min read
100+ Happy Birthday Wishes: Sweet Quotes & Message ideas
Birthday is the most important day in anyone's life. So wishing your friends, relatives, or colleagues a Happy birthday will make the bond super strong. We have crafted the Top 100 Happy Birthday wishes that you can share on the eve with your friends, partner, or any colleague. Happy Birthday Wishes
14 min read
15 Best IPTV Service Providers Subscriptions (Top Picks)
Finding a reliable IPTV service provider can be challenging with so many options available. As streaming becomes the new standard for entertainment, IPTV services have gained huge popularity by offering a wide variety of TV channels, on-demand content, and international programmingâall through an in
14 min read
Best Free Movie Download Sites for 2024
Want to watch a movie whenever you want? Simply download it to your device and watch it later, even if you don't have an internet connection. This is called "streaming it locally." The best part about downloading a movie is that it's always ready for you, no matter where you are! Most legal movie do
9 min read
6 Best ChatGPT Plugin for Summarizing YouTube Videos Quickly
Presenting the best ChatGPT plugin for YouTube summary that will transform media consumption. Gathering important ideas from long YouTube videos in a world full of information can be intimidating. It reduces intricate information into a brief synopsis, enabling you to quickly assimilate the main poi
9 min read
ChatGPT Tips & Tricks: 5 Secret Tips That Can Boost Your ChatGPT Game to God Mode
Are you one of those folks who've tried using ChatGPT but ended up feeling a bit frustrated? Well, let us tell you, you're not alone in this sad situation. Whether you're a student or a tech whiz working in IT, ChatGPT has been a helpful companion for everyone! But the thing is, not everyone knows h
7 min read
JavaScript Tutorial
JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. JavaScript is an interpreted language that executes code line by line providing more flexibility. HTML adds Structure to a web page, CSS st
11 min read
IPTV España: Los 10 Mejores Proveedores de Listas IPTV en España
En España, IPTV se ha consolidado como la opción preferida de muchos usuarios en España. En 2025, encontrar los mejores proveedores de listas de IPTV se ha convertido en una tarea crucial para disfrutar de una experiencia televisiva excepcional. Este artÃculo explora el universo de IPTV España, desg
10 min read
How to Download and Install the Google Play Store
The Google Play Store is the heartbeat of your Android experienceâhome to millions of apps, games, and updates that keep your device functional, fun, and secure. But what if your phone or tablet doesnât have it pre-installed? In this step-by-step guide, youâll learn how to safely download and instal
6 min read
HTML Tutorial
HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
11 min read