If your repository looks like a botanical garden of abandoned feature branches you are not alone. This guide walks through how to remove local and remote branches in GitLab while keeping commit history intact and not triggering a small apocalypse for your teammates.
Quick safety checks before you delete
Deleting a branch is permanent from the reference point of the branch name. The commits may still be reachable but do your due diligence first. Run these commands to see what you are dealing with and to avoid drama.
- List local branches with a peek at the current head using
git branch
- See all refs including remotes with
git branch -a
- Double check that work has been merged or backed up before removing the branch name
Delete local branches the sensible way
When the feature is merged there is a polite local delete that protects you from accidental data loss.
git checkout main
git branch -d my-branch
If you are absolutely sure and also a little reckless you can force delete.
git branch -D my-branch
Use the forced delete only when you have confirmed the commits are stored elsewhere or you enjoy explaining mistakes to your team.
Remove branches from the remote GitLab repo
Remote deletion is simple and satisfying. This command tells the remote to drop the branch name.
git push origin --delete my-branch
If you prefer GUI two clicks over typed commands the GitLab web UI offers a Delete button on the Repository Branches page. For bulk cleanup use the branch list filter to show merged branches and delete entries from there while reviewing first.
Keep your local clone tidy after remote deletes
Deleting a remote branch does not automatically remove the stale remote ref in your local clone. Clean that up with these commands.
git fetch --prune
git remote prune origin
Protected branches and permissions
Protected branches are guarded for a reason. You will need the appropriate role to delete them. If you have permissions you can unprotect a branch in Project Settings under Repository Protected Branches and then delete it. Otherwise ask a maintainer to help. This keeps important branches safe from accidental deletions and from well intentioned chaos.
Merge requests and source branch cleanup
Save future housekeeping by enabling remove source branch on merge requests. This setting prevents leftover branches from spawning like bad ideas. It is a small choice that spares future developers from a graveyard of stale names.
Automation and good repository maintenance
- Enable remove source branch on merge to automate obvious cleanup
- Use branch protection rules wisely to prevent accidental removal of mainline branches
- Consider periodic cleanup for stale branches if your project is active and you have the rights
- Use clear branch naming so reviewers and CI can ignore or archive old work easily
That is all it takes to keep a GitLab repo from turning into a branch graveyard. Use the git commands for fast precise removal and the web UI for careful review. Clean repo maintenance is a small gift to the next developer who inherits your work and a great way to avoid sad Slack messages at 3 AM.