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:
git branch --merged
: This fetches all the merged branches.egrep -v '\*|develop|main
: This filters out the current branch (represented by the*
) and any branches nameddevelop
andmain
.xargs git branch -d
: This command takes the filtered list and deletes each branch.
AlloyDB
Amazon Cognito
Amazon EC2
Amazon ECS
Amazon QuickSight
Amazon RDS
Amazon Redshift
Amazon S3
API
Autonomous Vehicle
AWS
AWS API Gateway
AWS Chalice
AWS Control Tower
AWS IAM
AWS Lambda
AWS VPC
BERT
BigQuery
Causal Inference
ChatGPT
Chrome Extension
CircleCI
Classification
Cloud Functions
Cloud IAM
Cloud Run
Cloud Storage
Clustering
CSS
Data Engineering
Data Modeling
Database
dbt
Decision Tree
Deep Learning
Descriptive Statistics
Differential Equation
Dimensionality Reduction
Discrete Choice Model
Docker
Economics
FastAPI
Firebase
GIS
git
GitHub
GitHub Actions
Google
Google Cloud
Google Search Console
Hugging Face
Hypothesis Testing
Inferential Statistics
Interval Estimation
JavaScript
Jinja
Kedro
Kubernetes
LightGBM
Linux
LLM
Mac
Machine Learning
Macroeconomics
Marketing
Mathematical Model
Meltano
MLflow
MLOps
MySQL
NextJS
NLP
Nodejs
NoSQL
ONNX
OpenAI
Optimization Problem
Optuna
Pandas
Pinecone
PostGIS
PostgreSQL
Probability Distribution
Product
Project
Psychology
Python
PyTorch
QGIS
R
ReactJS
Regression
Rideshare
SEO
Singer
sklearn
Slack
Snowflake
Software Development
SQL
Statistical Model
Statistics
Streamlit
Tabular
Tailwind CSS
TensorFlow
Terraform
Transportation
TypeScript
Urban Planning
Vector Database
Vertex AI
VSCode
XGBoost