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 Remote Branches with Git
📖 Scenario: You are working on a team project using Git. Some branches on the remote repository are no longer needed and should be cleaned up to keep the project organized.
🎯 Goal: You will learn how to delete a remote branch using Git commands safely and correctly.
📋 What You'll Learn
Use the git branch -r command to list remote branches
Use the git push command with the correct syntax to delete a remote branch
Understand the difference between local and remote branches
💡 Why This Matters
🌍 Real World
Cleaning up remote branches helps keep the project repository organized and avoids confusion for team members.
💼 Career
Knowing how to manage remote branches is essential for collaboration in software development teams using Git.
Progress0 / 4 steps
1
List remote branches
Run the command git branch -r to list all remote branches available in your repository.
Git
Hint
This command shows all branches on the remote repository so you can see which ones exist.
2
Choose the remote branch to delete
Create a variable called branch_to_delete and set it to the exact name of the remote branch you want to delete, for example 'feature/old-update'.
Git
Hint
Use the exact branch name as shown in the remote branches list.
3
Delete the remote branch
Use the command git push origin --delete followed by the variable branch_to_delete to delete the remote branch.
Git
Hint
This command tells Git to remove the branch from the remote repository.
4
Confirm the remote branch is deleted
Run git branch -r again to confirm that the remote branch feature/old-update no longer appears in the list.
Git
Hint
The deleted branch should not appear in the output list.
Practice
(1/5)
1. What does the command git push origin --delete feature1 do?
easy
A. Deletes the remote branch named 'feature1' from the origin repository.
B. Deletes the local branch named 'feature1'.
C. Deletes all branches except 'feature1' on the remote.
D. Renames the remote branch 'feature1' to 'origin'.
Solution
Step 1: Understand the command structure
The command uses git push with origin to interact with the remote repository named 'origin'.
Step 2: Interpret the --delete flag
The --delete flag tells Git to remove the specified branch from the remote repository.
Final Answer:
Deletes the remote branch named 'feature1' from the origin repository. -> Option A
Hint: Use 'git push origin --delete branch' to remove remote branches [OK]
Common Mistakes:
Confusing remote branch deletion with local branch deletion
Using 'git branch -d' which deletes local branches only
Thinking '--delete' renames branches
Assuming it deletes all branches
2. Which of the following is the recommended modern syntax to delete a remote branch named bugfix?
easy
A. git push origin :bugfix
B. git push origin --remove bugfix
C. git branch -r -d bugfix
D. git push origin --delete bugfix
Solution
Step 1: Review valid commands for deleting remote branches
The modern and clear syntax is git push origin --delete branch-name.
Step 2: Check each option
git push origin :bugfix uses an older syntax but is valid; however, the question asks for the recommended modern syntax, which is the explicit --delete flag. git push origin --remove bugfix uses a non-existent flag --remove. git branch -r -d bugfix deletes local remote-tracking branches, not remote branches.
Confusing remote branch deletion with local branch deletion
4. You ran git push origin --delete hotfix but got an error: error: unable to delete 'hotfix': remote ref does not exist. What is the most likely cause?
medium
A. The local branch 'hotfix' is checked out and cannot be deleted.
B. You do not have permission to delete branches on the remote.
C. The remote branch 'hotfix' does not exist on the remote repository.
D. The syntax of the command is incorrect.
Solution
Step 1: Analyze the error message
The error says 'remote ref does not exist', meaning the branch named 'hotfix' is not found on the remote.
Step 2: Check command and permissions
The command syntax is correct, and permission errors usually show different messages. The local branch state does not affect remote deletion.
Final Answer:
The remote branch 'hotfix' does not exist on the remote repository. -> Option C
Quick Check:
Remote branch missing causes 'remote ref does not exist' error [OK]
Hint: Check if remote branch exists before deleting [OK]
Common Mistakes:
Assuming local branch status affects remote deletion
Blaming permissions without checking branch existence
Mistaking syntax error for branch absence
Ignoring error message details
5. You want to delete multiple remote branches named feature1, feature2, and feature3 in one command using the --delete flag. Which of the following is the correct way to do this?
C. git push origin --delete feature1 feature2 feature3
D. git branch -r -d feature1 feature2 feature3
Solution
Step 1: Understand multi-branch deletion syntax
Git does not support deleting multiple remote branches in one command by listing them after --delete. You must run separate commands or use multiple --delete flags.
Step 2: Evaluate options
git push origin --delete feature1 feature2 feature3 is invalid syntax and will cause an error. git push origin --delete feature1 && git push origin --delete feature2 && git push origin --delete feature3 runs multiple commands separately, which works correctly. git push origin :feature1 :feature2 :feature3 uses old syntax without the --delete flag and may not be recommended. git branch -r -d feature1 feature2 feature3 deletes local remote-tracking branches, not remote branches.