Irresistable Call to Action

With Milly child theme, you can create an unlimited number of popup overlays and display any Divi Builder section inside!

How to Delete All Branches Except Master in Git

by | Oct 5, 2023 | blog, GIT | 0 comments

Delete all branches except master/main

As your software project evolves, Git branches are created and used for various features, bug fixes, and experiments.

Over time, these branches can accumulate and clutter your repository. To maintain a clean and organized codebase, you might want to delete branches that are no longer needed.

In this comprehensive guide, we’ll show you how to delete all branches except the master branch in Git.

Whether you’re new to Git or just need a quick refresher, this article will help you streamline your Git repository efficiently.

Understanding the master Branch

What is the master Branch?

In Git, the master branch is often considered the primary branch or the default branch in your repository.

It typically represents the latest stable version of your project’s code.

While the term “master” is commonly used, some projects might choose different names for their main branch, like main or mainline.

List Existing Branches

Before you proceed to delete branches, it’s crucial to know what branches are currently in your repository.

You can list all branches with the following command:

This command will display a list of both local and remote branches.

Delete All Branches Except master

To delete all branches except the master branch, you can follow these steps:

1. Checkout master:

First, switch to the master branch by using the following command:

2. List Branches (Optional):

You can list all branches again to confirm that you are on the master branch.

The * symbol next to master indicates the currently checked out branch.

3. Delete Branches:

Use a loop to delete all branches except master.

The command below will iterate through each branch and delete it:

This command lists all branches, filters out the master branch, and then deletes the remaining branches.

4. Verify Deletion (Optional):

To ensure that all branches except master were deleted, you can list the branches again:

You should see only the master branch listed.

Common Scenarios and Challenges

Handling Unmerged Changes

If any of the branches you want to delete contain unmerged changes, Git will prevent you from deleting them.

In such cases, you can use the -D option instead of -d in the git branch -d command to force delete the branches.

Be cautious when using this option, as it permanently removes branches and their commits.

Backing Up Important Branches

Before performing branch deletion, it’s a good practice to create backups of any important branches.

You can do this by creating a branch from the branch you want to back up.

For example:

This creates a new branch that serves as a backup.

Best Practices

  • Regularly clean up branches that are no longer needed.
  • Keep important branches backed up to prevent accidental deletions.
  • Use meaningful branch names to avoid confusion.

Final Thoughts on Deleting Git Branch Except Master

Cleaning up your Git repository by deleting branches that are no longer in use can help you maintain a clean and organized project history.

By following the steps outlined in this guide, you can easily delete all branches except the master branch, ensuring that your repository remains well-organized and efficient.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

About Website Hurdles

Well, hey there!

I’m Stanley, founder of Website Hurdles and I curate high-quality guides geared towards making money online, profitable blogging and building passive income.

Recent Posts

19 Profitable Pet Business Ideas
19 Profitable Pet Business Ideas

The pet industry continues to thrive as more people welcome pets into their homes and prioritize their well-being. If you're passionate about pets and considering starting a business in this industry, there are numerous opportunities to explore. In this guide, we'll...

7 Best Jobs for People with ADHD
7 Best Jobs for People with ADHD

Attention Deficit Hyperactivity Disorder (ADHD) is a neurodevelopmental disorder that can affect various aspects of life, including work and career choices. However, individuals with ADHD possess unique strengths such as creativity, hyperfocus, and adaptability, which...

7 Highest Paying Business Majors
7 Highest Paying Business Majors

Business majors are in demand across various industries, offering a wide range of career opportunities with competitive salaries. If you're considering pursuing a degree in business, it's essential to explore the highest paying majors within this field. In this...

Display any content!

With Milly child theme, you can create an unlimited number of popup overlays and display any Divi Builder section inside!

Use a Code module to embed an external form, or add a standard Contact Form:

Pin It on Pinterest

Share This