Git

How to Prune Local Tracking Branches That Do Not Exist on Remote Anymore

When developers work on large projects, they divide them into modules and make it easy for every team member to work on them. On Git, they deal with the branches by working and then pushing them to the remote hosting service. Sometimes, the tracked branches get deleted from the remote repositories, and you may want to update the local repository with that. Therefore, it is required to perform the pruning with the “$ git remote prune origin” command.

This post described the method to prune the Git local tracking branches removed from the remote repository.

How to Prune Git Local Tracking Branches That Do Not Exist on Remote Anymore?

To prune the local tracking branches that do not exist on the remote anymore, first, move to the particular Git repository and build a connection between the local and the remote repository through cloning. After that, fetch the updated remote repository and view the remote URL list. Next, display all tracking remote branches. Go to the GitHub Hosting service and show the existing list of remote branches. Lastly, prune local tracking branches by executing the “$ git remote prune origin” command.

Now, implement the above-listed instruction!

Step 1: Navigate to Git Repository

Move to the Git local repository by executing the “cd” command:

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

Step 2: Clone Remote Repository

Now, run the “git clone” command with the remote repository URL to connect with the local repository:

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

Step 3: Check Remote URL List

In order to check the remote URL list for tracking from the remote repository to the local repository, run the “git remote” command along with the “-v” option:

$ git remote -v

Step 4: Fetch Updated Remote Repository

Execute the “git fetch” command to download the updated version of the Git remote repository:

$ git fetch

Step 5: List Remote Branches

To view all the fetched remote branches, execute the following command:

$ git branch -r

Step 6: Verify Remote Branches From GitHub

Next, open up the GitHub hosting service and check the remote branches list, as according to the below-listed snippet, the fetched “dev” branch is deleted from the remote repository and does not exist anymore:

Step 7: Prune Local Tracking Branches

Finally, execute the “git remote prune” command along with the remote name “origin”:

$ git remote prune origin

It can be seen that the “dev” branch is pruned:

Step 8: Verify Prune Local Tracking Branch

Lastly, run the “git branch” command along with the “-r” option to ensure that the deleted tracked branch is pruned from the repository:

$ git branch -r

That’s all! We have pruned the local tracking branches removed from the remote repository.

Conclusion

To prune the local tracking branches that do not exist on the remote anymore, first, navigate to the Git repository and connect the local repository with the remote repository through cloning. Then, fetch the updated remote repository and check the remote URL list. Next, view all tracking remote branches. After that, open up the GitHub Hosting service and view the existing list of remote branches. Lastly, execute the “$ git remote prune origin” command. This post described the method to prune the local tracking branches deleted from 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.