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
Deleting branches
📖 Scenario: You are working on a project with multiple branches in Git. Some branches are no longer needed and should be deleted to keep the repository clean and organized.
🎯 Goal: Learn how to delete local and remote Git branches safely using the correct commands.
📋 What You'll Learn
Create a local branch named feature1
Create a local branch named feature2
Delete the local branch named feature1
Delete the remote branch named feature2
💡 Why This Matters
🌍 Real World
Deleting unused branches helps keep your Git repository clean and easier to manage, especially when working with teams.
💼 Career
Knowing how to manage branches is essential for developers and DevOps engineers to maintain code quality and collaboration.
Progress0 / 4 steps
1
Create local branches
Create two local branches named feature1 and feature2 using the git branch command.
Git
Hint
Use git branch branch_name to create a new branch.
2
Delete local branch
Delete the local branch named feature1 using the safe delete command git branch -d feature1.
Git
Hint
Use git branch -d branch_name to delete a local branch safely.
3
Delete remote branch
Delete the remote branch named feature2 using the command git push origin --delete feature2.
Git
Hint
Use git push origin --delete branch_name to delete a remote branch.
4
Verify branch deletion
Use git branch to list local branches and git branch -r to list remote branches to verify that feature1 and feature2 are deleted.
Git
Hint
Run git branch and git branch -r to see local and remote branches.
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
Step 1: Understand the command syntax
The command git branch -d is used to delete a local branch safely.
Step 2: Check the merge condition
The -d option deletes the branch only if it has been fully merged to avoid losing work.
Final Answer:
Deletes the local branch named 'feature' only if it is fully merged. -> Option B
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
Step 1: Identify force delete option
The -D option in git branch force deletes a local branch even if unmerged.
Step 2: Confirm command correctness
git branch -D bugfix is the correct syntax to force delete the local branch named 'bugfix'.
Final Answer:
git branch -D bugfix -> Option A
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
Step 1: Understand the command purpose
git push origin --delete release is used to remove a branch from the remote repository named 'origin'.
Step 2: Confirm the effect on remote branch
If the remote branch 'release' exists, this command deletes it and shows a confirmation message.
Final Answer:
Deletes the remote branch 'release' and shows confirmation. -> Option A