Set a recurring 15-minute weekly appointment and pair it with an existing routine, such as after your Sunday coffee or before your first Monday meeting. This method works when you want a simple, low-effort system that prevents branches from piling up, especially if you work on a team or manage multiple repositories.
Before You Start: What You Need in Place
- Access to your repository (local and remote, if you use one).
- A list of your active branches (run
git branchlocally, or check your Git hosting service). - Knowledge of which branches are merged or obsolete. You can check with
git branch --merged. - A backup plan: if you are unsure about a branch, push it to a remote archive or tag it before deleting.
Why Branch Cleanup Habits Fail
Most cleanup routines collapse because they are too big, too vague, or not tied to a trigger. You might plan to “clean up branches” but never define when or how many. Without a specific trigger, you forget. Without a time limit, you postpone. The fix is to make the habit tiny, scheduled, and attached to something you already do.
Set a 15-Minute Weekly Cleanup
Choose a day and time that is stable. For example, every Friday at 4:00 PM, or every Monday at 9:15 AM. Put it in your calendar as a recurring event with a reminder. The goal is not to clean everything—just to spend 15 minutes on it. If you finish early, stop. If you run out of time, leave the rest for next week.
How to Identify Branches to Delete
Run git branch --merged to see branches that have been merged into your current branch. Those are safe to delete unless you need them for reference. Also check branches that have not been updated in months: use git log -1 --format='%ci' branchname to see the last commit date. If a branch is older than 90 days and not merged, ask yourself if it is still needed. If you are unsure, push it to a remote archive branch (e.g., archive/feature-name) before deleting.
Step-by-Step: Your Weekly Cleanup Routine
- Open your terminal and navigate to your repository. Run
git fetch --pruneto remove remote tracking branches that no longer exist on the remote. - Run
git branchto list all local branches. Count them. If you have fewer than 5, you are done—stop. - Run
git branch --mergedto see which branches have been merged into your current branch. For each one, verify it is truly merged by checking the last commit date withgit log -1 --format='%ci' branchname. - Delete each merged branch with
git branch -d branchname. The-dflag only deletes if the branch has been merged, so it is safe. - For branches that are not merged but clearly obsolete (e.g., a feature that was abandoned), delete with
git branch -D branchnameonly if you are certain. If there is any doubt, tag the branch first:git tag archive/branchname branchname, then delete. - If you use a remote, delete the remote branches that are no longer needed:
git push origin --delete branchname. Do this only for branches you have confirmed are safe. - After cleanup, run
git branch -ato see the final list. Note how many you removed and how many remain.
Worked Example: Cleaning Up a Real Repository
Suppose you run git branch and see 12 branches: main, feature/login, feature/payment, bugfix/header, experiment/dark-mode, old-refactor, temp-fix, wip-dashboard, feature/export, docs/readme, test/coverage, and release/v1.2.
You run git branch --merged and see: bugfix/header, docs/readme, test/coverage, and release/v1.2. You delete those four with git branch -d. That takes about 3 minutes.
Next, you check the remaining branches. feature/login has a commit from 2 weeks ago, so you keep it. feature/payment has a commit from 3 months ago, but you are still working on it, so you keep it. experiment/dark-mode has not been touched in 6 months, and you decide to tag it as archive/dark-mode and then delete it. old-refactor and temp-fix are clearly obsolete—you delete them with -D after confirming they are not referenced anywhere. wip-dashboard has a commit from yesterday, so you keep it. feature/export is merged, but you forgot to include it in the first list because you were on a different branch—so you delete it now.
In total, you removed 7 branches in about 10 minutes. Your final list is: main, feature/login, feature/payment, wip-dashboard, and feature/export (if you kept it). You feel accomplished and stop.
What Goes Wrong: Three Common Mistakes
Deleting a Branch That Is Not Fully Merged
If you use git branch -d on an unmerged branch, Git will refuse and show an error. That is good. But if you use -D without checking, you lose commits. This causes lost work and panic. Always use -d first, and only use -D when you are absolutely sure the branch is obsolete.
Forgetting to Prune Remote Tracking Branches
If you delete a branch on the remote but not locally, your local list still shows it. Over time, you accumulate ghost branches. Running git fetch --prune once a week keeps your local list in sync. Without it, you might think you have more branches than you do, and you waste time checking branches that no longer exist.
Making Cleanup a Monthly Marathon
If you wait a month, you might have 30 branches to review. That feels overwhelming, so you skip it. A 15-minute weekly session is easier to start and keeps the list small. If you miss a week, do not try to catch up with a 2-hour session—just resume the next week.
How to Keep the Habit Going
After your cleanup, take 30 seconds to write down how many branches you removed and how it felt. This small note reinforces the benefit. Also, set a rule: every time you merge a pull request, delete the branch immediately. That reduces the weekly workload. If you work with others, agree on a naming convention like feature/description so you can quickly identify purpose. Finally, if you miss a week, do not judge yourself. Just do it the next week.
What If You Are Unsure About a Branch?
When in doubt, do not delete. Tag it first: git tag archive/branchname branchname. Then delete the branch. The tag preserves the commit history. You can always delete the tag later. This gives you safety without leaving the branch in your list.
Sticking to branch cleanup is not about discipline—it is about making the task small and regular. A 15-minute weekly appointment, paired with an existing routine, turns cleanup into a habit that takes little effort and keeps your repository tidy.
Related
- Fall Garden Cleanup for Beginners: What to Do
- Garden Cleanup vs Pruning: Which to Do
- How to Start Cleaning Habits That Stick Without Overwhelm
General information for everyday wellbeing, not medical advice. If something affects your health, sleep or mood for more than a short while, talk to a qualified professional. Read more
Spotted an error? Tell us and we will correct it (corrections policy).