Picture yourself working in an agile team: after three sprints, you’ve handled 25 tickets, so you’ve created 25 branches. Run git branch and you get an endless list. Always remember to clean up your branches, locally and on the server.
Starting point
git branch
# feature/1
# feature/2
# feature/3
# feature/4
# * master
The server has the same list.
Delete a merged branch
feature/4 has been approved and merged into master. We delete it locally:
git branch -d feature/4
# Deleted branch feature/4
Then on the server:
git push origin --delete feature/4
Refresh the repository page: the branch is gone.
Careful:
git push origin feature/4, without--delete, pushes the branch instead of deleting it. The older syntaxgit push origin :feature/4also deletes it.
Delete an unmerged branch
feature/3 was not merged, and we still want to delete it:
git branch -d feature/3
# error: the branch 'feature/3' is not fully merged.
Git refuses: it thinks you forgot to merge the branch, and protects you from losing work. If you’re sure, force the deletion with -D (capital D, for delete force):
git branch -D feature/3
# Deleted branch feature/3
git push origin --delete feature/3
A bit more tidying
When someone else deletes a branch on the server, your local repository keeps a trace of it (origin/feature/x). To remove it:
git fetch --prune
Recap
| Command | Role |
|---|---|
git branch -d <branch> | Delete a merged local branch |
git branch -D <branch> | Force-delete an unmerged local branch |
git push origin --delete <branch> | Delete a branch on the server |
git fetch --prune | Forget branches deleted from the server |
Exercise · The big cleanup
The feature/login branch is merged. The test/trial branch isn’t, but you don’t want it anymore. Both exist locally and on the server. Delete them everywhere.
Show the solution
git branch -d feature/login
git push origin --delete feature/login
git branch -D test/trial
git push origin --delete test/trial
-d is enough for the merged branch. For test/trial, Git would refuse -d: you need -D.
What’s Next
Last chapter: git diff, to check exactly what you changed before committing.
This is Chapter 10 of the Git in Practice course. Follow along to learn how to use Git every day and as a team.