Bird
Raised Fist0
Gitdevops~10 mins

Deleting branches in Git - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the command git branch -d feature do?
easy
A. Deletes the remote branch named 'feature'.
B. Deletes the local branch named 'feature' only if it is fully merged.
C. Force deletes the local branch named 'feature' regardless of merge status.
D. Creates a new branch named 'feature'.

Solution

  1. Step 1: Understand the command syntax

    The command git branch -d is used to delete a local branch safely.
  2. Step 2: Check the merge condition

    The -d option deletes the branch only if it has been fully merged to avoid losing work.
  3. Final Answer:

    Deletes the local branch named 'feature' only if it is fully merged. -> Option B
  4. Quick Check:

    git branch -d safely deletes merged branches [OK]
Hint: Use -d to delete merged branches safely [OK]
Common Mistakes:
  • Confusing -d with -D which force deletes
  • Thinking it deletes remote branches
  • Assuming it deletes unmerged branches
2. Which command correctly force deletes a local branch named bugfix?
easy
A. git branch -D bugfix
B. git branch -d bugfix
C. git push origin --delete bugfix
D. git delete branch bugfix

Solution

  1. Step 1: Identify force delete option

    The -D option in git branch force deletes a local branch even if unmerged.
  2. Step 2: Confirm command correctness

    git branch -D bugfix is the correct syntax to force delete the local branch named 'bugfix'.
  3. Final Answer:

    git branch -D bugfix -> Option A
  4. Quick Check:

    Force delete local branch = git branch -D [OK]
Hint: Use -D to force delete local branches [OK]
Common Mistakes:
  • Using -d which only deletes merged branches
  • Trying to delete remote branch with local command
  • Typing invalid commands like git delete branch
3. What is the output of the command git push origin --delete release if the remote branch release exists?
medium
A. Deletes the remote branch 'release' and shows confirmation.
B. Deletes the local branch 'release' only.
C. Shows an error because the syntax is incorrect.
D. Creates a new remote branch named 'release'.

Solution

  1. Step 1: Understand the command purpose

    git push origin --delete release is used to remove a branch from the remote repository named 'origin'.
  2. Step 2: Confirm the effect on remote branch

    If the remote branch 'release' exists, this command deletes it and shows a confirmation message.
  3. Final Answer:

    Deletes the remote branch 'release' and shows confirmation. -> Option A
  4. Quick Check:

    Remote branch deletion uses git push origin --delete [OK]
Hint: Use git push origin --delete to remove remote branches [OK]
Common Mistakes:
  • Thinking it deletes local branches
  • Using wrong syntax causing errors
  • Assuming it creates branches
4. You run git branch -d feature but get an error saying the branch is not fully merged. What should you do to delete it anyway?
medium
A. Rename the branch before deleting.
B. Use git push origin --delete feature to delete locally.
C. Run git branch -d feature again.
D. Use git branch -D feature to force delete the branch.

Solution

  1. Step 1: Understand the error cause

    The error means the branch 'feature' has changes not merged to other branches.
  2. Step 2: Use force delete option

    To delete it anyway, use git branch -D feature which forces deletion regardless of merge status.
  3. Final Answer:

    Use git branch -D feature to force delete the branch. -> Option D
  4. Quick Check:

    Force delete unmerged branch = git branch -D [OK]
Hint: Use -D to force delete unmerged branches [OK]
Common Mistakes:
  • Trying to delete remote branch with local command
  • Repeating the same command without force
  • Renaming branch does not delete it
5. You want to delete a branch named hotfix both locally and remotely. Which sequence of commands is correct?
hard
A. git branch -d hotfix && git push origin --delete hotfix
B. git push origin --delete hotfix && git branch -d hotfix
C. git branch -D hotfix && git push origin --delete hotfix
D. git branch -d hotfix && git branch -D hotfix

Solution

  1. Step 1: Delete local branch forcefully

    Use git branch -D hotfix to delete the local branch even if unmerged.
  2. Step 2: Delete remote branch

    Use git push origin --delete hotfix to remove the branch from the remote repository.
  3. Final Answer:

    git branch -D hotfix && git push origin --delete hotfix -> Option C
  4. Quick Check:

    Force delete local then delete remote = git branch -D hotfix && git push origin --delete hotfix [OK]
Hint: Force delete local then delete remote branch [OK]
Common Mistakes:
  • Deleting remote before local can cause confusion
  • Using -d instead of -D for local unmerged branches
  • Trying to delete local branch twice