Git

How to Prune Remote Branches in Git

On Git, developers usually work locally, then merge it on a remote repository by pushing the code. Git enables its users to create new branches and switch to them if required. By default, the “main” branch is utilized for working in the root branch of the Git project. Sometimes, users need to prune unneeded remote branches from the Git repository and want to free up the workspace that will make it more efficient. For this purpose, the “$ git remote prune origin” command can be utilized.

This study will discuss the method to prune remote branches in Git. So, go ahead and explore it!

How to Prune Remote Branches in Git?

Suppose you have a Git development project with many branches created in the Git remote repository and fetched into the local repo to perform different operations. However, after performing these operations, you want to delete them from our local repo. To do so, list all branches of the Git repository to check all branches that are available in our repository. Then, run the “$ git remote prune origin” command for the specified purpose.

Let’s go ahead to implement the discussed scenario!

Step 1: Move to Git Repository

Navigate to the Git local repository using the given-below command:

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

Step 2: List Git Branches

Next, list all the existing branches of the Git repository, including both remote and local branches:

$ git branch -a

Here, the list of all available branches is displayed, and we have highlighted the remote branches:

Step 3: Prune Remote Branches

Now, to prune the tracked remote branches, execute the “git remote prune” command with <origin>:

$ git remote prune origin

According to the below output, over remote branches are pruned successfully:

We have learned the easiest procedure to prune remote branches in Git.

Conclusion

To prune the remote branches in Git, first, go to Git local repository and execute the “$ git branch -a” command to list all branches of the Git repository. After that, utilize the “$ git remote prune origin” command to prune the remote tracked branches. In this study, we have demonstrated how to prune remote branches in Git.

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.