Git

Remove Tracking Branches no Longer on Remote

To manage the development project modules, users can create multiple branches on the remote and local repositories. After creating branches, pull and switch to it locally. Then, complete the project development work, commit changes, push them to the remote repository and merge the reviewed code into a main Git remote branch. The last step is to perform a merge operation which tends to remove the Git remote branch automatically. In such a scenario, the “$ git remote prune origin” command removes these tracked branches that are no longer placed on the remote.

This guide will provide the procedure to remove the tracking branches that no longer exist on the Git remote repository.

How to Remove Tracking Branches no Longer on Remote Repository?

To remove tracking branches no longer on the remote, first, move to the particular Git repository. Then, clone the Git remote repository and the new remote URL. Next, fetch the updated remote repository and check the remote branches list. Go to the GitHub hosting service and check the existing branches list. Lastly, execute the “$ git remote prune origin” command to remove the tracking branch reference.

Step 1: Navigate to Git Local Repository

Move to the Git particular repository using the following command:

$ cd "C:\Users\nazma\Git\mari_khan"

Step 2: Clone Remote Repository

Run the “git clone” command and clone the remote repository to the local repository:

$ git clone https://github.com/GitUser0422/demo5.git

Step 3: Add Remote URL

Next, add the remote URL using the “git remote add” command alone with the remote name and remote repository path:

$ git remote add origin https://github.com/GitUser0422/demo5.git

Here, the “origin” is the remote name:

Step 4: Verify Remote URL

Then, verify the added remote repository URL with the help of the “git remote” command with the “-v” option:

$ git remote -v

Execution of the above command will list all the remote connections:

Step 5: Fetch Updated Remote Repository

Now, run the “git fetch” command to fetch the updated remote repository:

$ git fetch

Step 6: List Fetch Remote Branches

To view all remote branches, run the “git branch” command with the “-r” option:

$ git branch -r

According to the below-listed output, the cloned remote repository contains four branches:

Step 7: Check Remote Branches

According to the drop-down branches menu, the remote repository does not contain the “dev” branch, which was fetched earlier on the local repository:

Step 8: Remove Reference

Run the “git remote prune” command with the remote name to remove the deleted remote branch reference:

$ git remote prune origin

It can be observed that the removed tracking branch is pruned successfully:

Step 9: Verify Removed Tracking Branch

Lastly, check the list of the fetch remote repository to verify the removed tracking branch:

$ git branch -r

Below output shows that the required remote branch reference is deleted successfully:

That’s it! We have provided the procedure to remove the tracked branches that are no longer on the remote repository.

Conclusion

To remove tracking branches no longer on the remote, first, navigate to the particular Git local repository. After that, clone the remote repository into the local repository and add the new remote URL. Next, fetch the updated remote repository and view the remote branches list. Open up the GitHub hosting service and check the existing branches list. Lastly, execute the “$ git remote prune origin” command to remove the tracking branch reference. In this guide, we have explained the method of removing tracking branches that no longer exist on the remote repository.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.