Sub topic 9
Deleting and renaming branches
This chapter assumes you can create, switch, and merge branches, and that you know a branch is a pointer to a commit. Deleting a branch removes only that pointer. The commits stay in the repository until Git's garbage collection removes unreachable ones, which happens weeks later by default.
Deleting a merged branch
After a branch is merged, its pointer has done its job. Delete it with -d:
git switch main
git branch -d feature/login-d is the safe form. Git refuses to delete the branch if its commits are not reachable from the branch it is compared against. That is the branch's upstream if one is set, and HEAD otherwise. The refusal protects you from losing the only reference to unmerged work.
You cannot delete the branch you are on. Switch to another branch first.
Force-deleting an unmerged branch
If you want to discard unmerged work, use -D, which is shorthand for --delete --force:
git branch -D abandoned-experimentGit prints the old tip's abbreviated SHA when it deletes the branch. Keep it if you might want the branch back.
Renaming a branch
Use -m (move). With two names, it renames any local branch. With one name, it renames the branch you are on:
git branch -m old-name new-name
git branch -m new-nameRenaming changes only the local pointer's name and its reflog. It does not touch the remote.
Cleaning up on the remote
Deleting a local branch does not delete its copy on the remote. Remove the remote branch separately:
git push origin --delete feature/loginOther people's deletions do not disappear from your repository automatically either. Your origin/feature/login remote-tracking branch stays until you prune it:
git fetch --prune
git branch -vvIn the git branch -vv output, a local branch whose upstream was deleted is marked gone. That mark is a good sign the local branch can be deleted.
Renaming a branch that is already pushed
A rename is a local change, so you publish it as a new branch and remove the old one:
git branch -m old-name new-name
git push -u origin new-name
git push origin --delete old-nameTell collaborators before you do this. Anyone who still has old-name checked out will have an upstream that no longer exists, and they will need to rename their local branch and set a new upstream.
Quick reference
| Goal | Command |
|---|---|
| Delete a merged branch | `git branch -d name` |
| Delete regardless of merge state | `git branch -D name` |
| Rename a branch | `git branch -m old new` |
| Delete a remote branch | `git push origin --delete name` |
| Remove stale remote-tracking branches | `git fetch --prune` |
Common errors
error: The branch 'x' is not fully merged: the branch has commits that are not in the comparison branch. Merge it, or use-Dif you mean to discard it.Cannot delete branch 'x' checked out at ...: the branch is checked out in this or another worktree. Switch away first.error: Cannot delete branch ... used by worktree: same cause, in a linked worktree.
Exercise
You run git branch -d feature/search and Git says the branch is not fully merged. You have confirmed the work was abandoned. What command deletes it, and how can you make it recoverable later?
Show answer
Run git branch -D feature/search. Note the abbreviated SHA that Git prints as it deletes the branch. You can later run git branch recovered-work <sha> to recreate the branch, or find the commit through git reflog.