Branch management is a routine, yet critical part of any Git workflow. If you find yourself repeatedly deleting branches after a feature is merged, a one‑line alias can save time and reduce clipboard clutter. You can delete both local and remote branches with a single command—no more juggling git branch -d and git push origin --delete.
Understanding Branch Cleanup with Git
In Git, removing a branch locally is harmless to the repository history but removing it remotely can affect teammates. Traditional deletion requires two separate commands and a mental switch between local and remote scopes. By condensing this into one alias, developers can perform cleanup in half the effort and with fewer mistakes.
Building the One‑Line Alias
Below is the full alias you can paste into your ~/.gitconfig or ~/.bashrc (depending on your shell) to delete a branch locally and on remote origin in one go:
git config --global alias.clean-branch '!f() {n local branch=$1n if [ -z "$branch" ]; thenn echo "Usage: git clean-branch <branch>"n exit 1n fin git branch -d "$branch" && git push origin --delete "$branch"n}; f'
- Alias name:
clean-branch– a descriptive shortcut that hints at both local and remote deletion. - Execution logic: The alias wraps a shell function (
f()) that accepts a branch name, checks for missing input, performs a safe local delete (-d), and then deletes the remote branch. - Failure handling: If the local delete fails (e.g., branch not fully merged), the remote deletion is not attempted, preventing accidental remote removal.
To use the alias, simply run:
git clean-branch feature‑xyz
This command will first try to delete the local branch feature‑xyz and, only if it succeeds, remove origin/feature‑xyz as well.
Safety & Best Practices
Because the alias performs destructive actions, it’s wise to incorporate checks and safeguards.
Confirm Branch Status Before Deleting
- Run
git statusto ensure you’re on a different branch. - Use
git branch --mergedto confirm the target branch is fully merged. - Proceed with the alias only when the branch is verified.
Use Forced Deletion Carefully
If you need to delete an unmerged branch, modify the alias to use -D instead of -d. However, keep a backup or push to a temporary branch before forcing removal.
Avoid Remote Deletion on Shared Repos
Only delete remote branches that are approved for removal. Collaborators may still rely on them if a feature hasn’t been fully integrated.
Advanced Variations & Automation
Beyond the basic alias, you can extend functionality to support multiple remotes, force deletions, or integration with git fetch --prune.
Multi‑Remote Support
git config --global alias.clean-branch '!f() {n local branch=$1 remote=${2:-origin}n git branch -d "$branch" && git push $remote --delete "$branch"n}; f'
Now you can specify a different remote: git clean-branch feature‑xyz upstream.
Force Deletion Option
git config --global alias.clean-branch '!f() {n local branch=$1 force=${2:-false}n local flag="-d"n [ "$force" = true ] && flag="-D"n git branch $flag "$branch" && git push origin --delete "$branch"n}; f'
Running git clean-branch feature‑xyz true will force delete even unmerged branches.
Automatic Prune After Cleanup
Add git fetch --prune at the end of the alias to ensure stale remote-tracking references are removed.
Why a One‑Line Alias Outperforms Manual Commands
Traditional branch cleanup requires:
git branch -d branch_namegit push origin --delete branch_name
With the alias:
- Single keystroke set‑up.
- Built‑in error handling.
- Consistent behavior across team repositories.
- Reduced likelihood of forgetting to delete the remote branch.
Conclusion
By adding a one‑line Git alias for combined local and remote branch deletion, developers can reclaim valuable time, prevent clutter, and maintain a cleaner repository history. Experiment with the variations above to match your workflow—whether you need multi‑remote support or force deletions—and keep the repo lean, one branch at a time.