2023-03-14

Cleaning Up Merged Branches in Git

The Need to Clean Merged Branches in Git

When you merge branches in Git, they don’t automatically disappear. They remain and could clutter your repo if not managed regularly.

This might seem harmless at first, but in a large project with numerous contributors, these residual branches can lead to confusion, making it harder for developers to identify which branches are active and which are obsolete. Therefore, a periodic clean-up is essential for maintaining a neat and efficient repository.

Viewing Merged Branches Locally

Identify which branches have already been merged.

bash
$ git branch --merged

Upon executing this command, Git lists down all branches in your local repository that have been merged.

Bulk Deleting Merged Branches

By combining a few commands, we can bulk delete the merged branches:

bash
$ git branch --merged | egrep -v '\*|develop|main' | xargs git branch -d

Let’s break down this command:

  1. git branch --merged: This fetches all the merged branches.
  2. egrep -v '\*|develop|main: This filters out the current branch (represented by the *) and any branches named develop and main.
  3. xargs git branch -d: This command takes the filtered list and deletes each branch.

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!