Sub topic 9
Deleting and renaming branches
This chapter assumes you can create and switch branches, you know that a branch is a pointer to a commit, and you have merged at least one branch. You need Git 2.23 or later.
Deleting a branch removes a pointer. It does not remove commits directly. That fact explains every safety check Git performs, and it explains how you recover from a mistake.
What deleting a branch does
A branch is a small file (or entry) that holds a commit ID. When you delete a branch, Git removes that pointer. The commits stay in the repository's object database. If another branch, tag, or commit still reaches them, nothing is lost.
Commits that no branch, tag, or other commit can reach are called unreachable. Git keeps unreachable commits for a while and removes them later during garbage collection. By default, reflog entries keep them alive for at least 30 days, and often longer for commits that were reachable from your branches. The last chapter of this tutorial covers recovery in detail.
Safe deletion with -d
Use git branch -d for the normal case, when work on the branch is finished and merged.
git switch main
git branch -d feature/loginGit refuses the deletion unless it considers the branch fully merged. The check compares the branch against its upstream branch if one is set. If there is no upstream, it compares against HEAD. This has two consequences:
- If you run
-dwhile onmain, the branch must be merged intomain. - If the branch has an upstream and is merged into that upstream but not into your current branch, Git deletes it and prints a warning.
When the branch is not merged, Git stops with a message like this:
error: the branch 'abandoned-experiment' is not fully merged.
If you are sure you want to delete it, run 'git branch -D abandoned-experiment'.Treat this message as a prompt to check, not as an obstacle to bypass.
Forced deletion with -D
git branch -D is shorthand for --delete --force. It skips the merge check.
git branch -D abandoned-experimentBefore you force-delete, note the tip commit so you can find it again. Git prints it when it deletes the branch:
Deleted branch abandoned-experiment (was 3f2a9c1).Keep that abbreviated ID. As long as the commit has not been garbage collected, git branch recovered 3f2a9c1 brings the branch back.
To see what you would lose first, list commits on the branch that are not on main:
git log --oneline main..abandoned-experimentAn empty result means the branch holds nothing that main lacks.
Deletions Git refuses regardless of merge state
Git will not delete the branch you are currently on, even with -D.
git branch -d main
# error: cannot delete branch 'main' used by worktree at '/path/to/repo'The wording varies by version, but the rule is constant. Switch to another branch first. The same applies to a branch checked out in another worktree (a linked working directory created with git worktree add).
Squash and rebase merges confuse -d
The -d check asks whether the branch's tip is reachable from the target. Some merge methods rewrite the commits, so the original tip never becomes reachable from main.
- A squash merge combines the branch into one new commit.
- A rebase-and-merge on a hosting service replays the commits as new ones with new IDs.
In both cases the content is on main, but git branch -d reports the branch as not merged. If you know the work landed through the pull request, -D is the correct tool. Confirm first by checking the pull request state or by searching main for the change.
git log --oneline --grep="login" mainDeleting remote branches
Deleting a local branch does not touch the copy on the remote. Remove that separately:
git push origin --delete feature/loginThis deletes the branch on origin. Your local remote-tracking branch origin/feature/login is removed as part of the push. Other people's clones still show a stale origin/feature/login until they prune.
git fetch --prune
git remote prune origingit fetch --prune removes remote-tracking branches whose remote branch no longer exists. To make this automatic, set the configuration once:
git config --global fetch.prune truePruning removes only remote-tracking references. It never deletes your local branches. A local branch that tracked a deleted remote branch is marked gone:
git branch -vv
# feature/login 3f2a9c1 [origin/feature/login: gone] Add login formA gone label is a good signal that the branch can be cleaned up, after you confirm it was merged.
Cleaning up many merged branches
List branches already merged into main:
git branch --merged mainDelete them in bulk, skipping main and develop:
git branch --merged main \
| grep -v -E '^\*|^\s*(main|develop)$' \
| xargs -r git branch -dThe -d flag keeps this safe, since Git re-checks each branch. The inverse listing, git branch --no-merged main, shows branches that still hold unmerged work. Review that list before you delete anything on it.
Renaming branches
git branch -m (short for --move) renames a branch. It moves the pointer and also moves the branch's reflog.
git branch -m old-name new-nameWith one argument, Git renames the branch you are currently on:
git branch -m better-nameRenaming refuses to overwrite an existing branch. Use -M (--move --force) only when you intend to replace the target. That replacement discards the old target pointer, so note its commit ID first.
| Command | Effect |
|---|---|
| `git branch -d name` | Delete if merged |
| `git branch -D name` | Delete regardless of merge state |
| `git branch -m old new` | Rename, fail if `new` exists |
| `git branch -M old new` | Rename, overwrite `new` if it exists |
Renaming a branch that is shared
A rename is local. The remote still has the old name, and the renamed local branch may still track the old remote branch. To rename a branch that is already pushed, publish the new name, then remove the old one:
git branch -m old-name new-name
git push -u origin new-name
git push origin --delete old-nameThe -u flag sets the upstream of new-name to origin/new-name, so later git push and git pull use the new remote branch.
Renaming the default branch, for example from master to main, also requires changing the default branch setting on the hosting service. Do that before you delete the old remote branch, because most services refuse to delete the default branch.
Common mistakes
- Using
-Das a habit. You lose the safety check that-dgives you. - Deleting the local branch and assuming the remote branch is gone. It is not.
- Forgetting that a squash merge makes
-dfail, then concluding the work was not merged. - Renaming locally and forgetting to update the upstream, so
git pushtargets the old name or fails. - Deleting a branch while it is the only reference to unique commits, then losing the commit ID. The reflog can still help, but only for a limited time.
Exercises
Exercise
You are on main and run git branch -d experiment. Git replies that the branch is not fully merged, but you know its pull request was squash-merged yesterday. Why did the check fail, and what do you do?
Show answer
A squash merge creates one new commit on main, so the original tip commit of experiment is not reachable from main and -d sees unmerged work. Confirm the change is on main (for example with git log --oneline --grep=... or by checking the pull request), then delete with git branch -D experiment. Note the commit ID Git prints in case you need to recover it.
Exercise
You rename a pushed branch with git branch -m old-name new-name. Which two commands make the rename visible on the remote and remove the old branch there?
Show answer
git push -u origin new-name publishes the new name and sets its upstream. git push origin --delete old-name removes the old branch from the remote.