0
0
Gitdevops~20 mins

Creating and switching in one step in Git - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Branch Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of creating and switching to a new branch
What is the output of the command git switch -c feature when run in a clean repository?
Git
git switch -c feature
ASwitched to a new branch 'feature'
Berror: pathspec 'feature' did not match any file(s) known to git
CAlready on 'feature'
Dfatal: You are not currently on a branch.
Attempts:
2 left
💡 Hint
The command creates a new branch and switches to it in one step.
💻 Command Output
intermediate
2:00remaining
Error when switching to an existing branch with -c
What happens if you run git switch -c main when the branch 'main' already exists?
Git
git switch -c main
Aerror: pathspec 'main' did not match any file(s) known to git
Bfatal: A branch named 'main' already exists.
CSwitched to branch 'main'
DSwitched to a new branch 'main'
Attempts:
2 left
💡 Hint
The -c option tries to create a new branch, so it fails if the branch exists.
🔀 Workflow
advanced
2:00remaining
Correct command to create and switch to a new branch in one step
Which command correctly creates and switches to a new branch named 'release' in one step?
Agit switch -c release
Bgit switch release -c
Cgit checkout -b release
Dgit branch release && git switch release
Attempts:
2 left
💡 Hint
The git switch command with -c creates and switches in one step.
Troubleshoot
advanced
2:00remaining
Troubleshooting error when creating and switching branch
You run git switch -c feature but get the error: fatal: You have unstaged changes. What is the most likely cause?
AYou are not inside a Git repository.
BThe branch 'feature' already exists.
CYou have uncommitted changes that conflict with the new branch creation.
DThe remote repository is unreachable.
Attempts:
2 left
💡 Hint
Git prevents switching branches if unstaged changes would be lost.
Best Practice
expert
2:00remaining
Best practice for creating and switching branches in scripts
In an automated script, which command is best to create and switch to a new branch 'hotfix' only if it does not already exist, without causing errors?
Agit branch hotfix || git switch hotfix
Bgit switch -c hotfix
Cgit switch hotfix || git switch -c hotfix
Dgit switch hotfix 2>/dev/null || git switch -c hotfix
Attempts:
2 left
💡 Hint
Suppress errors when switching to existing branch, then create if missing.