Skip to content
Related Articles

Related Articles

Difference Between Git remote prune, Git prune and Git fetch –prune

View Discussion
Improve Article
Save Article
  • Last Updated : 17 Jul, 2022
View Discussion
Improve Article
Save Article

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

 

  1. create a branch on one  repo and fetch it on its duplicate
  2. delete that branch from one repo 
  3. 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
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!