Keeping the Branch List Useful
Branches are cheap to create, so they pile up: finished features, quick fixes, abandoned experiments. A long list makes it hard to see what is still in progress. This lesson shows how to rename branches and how to delete the ones you no longer need without losing work.
Renaming
git branch -m <old-name> <new-name> # rename any branch
git branch -m <new-name> # rename the branch you are on
Renaming changes only the name. The branch still points at the same commit, and its history is unchanged. Git refuses if the new name is already taken.
Which Branches Are Merged?
A branch is merged into your current branch when its tip is already part of the current branch's history, so every commit on it is safe there.
git branch --merged # branches whose work is in the current branch
git branch --no-merged # branches with commits the current branch lacks
Run these from the branch that receives finished work, usually main. The
current branch always appears in the --merged list, because it contains its
own history.
Deleting Safely
git branch -d <branch> # delete only if its work is merged
git branch -D <branch> # delete even if its work is not merged
Deleting a branch removes the name, not the commits it pointed at. For a merged
branch that loses nothing: the commits are still reachable from main. For an
unmerged branch, the name is the only thing that leads to its commits, so
deleting it can make that work hard to find.
That is why -d refuses an unmerged branch with an error saying it is not fully merged. Treat that refusal as a question: is this work really
abandoned? Check with git log <branch> first, and use -D only when the
answer is yes.
Two more safety rules:
- Git will not delete the branch you are on. Switch to another branch first.
- When Git deletes a branch it prints
Deleted branch <name> (was <ID>).If you deleted the wrong one,git branch <name> <ID>recreates it at the same commit. The recovery module shows how to find the ID later if you missed it.
Exercise: Tidy the Branch List
The shop repository has a merged fix, a feature with a placeholder name, and an experiment nobody has decided about yet. Tidy the list without losing unmerged work.
Starting state: Git configured as in the configuration lesson.
-
Run the setup:
mkdir -p ~/git-practice/branch-cleanup cd ~/git-practice/branch-cleanup git init -b main cat > shop.md <<'EOF' # Shop EOF git add shop.md git commit -m "Start shop page" git switch -c hours-fix cat > shop.md <<'EOF' # Shop Open every day from 9:00 to 18:00. EOF git commit -am "Add opening hours" git switch main git merge hours-fix git switch -c feature1 cat > theme.txt <<'EOF' background = dark EOF git add theme.txt git commit -m "Start dark mode" git switch main git switch -c banner cat > banner.txt <<'EOF' Ten percent off this week. EOF git add banner.txt git commit -m "Try a discount banner" git switch main -
See which branches are merged into
main:git branch git branch --merged git branch --no-merged--mergedlistshours-fixandmain.--no-mergedlistsbannerandfeature1: each has a commitmaindoes not. -
Give
feature1a name that says what it is:git branch -m feature1 dark-mode git branchThe list now shows
dark-mode, pointing at the same "Start dark mode" commit. -
Delete the merged branch:
git branch -d hours-fixGit prints
Deleted branch hours-fix (was ...)with the commit's short ID. Its work is safe: the opening hours are inmain'sshop.md. -
Try the same with
banner, which is not merged:git branch -d banner git branchGit refuses:
the branch 'banner' is not fully merged, and a hint names-D. Nobody has decided to abandon the banner, so leave it. The list showsbanner,dark-modeandmain.
Success: the branch list is banner, dark-mode and main; the merged
hours-fix is gone; and no unmerged commit was deleted.
If something differs:
| What you see | Likely cause | Next step |
|---|---|---|
is not fully merged | The branch has commits the current branch lacks | Check git log <branch>; keep it, merge it, or use -D only if the work is abandoned |
| An error that Git cannot delete the branch, naming your folder | It is the branch you are on | Switch to another branch first |
fatal: a branch named '...' already exists | The new name is taken | Choose another name, or delete the old branch first if it is merged |
error: branch '...' not found | A misspelled name | Check the list with git branch |
| You deleted the wrong branch | A typo or a rushed -D | git branch <name> <ID>, using the ID from the Deleted branch message |
-
Clean up:
cd ~/git-practice ls rm -rf branch-cleanup ls
Independent task: before cleaning up, create a branch old-idea from
main with one commit:
git switch -c old-idea
cat > idea.txt <<'EOF'
Sell gift cards.
EOF
git add idea.txt
git commit -m "Try gift cards"
git switch main
The team decides to abandon it. Delete it, confirm it is gone, then bring it back using only the output of the delete command, and show that the restored branch still has the "Try gift cards" commit.
Check Your Understanding
Would you use git branch -d or git branch -D, and why?
hotfixwas merged intomainlast week, and you are onmain.spikeholds an experiment the team agreed to abandon. It was never merged.- You are on
featureand want to deletefeature.
Answer and Reasoning
Independent task
git branch -D old-idea
git branch
git branch old-idea <ID>
git log -1 old-idea
-d would refuse, because the commit is not merged into main, so abandoning
it needs -D. Git prints Deleted branch old-idea (was <ID>)., and the list
no longer shows old-idea. git branch old-idea <ID>, with the ID from that
message, recreates the branch at the same commit, and git log -1 old-idea
shows "Try gift cards". The commit was never destroyed; only its name was
removed.
Understanding check
-d. The branch is merged, so-dsucceeds and nothing is lost. If it ever refused, that would be a sign the branch was not merged after all.-D, after checkinggit log spiketo confirm the work really is abandoned.-dwould refuse because the branch is unmerged.- Neither yet: Git will not delete the branch you are on. Switch to another
branch, such as
main, then use-diffeatureis merged.