How to list all local branches merged into master in Git?

GitGitBeginner
Practice Now

Introduction

Git is a powerful version control system that allows developers to manage and collaborate on code effectively. One common task in Git is to list all local branches that have been merged into the master branch. This information can be useful for cleaning up your local repository and maintaining a organized codebase. In this tutorial, we will guide you through the process of listing all local branches merged into master in Git.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/BranchManagementGroup(["`Branch Management`"]) git/BranchManagementGroup -.-> git/branch("`Handle Branches`") git/BranchManagementGroup -.-> git/checkout("`Switch Branches`") git/BranchManagementGroup -.-> git/merge("`Merge Histories`") git/BranchManagementGroup -.-> git/log("`Show Commits`") git/BranchManagementGroup -.-> git/shortlog("`Condensed Logs`") subgraph Lab Skills git/branch -.-> lab-417747{{"`How to list all local branches merged into master in Git?`"}} git/checkout -.-> lab-417747{{"`How to list all local branches merged into master in Git?`"}} git/merge -.-> lab-417747{{"`How to list all local branches merged into master in Git?`"}} git/log -.-> lab-417747{{"`How to list all local branches merged into master in Git?`"}} git/shortlog -.-> lab-417747{{"`How to list all local branches merged into master in Git?`"}} end

Understanding Git Branches

Git is a distributed version control system that allows developers to manage and track changes in their codebase effectively. One of the fundamental concepts in Git is the branch, which represents a separate line of development within a repository.

What is a Git Branch?

A Git branch is a lightweight, movable pointer to a specific commit in the repository's history. Branches allow developers to work on different features or bug fixes simultaneously without interfering with the main codebase, known as the master or main branch.

Why Use Git Branches?

Using branches in Git provides several benefits:

  1. Parallel Development: Branches enable multiple developers to work on different features or bug fixes concurrently, without affecting the main codebase.
  2. Experimentation: Developers can create new branches to try out ideas or explore new approaches without risking the stability of the main branch.
  3. Collaboration: Branches facilitate collaboration by allowing developers to work on separate features and then merge their changes back into the main branch.
  4. Easier Debugging: If a bug is introduced in a specific branch, it can be easily isolated and fixed without affecting the rest of the codebase.

Creating and Switching Branches

To create a new branch in Git, you can use the git branch command followed by the name of the new branch:

git branch feature/new-functionality

To switch to the newly created branch, use the git checkout command:

git checkout feature/new-functionality

Alternatively, you can create and switch to a new branch in a single step using the git checkout -b command:

git checkout -b feature/new-functionality

Merging Branches

Once the work on a feature or bug fix is complete, the branch can be merged back into the main branch. This is done using the git merge command:

git checkout main
git merge feature/new-functionality

This will integrate the changes from the feature/new-functionality branch into the main branch.

Listing Local Branches Merged into Master

As you work on different features or bug fixes in your Git repository, you may accumulate multiple branches. Over time, some of these branches may be merged into the master or main branch. Keeping track of which local branches have been merged can be helpful for maintaining a clean and organized repository.

Using git branch --merged

Git provides the git branch --merged command to list all local branches that have been merged into the current branch. This is particularly useful when you want to identify branches that can be safely deleted, as they have already been incorporated into the main codebase.

To list all local branches that have been merged into the master branch, you can run the following command:

git branch --merged master

This will display a list of all local branches that have been merged into the master branch.

Understanding the Output

The output of the git branch --merged command will show you a list of branch names, with the current branch marked with an asterisk (*). For example:

  feature/new-functionality
* main
  bugfix/issue-123

In this example, the feature/new-functionality and bugfix/issue-123 branches have been merged into the main branch, which is the current branch.

Deleting Merged Branches

Once you have identified the local branches that have been merged into the master branch, you can safely delete them using the git branch -d command. This helps keep your repository clean and organized.

git branch -d feature/new-functionality
git branch -d bugfix/issue-123

If a branch has not been merged, Git will prevent you from deleting it, unless you use the -D flag, which will force the deletion.

Leveraging the Merged Branch List

The list of local branches that have been merged into the master branch can be leveraged in various ways to maintain a clean and organized Git repository.

Cleaning Up Merged Branches

One of the primary use cases for the merged branch list is to clean up your local repository by deleting the branches that have already been merged. This helps keep your repository tidy and reduces the clutter in your branch list.

git branch --merged master | grep -v "master" | xargs git branch -d

This command first lists all the local branches that have been merged into the master branch, then excludes the master branch itself, and finally deletes the remaining merged branches.

Identifying Unmerged Branches

In addition to listing the merged branches, you can also use the git branch --no-merged command to identify the branches that have not been merged into the master branch. This can be useful for determining which branches still need to be merged or addressed.

git branch --no-merged master

This command will display a list of all the local branches that have not been merged into the master branch.

Automating Branch Management

To further streamline your branch management process, you can create a script or alias that combines the git branch --merged and git branch -d commands. This can help you quickly identify and delete merged branches, making your repository maintenance more efficient.

For example, you can create a Git alias in your .gitconfig file:

[alias]
    clean-merged = "!git branch --merged master | grep -v \"master\" | xargs git branch -d"

Then, you can run git clean-merged to delete all local branches that have been merged into the master branch.

By leveraging the merged branch list, you can keep your Git repository organized, reduce clutter, and focus on the active development work.

Summary

By following the steps outlined in this tutorial, you will learn how to efficiently list all local Git branches that have been merged into the master branch. This knowledge will empower you to better manage your Git workflow, identify and remove unnecessary branches, and maintain a clean and organized codebase. Mastering this Git technique will streamline your development process and improve your overall productivity.

Other Git Tutorials you may like