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:
Step 2: Clone Remote Repository
Now, run the “git clone” command with the remote repository URL to connect with the local repository:
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:
Step 4: Fetch Updated Remote Repository
Execute the “git fetch” command to download the updated version of the Git remote repository:
Step 5: List Remote Branches
To view all the fetched remote branches, execute the following command:
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”:
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:
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.