One of the great things about Git is that it’s *very careful about deleting data*. This makes it pretty hard to lose commits or other valuable data in Git! A small downside of this is that you might sometimes see stale data that you actually 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 deletes a branch on your shared remote repository; the branch will still be displayed for you unless you explicitly instruct Git to clean up.
Approach
In this article, we’ll look at how to do this using the `prune` option in Git.
Using “prune” on a Remote Repository:
“prune” is available as an option for the `git fetch` and `git remote` commands. ( `git prune` command – is used during garbage collection.). The easiest way to use prune is to provide it as an option when fetching:
Command: git fetch --prune origin
In cases where you’d like to *only* perform a prune and *not* fetch remote data, you can use it with the `git remote` :
Command: git remote prune origin
The result is the same in both cases: stale references to remote branches that don’t exist anymore on the specified remote repository will be deleted. By the way: you never have to worry about your local branches, since prune will never affect those.
clone the same repo twice, so that you properly understand the working of git prune.
git clone repolink
- create a branch on one repo and fetch it on its duplicate
- delete that branch from one repo
- when you list the branches on the other repo it will not get updated.
git branch
git push origin HEAD
git branch -r
use git fetch –prune:
The branch will be automatically updated in the 2nd repo if we use the prune command to delete
git fetch --prune
Suppose in some cases where you’d like to *only* perform a prune and *not* fetch remote data
git remote prune origin
If you want automatically prune itself
git config —global fetch.prune true
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
17 Jul, 2022
Like Article
Save Article