How to Use Git Prune to Remove Outdated References in Git
How to Use Git Prune to Remove Outdated References in Git
What isgit prune?
git pruneis a Git command used to prune or remove unreachable objects, such as commits and blobs, that are no longer referenced by any branch ortagin a repository. These objects might exist due to branch deletions,rebases, orresets. By cleaning up these objects,git prunehelps maintain an optimized repository, improve performance, and reduce disk space usage.
It’s important to note thatgit pruneoperates only on objects that are truly unreachable. If an object is referenced by any branch, tag, orreflogentry, it will not be pruned. Pruning is often used automatically with Git’s garbage collection mechanism to ensure the repository is efficiently maintained.
In the next section, we’ll learn how to usegit pruneto remove outdated references locally in Git.
How to remove outdated references locally in Git
Thegit prunecommand is a handy tool when it comes to removing unreachable objects locally. Before starting the process, fork and clone thedocsremote repository to your local machine to follow along with the tutorial.
Now, while on the repository locally, the first step is to run this command in theterminal:
git prune --dry-run
Copy to clipboard
git prune --dry-run
The--dry-runflag with thegit prunecommand lists all the local outdated references in the repository that can be pruned. Here is a sample output:
01268db12e0e708a525a4d4291625a4edc842784 commit01a8025a253f5574213041277487b95c0a4b4148 commit01de9fd25808933693cfe9a7de30bf87a44176dd commit...
Copy to clipboard
01268db12e0e708a525a4d4291625a4edc842784 commit01a8025a253f5574213041277487b95c0a4b4148 commit01de9fd25808933693cfe9a7de30bf87a44176dd commit...
All the references that we can see in the output are commits. However, the references can include blobs (files), trees (directory snapshots), and tags as well.
Then, run thegit prunecommand to get rid of all these references at once:
git prune
Copy to clipboard
git prune
Sometimes, a few references that can be pruned don’t appear on the list shown after executinggit prune --dry-run, because they are still referenced in the reflog. The reflog is a record of all the actions that a user performs in Git, including branch switches, rebases, resets, and commits. These references expire from the reflog after a particular amount of time. However, if we want to remove them now, we can run this command:
git reflog expire --expire-unreachable=now –all
Copy to clipboard
git reflog expire --expire-unreachable=now –all
In this command:
- git reflog expire: Expires or removes a reference from the reflog.
- --expire-unreachable=now: Sets the expiry date of all the local unreachable references tonow, effectively setting them up to be removed immediately.
- --all: Targets all the reflog entries that currently exist in the repository.
We can also use thegit prunecommand to set an expiration time for the local outdated references, after which they will be pruned:
git prune --expire=3.weeks.ago
Copy to clipboard
git prune --expire=3.weeks.ago
Here, the--expireflag sets an expiration time of3.weeks.ago, which indicates that Git will remove all the references that have been unreachable for more than 3 weeks. Some more examples of the expiration time can be2.months.ago,5.days.ago,yesterday, etc.
We discussed how to remove outdated references locally in Git, but what about removing them remotely?
In the next section, we’ll learn how to remove outdated references remotely in Git.
How to remove outdated references remotely in Git
When working on aremote repository, we may occasionally need to delete remote branches that are no longer required. Upon deletion, they are removed from the repository, but the references to these remote branches aren’t. Instead, these references exist in the repository as remote outdated references.
To remove these references, we can use thegit fetchcommand with--pruneflag:
git fetch --prune origin
Copy to clipboard
git fetch --prune origin
- git fetch: Fetches the latest changes (including any new remote outdated references) fromoriginto the local repository.
- --prune: Removes all the remote outdated references in the repository.
- origin: An alias for the forkeddocsremote repository.
If we want to remove outdated references remotely without fetching the latest changes, we can use this command instead:
git remote prune origin
Copy to clipboard
git remote prune origin
- git remote: Specifies the target remote repository (origin).
- prune: Removes all the remote outdated references in the repository.
Now that we’re done discussing how to remove outdated references locally and remotely in Git, let’s have a look at the advantages of usinggit prune.
Advantages of usinggit prune
The several advantages of using thegit prunecommand include:
- Reduces Clutter: Helps keep the repository clean by removing obsolete references.
- Frees Up Space: Reclaims disk space by deleting unreachable objects.
- Improves Performance: Enhances the performance of a repository by deleting unnecessary objects.
- Prevents Confusion: Reduces the chances of accidental usage of obsolete branches or commits by eliminating outdated references.
With the advantages covered, let’s next look at the ideal scenarios for usinggit prune.
When to usegit prune?
We can use thegit prunecommand in multiple scenarios, such as:
- After deleting branches to remove unnecessary references.
- When cleaning up stale references after performing afetchoperation.
- After performing a rebase or reset, which can leave behind unreachable commits that are no longer needed.
Conclusion
In this tutorial, we explored thegit prunecommand and learned how to remove local and remote outdated references in Git. We also covered its advantages and the ideal scenarios for using it.
Keeping our Git repositories clean usinggit pruneensures efficient repository management, better performance, and easier navigation of branches. Regular pruning helps maintain a well-structured repository, preventing unnecessary clutter and confusion.
If you want to learn more about Git branching and collaboration, check out theLearn Git: Branching and Collaborationcourse on Codecademy.
Frequently asked questions
1. What does git prune do?
Git prune removes unreachable objects and outdated references from your repository.
2. How do I use git prune safely?
Always rungit prune --dry-runfirst to preview changes, thengit pruneto execute.
3. Is git prune safe to use?
Yes, git prune only removes truly unreachable objects. It won’t delete anything referenced by branches or tags.
4. When should I run git prune?
After deleting branches, performing rebases, or when your repository feels cluttered.
5. What’s the difference between local and remote pruning?
Local pruning removes unreachable objects in your repository. Remote pruning removes references to deleted remote branches.
'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'
Complete Payment to Unlock Full Guide & Source Code
You are viewing the 30% preview. Join TopCourseAI Pro to unlock the remaining 70% of this article, unblur all code snippets, and gain full access to all 412 technical projects and guides.
Ready to access the complete step-by-step tutorial?
Gain full unhindered access to all 412 technical projects and guides, unblur all source code snippets, and download runnable scripts.
How to Use Figma Sites to Build Website
Next BlueprintHow to Use Git Rebase: A Complete Guide
Related Blueprints & Guides
View all 166 →Android Studio Quick-Start Guide
Android Studio often intimidates beginners, but by learning a few handy shortcuts and getting the lay of the land, we can navigate this powerful development environment with ease.
Developer toolsApache Kafka Tutorial: A Complete Guide for Beginners
Apache Kafkais a well-known distributed event streaming platform designed to efficiently manage real-time data feeds. It was originally developed by LinkedIn in 2011 and later open-sourced. Apache Kafka has gained immens...
Developer toolsBuilding Websites with Wix
Creating a website has never been easier. Previously, making a presentable website meant learning HTML, CSS, andJavaScript. The whole process could take weeks, months, or even years before creating a website that you’re...
Ready to build production-grade AI systems?
Enjoyed this guide? Continue exploring our library of 166+ production blueprints, multi-agent swarms, and autonomous agent systems with 100% unblurred, executable source code.