In this tutorial you will learn how to delete a local branch in Git using the “git branch -d” and “git branch -D” commands. This shows you what Git is doing internally when you delete a local branch.
Code used during this tutorial:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# Git Delete local branch # Get some info about the current project git status git branch git branch branch_delete git branch ls -l .git/refs/heads/ # -d Delete a branch. The branch must be fully merged in its upstream branch, # or in HEAD if no upstream was set with --track or --set-upstream git branch -d branch_delete git branch ls -l .git/refs/heads/ # Error deleting the current branch git checkout -b branch_delete git status git branch # error: Cannot delete the branch 'branch_delete' which you are currently on. git branch -d branch_delete git checkout master git branch -d branch_delete git branch # Error deleting modified branch git branch branch_delete git checkout branch_delete git status echo "Hello world" > file-4.txt git status git add file-4.txt git commit -m "file-4.txt with Hello world content" git status git checkout master # error: The branch 'branch_delete' is not fully merged. # If you are sure you want to delete it, run 'git branch -D branch_delete'. git branch -d branch_delete # -D Delete a branch irrespective of its merged status git branch -D branch_delete # Man page man git-branch |