Usually when I merge my feature branch into master after a successful code review I run:
git branch -d <branch-name>
git push origin :<branch-name>
Note: don't forget the :
before the branch name when you push.
The first command deletes your branch locally and the second one deletes the remote one.
If this becomes a common practice in the team, you'll reach your goal.
A way to automatise it is to use a post-merge git hook, where you check if the current branch is master, you take the branch-name from the commit message and you run the two commands I wrote above.
Create a file in your project folder
touch .git/hooks/post-merge
and add execution permissions to it
chmod +x .git/hooks/post-merge
then open it with a text editor and add this content:
#!/bin/sh
#---------------------------------
# Delete branch merged into master
#---------------------------------
# Define your master branch name
master="master"
currentBranch=$(git branch | grep \* | cut -d ' ' -f2)
if [ $currentBranch = $master ]; then
branchToDelete=$(git rev-parse --abbrev-ref $(git show-ref | grep $(git rev-parse HEAD^2)| cut -d ' ' -f 2))
if [ $branchToDelete != "" ]; then
git branch -d $branchToDelete
git push origin :$branchToDelete
fi
fi
exit 0