Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • jQuery
  • AngularJS
  • ReactJS
  • Next.js
  • React Native
  • NodeJS
  • Express.js
  • MongoDB
  • MERN Stack
  • PHP
  • WordPress
  • Bootstrap
  • Tailwind
  • CSS Frameworks
  • JS Frameworks
  • Web Development
Open In App
Next Article:
Difference Between Git remote prune, Git prune and Git fetch --prune
Next article icon

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

Last Updated : 11 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Git is a version control system that helps developers manage and track changes in their codebase. Among its many commands, git remote prune, git prune, and git fetch --prune are essential for maintaining a clean and organized repository. This article will explain the differences between these commands, their syntax, uses, and provide examples to illustrate their functionalities.

What is git remote prune?

git remote prune is a command used to clean up references to remote branches that no longer exist in the remote repository.

Syntax

git remote prune <remote-name>

Uses of git remote prune

  • To remove stale references to remote branches that have been deleted from the remote repository.
  • Helps to keep your local repository clean and up-to-date with the remote repository.

Example

# Prune stale remote-tracking branches from origin git remote prune origin

This command will remove any branches in your local repository that no longer exist on the remote named origin.

What is git prune?

git prune is a low-level Git command that removes objects that are no longer referenced by any object in the repository. This is useful for cleaning up unnecessary objects that can accumulate over time.

Syntax

git prune

Uses of git prune

  • To clean up unreachable objects from the repository.
  • Helps reduce the size of the repository by removing unnecessary objects.

Example

# Prune all unreachable objects from the repository git prune

This command will remove all objects that are not reachable from any branch or tag in the repository.

What is git fetch --prune?

git fetch --prune is a command that combines the functionality of git fetch and git remote prune. It fetches updates from the remote repository and removes any remote-tracking branches that no longer exist on the remote.

Syntax

git fetch --prune <remote-name>

Uses of git fetch --prune

  • To update your local repository with the latest changes from the remote repository.
  • Automatically removes stale remote-tracking branches that have been deleted from the remote.

Example

# Fetch updates from origin and prune stale remote-tracking branches git fetch --prune origin

This command will fetch the latest changes from the origin remote and remove any branches in your local repository that no longer exist on the remote.

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
Difference Between Git remote prune, Git prune and Git fetch --prune
  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
Difference Between Git remote prune, Git prune and Git fetch --prune

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
Difference Between Git remote prune, Git prune and 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

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

Featuregit remote prunegit prunegit fetch --prune
Primary FunctionRemoves stale remote-tracking branchesRemoves unreachable objectsFetches updates and prunes stale branches
Syntaxgit remote prune <remote-name>git prunegit fetch --prune <remote-name>
Usage ContextCleaning up remote-tracking branchesCleaning up repository objectsUpdating and cleaning remote branches
Examplegit remote prune origingit prunegit fetch --prune origin
ScopeRemote-tracking branchesRepository objectsRemote-tracking branches and updates

Conclusion

Understanding the differences between git remote prune, git prune, and git fetch --prune is important for maintaining a clean and efficient Git repository. Each command serves a unique purpose in managing and organizing your repository. git remote prune focuses on cleaning up remote-tracking branches, git prune removes unreachable objects, and git fetch --prune combines fetching updates with pruning stale branches.


Next Article
Difference Between Git remote prune, Git prune and Git fetch --prune

V

vishant8016
Improve
Article Tags :
  • Web Technologies
  • Git

Similar Reads

    Difference Between Git Fetch and Git Pull
    Understanding the difference between git fetch and git pull is important for effective version control in Git. Git Fetch and Git Pull are two important commands in Git that help in managing remote repositories. While both commands involve retrieving data from remote repositories, they serve distinct
    5 min read
    Difference Between Git Push Origin and Git Push Origin Master
    Understanding the difference between git push origin and git push origin master is important for efficient version control in Git. These commands are used to upload changes from your local repository to a remote repository, but they function differently. This article will explain these differences,
    3 min read
    Git - Difference Between Git Revert, Checkout and Reset
    Git offers a range of commands to manage and manipulate your codebase. Among these commands, git revert, git checkout, and git reset are frequently used for different purposes. Understanding the differences between these commands is important for effective version control. In this article, we'll exp
    6 min read
    Git - Difference Between HEAD, Working Tree and Index
    Git, a popular version control system, helps developers track changes and collaborate on projects efficiently. To use Git effectively, it's essential to understand its key components: HEAD, the working tree, and the index (also known as the staging area). This article will explain these concepts and
    4 min read
    Difference Between Git and GitHub
    Git: Git is a distributed version control system for tracking changes in source code during software development. It is designed for coordinating work among programmers, but it can be used to track changes in any set of files. Its goals include speed, data integrity, and support for distributed, non
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences