Git

Cleaning Up Old Remote Git Branches

While working on a large project on Git, developers create different branches for multiple features. However, it can be complicated for them to have a reference for each branch in the Git repository. In this situation, cleaning up the unused old branches is required once they are no longer used. More specifically, cleaning up old remote branches can be done in several ways.

In this write-up, we will discuss:

Method 1: How to Clean Up Old Git Remote Branches Utilizing the β€œgit branch” Command?

Sometimes, developers want to delete the unused remote branch from their local repository but want to keep it in the remote repository. So, they need to delete the remote branch locally. To do so, run the β€œgit branch -r -d <remote>/<branch-name>” command.

Step 1: Move to Local Git Repository

Utilize the β€œcd” command for redirecting to the particular repository:

$ cd

Step 2: Check List of All Branches

Then, execute the β€œgit branch” command along with the β€œ-a” option to view the list of all local and remote branches in the repository:

$ git branch -a

The below output indicates that the current repository contains four local and two remote branches:

Step 3: Delete Remote Branch

Write out the β€œgit branch” command and add the remote branch that needs to be deleted. For instance, in our case, we want to delete the remote β€œmain” branch:

$ git branch -r -d origin/main

Here, β€œ-r” and β€œ-d” options are used to delete the branch recursively:

Step 4: Verify List of branches

In order to ensure whether the remote branch has been deleted from the local repository or not, run the below-provided command:

$ git branch -a

It can be observed that the local repository has only a β€œmaster” remote branch and the remote β€œmain” branch has been deleted from it:

Method 2: How to Clean Up Old Git Remote Branches Utilizing the β€œgit push” Command?

While working on Git, developers can delete a single remote branch. They can do it by executing the β€œgit push <remote> –delete <branch>” command.

Step 1: Check List of All Branches

First, view the list of all available branches in the current repository by running the below-provided command:

$ git branch -a

Step 2: Remove Old Remote Branch

Then, run the β€œgit push” command along with the remote branch name to delete it:

$ git push origin --delete master

Here:

  • β€œorigin” is a remote URL name.
  • β€œ–delete” option is used to remove the branch.
  • β€œmaster” is the remote branch that needs to be deleted.

It can be observed that the remote β€œmaster” branch has been deleted or removed from the GitHub repository:

Step 3: Verify the Changes

Check whether the remote branch has been deleted or not by viewing the list of remote branches in the repository:

$ git branch -a

It can be seen, the deleted branch does not exist in the local repository:

Method 3: How to Clean Up Old Git Remote Branches Utilizing the β€œgit remote prune” Command?

Sometimes, users have branches in their local machine that have been removed from the GitHub repository. So, they need to delete those remote branches from their local repository. Use the β€œgit remote prune <remote>” command for this corresponding purpose.

Look at the following steps for a better understanding!

Step 1: Check Remote Branches in Local Git Repository

Execute the following command to display the list of all existing branches:

$ git branch -a

It can be observed that there are two β€œmain” and β€œmaster” remote branches present in the local repository:

Step 2: Check Remote Branches in Remote Repository

Then, open GitHub hosting service and redirect to the particular remote repository. Then, check the list of remote branches in that repository:

Note: Here, you can see that there is only one branch, i.e., the β€œmain” branch in the remote repository. However, the local repository contains β€œmaster” and β€œmain” two remote branches. Therefore, it is required to update the local repository with the latest version of the remote repository through pruning.

Step 3: Remove the Remote Branch From Local Repository

Now, delete the remote branch from the local Git repository and update it using the below-stated command:

$ git remote prune origin

Step 4: Verify the New Changes

Next, view the list of all branches to ensure whether the remote branch has been deleted from the local repository or not:

$ git branch -a

According to the below screenshot, the local repository has successfully updated with the remote repository and now it has only one β€œmain” branch:

We have explained the different ways of cleaning up old remote branches in Git.

Conclusion

In order to clean up the remote branch from the local Git repository only, utilize the β€œgit branch -r -d <remote>/<branch-name>” command. If the user wants to delete a single remote branch, then execute the β€œgit push <remote> –delete <branch>” command. Whereas the β€œgit remote prune origin” command cleans the deleted remote branches from the local repository. This article demonstrated the method of cleaning up old remote Git branches.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.