Horje
Remove Tracking Branches No Longer On Remote In Git

When working with Git, it’s common to deal with remote branches, which are branches on a remote repository. Over time, some of these remote branches may be deleted or renamed. However, your local repository might still have references to these old branches. This can clutter your branch list and cause confusion. In this article, we’ll explore how to remove tracking branches that are no longer present on the remote repository.

What are Tracking Branches?

Tracking branches are local branches that have a direct relationship with a branch on a remote repository. They allow you to collaborate with others by tracking changes made on the remote repository. When a remote branch is deleted, its tracking branch remains in your local repository until you manually clean it up.

Why Remove Old Tracking Branches?

Removing old tracking branches is important for maintaining a clean and manageable repository. Some benefits include:

  • Reduced Clutter: Fewer branches to navigate, making it easier to find relevant branches.
  • Accurate Reflection of Remote: Your local repository accurately reflects the current state of the remote repository.
  • Avoid Confusion: Prevents confusion caused by outdated branches that no longer exist on the remote.

Approach 1: Using git fetch -p

The -p (or –prune) option with git fetch will automatically remove any remote-tracking branches that no longer exist on the remote.

Step 1: Open your terminal or command prompt.

Step 2: Navigate to your Git repository directory using cd /path/to/your/repo.

Step 3: Run the command:

git fetch -p

Output:

1. Go to the project directory:

Screenshot-2024-06-13-103258

cd rohit/zillow

2. Run this command :

Screenshot-2024-06-13-105304

git fetch -p

After running git fetch -p, Git will do the following:

  1. Fetch any new commits from the remote repository to your local repository.
  2. Update your remote-tracking branches (e.g., origin/main, origin/develop) to reflect the latest state of the remote branches.
  3. Remove any remote-tracking branches that were deleted on the remote repository since the last fetch. This ensures that your local references accurately reflect the current state of the remote repository.

Approach 2: Using git remote prune

The git remote prune command is specifically designed to prune all branches of a given remote.

Step 1: Open your terminal or command prompt.

Step 2: Navigate to your Git repository directory using cd /path/to/your/repo.

Step 3: Run the command:

git remote prune origin

Purpose: This command is used to prune (remove) stale remote tracking branches from your local repository that have been deleted on the remote repository (origin).

Execution: When you run git remote prune origin, Git communicates with the remote repository (origin) and checks for any remote tracking branches that exist locally but no longer exist on the remote.

Output: If there were any branches that were pruned (removed), Git would typically list them in the output. However, in your case, since there was no output and assuming no error messages were displayed, it indicates that there were no stale remote tracking branches to prune.

Screenshot-2024-06-13-110742

git remote prune origin

Approach 3: Using git branch -r and Manual Deletion

If you prefer to manually verify which branches to delete, you can list remote-tracking branches and delete them one by one.

Step 1: Open your terminal or command prompt.

Step 2: Navigate to your Git repository directory using cd /path/to/your/repo.

Step 3: List all remote-tracking branches

git branch -r

Output

Screenshot-2024-06-13-112218

git branch -r

  • origin/HEAD -> origin/main: The HEAD of the remote origin is pointing to the main branch.
  • origin/approach_3_branch: This is the new branch fetched from the remote repository.
  • origin/main: The main branch on the remote repository.

Step 4: Identify branches that no longer exist on the remote.

Step 5: Delete each stale branch manually using

git branch -dr <remote/branch-name>

Output:

Screenshot-2024-06-13-112923

git branch -dr origin/approach_3_branch

This does not delete the branch from the remote repository itself, but only removes your local reference to it




Reffered: https://www.geeksforgeeks.org


Git

Related
How To Change Folder With Git Bash? How To Change Folder With Git Bash?
How to Revert Multiple Git Commits? How to Revert Multiple Git Commits?
How To Undo Pushed Commits Using Git? How To Undo Pushed Commits Using Git?
How to Undo “git commit –amend&quot;? How to Undo “git commit –amend&quot;?
MERN Stack Project SetUp - A Complete Guide MERN Stack Project SetUp - A Complete Guide

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
16