CodeBlanks Logo Icon CodeBlanks

Deleting a branch

Learn to delete a branch with git branch -d.

Delete a Branch You No Longer Need

When a feature is complete or an experiment is finished, you can delete its branch. The git branch -d command safely deletes a branch whose changes have been merged.

git branch -d old-experiment

Git may warn you during a normal deletion if the branch contains changes that have not been merged.

Check Before Deleting

Make sure you are not currently working on the branch you want to delete. Git does not let you delete the active branch; you must switch to another branch first.

git switch main
git branch -d temporary-report

If you try to delete a branch that has not been merged, the -d operation may be rejected. This warning helps prevent accidental loss of unfinished work.

Practice Safely Delete an Old Branch

Delete the merged branch named completed-feature. Then print a message confirming that the deletion is complete.

Starter code

terminal
print("Dal temizliği başlıyor")

Sample solution

terminal
git branch -d tamamlanan-ozellik
print("Eski dal silindi")

Expected output

Dal temizliği başlıyor
Eski dal silindi

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which command safely deletes a merged branch?

  • git branch -d old-experiment Correct answer
  • git branch -r old-experiment
  • git switch -d old-experiment
  • git merge -d old-experiment

Explanation: Correct. The -d option is used to safely delete a merged branch.

2 Use the Branch Deletion Option

Question
git branch ____ arsiv
Answer
git branch -d arsiv

Explanation: The -d option is used to safely delete the merged branch named arsiv.

3 Branch Deletion Concepts

Term Meaning
git branch -d Deletes a merged branch
Etkin dal The branch currently in use
Birleştirilmemiş dal A branch not yet integrated
Silme uyarısı Helps prevent work loss

All lessons in Intermediate

20 Intermediate final

  1. Branch management
  2. Rebase
  3. Conflict
  4. PR flow