Git Foundations / Branch and Merge

Rename and delete branches

Lesson 17 of 33. Open to read. No account needed.

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.

  1. 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
    
  2. See which branches are merged into main:

    git branch
    git branch --merged
    git branch --no-merged
    

    --merged lists hours-fix and main. --no-merged lists banner and feature1: each has a commit main does not.

  3. Give feature1 a name that says what it is:

    git branch -m feature1 dark-mode
    git branch
    

    The list now shows dark-mode, pointing at the same "Start dark mode" commit.

  4. Delete the merged branch:

    git branch -d hours-fix
    

    Git prints Deleted branch hours-fix (was ...) with the commit's short ID. Its work is safe: the opening hours are in main's shop.md.

  5. Try the same with banner, which is not merged:

    git branch -d banner
    git branch
    

    Git 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 shows banner, dark-mode and main.

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 seeLikely causeNext step
is not fully mergedThe branch has commits the current branch lacksCheck 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 folderIt is the branch you are onSwitch to another branch first
fatal: a branch named '...' already existsThe new name is takenChoose another name, or delete the old branch first if it is merged
error: branch '...' not foundA misspelled nameCheck the list with git branch
You deleted the wrong branchA typo or a rushed -Dgit branch <name> <ID>, using the ID from the Deleted branch message
  1. 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?

  1. hotfix was merged into main last week, and you are on main.
  2. spike holds an experiment the team agreed to abandon. It was never merged.
  3. You are on feature and want to delete feature.

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

  1. -d. The branch is merged, so -d succeeds and nothing is lost. If it ever refused, that would be a sign the branch was not merged after all.
  2. -D, after checking git log spike to confirm the work really is abandoned. -d would refuse because the branch is unmerged.
  3. Neither yet: Git will not delete the branch you are on. Switch to another branch, such as main, then use -d if feature is merged.

References

Study this course with an account

Course access adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 99 plus 18% GST. 90 days of online access. No auto-renewal.

See course access

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy