0
0
Gitdevops~10 mins

Deleting branches in Git - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Deleting branches
Start
Check branch exists?
NoError: branch not found
Yes
Check if branch is current branch?
YesError: cannot delete current branch
No
Delete branch command
Branch deleted
End
This flow shows the steps git follows when deleting a branch: it checks if the branch exists, ensures it's not the current branch, then deletes it.
Execution Sample
Git
git branch -d feature
# Deletes local branch 'feature' if merged

git branch -D feature
# Force deletes local branch 'feature'
This code deletes a local git branch named 'feature', first safely then forcefully.
Process Table
StepActionCondition/CheckResult/Output
1Check if branch 'feature' existsBranch existsYes, branch found
2Check if 'feature' is current branchIs current branch?No, safe to delete
3Run 'git branch -d feature'Is branch merged?Yes, branch deleted successfully
4Check if branch 'feature' exists againBranch exists?No, branch removed
5Try deleting non-existent branchBranch exists?No, error: branch not found
6Try deleting current branchIs current branch?Yes, error: cannot delete current branch
💡 Branch deleted successfully or error shown if conditions not met
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
branch_existsunknowntruetruefalsefalse
is_current_branchunknownunknownfalsefalsefalse
branch_deletedfalsefalsefalsetruetrue
Key Moments - 2 Insights
Why can't I delete the branch I'm currently on?
Git prevents deleting the current branch to avoid losing your working context. See execution_table step 6 where deletion fails if branch is current.
What happens if the branch is not merged when using 'git branch -d'?
'git branch -d' refuses to delete unmerged branches to protect work. You must use 'git branch -D' to force delete. This is implied in step 3 where deletion depends on merge status.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'branch_exists' after step 4?
Afalse
Btrue
Cunknown
Derror
💡 Hint
Check the 'branch_exists' row in variable_tracker after step 4
At which step does git refuse to delete the branch because it is the current branch?
AStep 3
BStep 4
CStep 6
DStep 1
💡 Hint
Look at execution_table step 6 for the current branch deletion error
If the branch is not merged, which command should you use to delete it?
Agit branch -d
Bgit branch -D
Cgit delete branch
Dgit remove branch
💡 Hint
Refer to execution_sample where '-D' forces deletion regardless of merge status
Concept Snapshot
Deleting branches in git:
- Use 'git branch -d branchname' to delete safely (only if merged)
- Use 'git branch -D branchname' to force delete
- Cannot delete current branch
- Check branch exists before deleting
- Errors shown if conditions not met
Full Transcript
This visual execution shows how git deletes branches. First, git checks if the branch exists. If not, it shows an error. Then it checks if the branch is the current one; if yes, deletion is blocked to protect your work. If the branch exists and is not current, git tries to delete it. The safe delete command '-d' only works if the branch is merged. Otherwise, you must use '-D' to force delete. The variable tracker shows branch existence and deletion status step by step. Key moments include understanding why current branches can't be deleted and the difference between safe and force delete commands.