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.