Automatically clean up merged and closed Git branches
Delete local Git branches that have been merged on GitLab or linked to closed Jira tickets across all your repositories
Overview
Save time by automating branch cleanup across multiple repositories instead of manually checking each branch. Reduce repository clutter by removing branches tied to completed work. Maintain a cleaner local development environment with only active branches.
How it works
The workflow scans all Git repositories in a specified directory, then for each repository it identifies branches that have been merged on GitLab by fetching merge request data via MCP. It also extracts Jira ticket IDs from branch names and checks their status in Jira. Any branch that has been merged or has a Done or Won't Do ticket is automatically deleted, while protecting critical branches like main/staging and the current working branch.
Who this is for
Developers and engineering teams who work across multiple repositories and use GitLab for code review and Jira for issue tracking.
Suggested prompt
Help me clean up stale Git branches. Start by asking me for: (1) the directory where my Git repositories live, (2) which branches to protect, and (3) whether you should delete automatically or just report what is stale. Scan each repository and identify branches merged on GitLab or linked to Jira tickets marked Done or Won't Do. Show me branch names, repos, and reasons. Delete or report based on my preference.
Frequently asked questions
How does the branch cleanup workflow decide which branches to delete?
The workflow checks two things: whether a branch has a merged merge request on GitLab, and whether the Jira ticket extracted from the branch name is marked as Done or Won't Do. Branches matching either condition are flagged for deletion, while protected branches (like main, staging, develop) and your current working branch are always preserved.
Can I preview which branches will be deleted before they are removed?
Yes. When you run the prompt, you can choose between automatic cleanup or a report-only mode. In report mode, the AI will list every stale branch with the reason it is considered stale and wait for your approval before deleting anything.
Does this work across multiple Git repositories?
Yes. You specify a parent directory (like ~/Sites or ~/code) and the workflow scans every Git repository inside it. It processes each repo independently, checking GitLab merge requests and Jira tickets for every branch.
What GitLab and Jira permissions are needed?
You need GitLab access to read merge request data for your projects, and Jira access to read issue statuses. The template uses standard Zapier authentication for both services, so you connect your accounts during setup.