Bird
Raised Fist0
Gitdevops~20 mins

Switching branches with git switch - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Git Switch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
Output of switching to an existing branch
What is the output when you run git switch feature-xyz if the branch feature-xyz exists and you have no uncommitted changes?
Aerror: pathspec 'feature-xyz' did not match any file(s) known to git
BSwitched to branch 'feature-xyz'
Cfatal: You have uncommitted changes. Please commit or stash them first.
DAlready on 'feature-xyz'
Attempts:
2 left
💡 Hint
Think about what git says when you successfully switch branches.
💻 Command Output
intermediate
1:30remaining
Error when switching to a non-existent branch
What error message appears when you run git switch non-existent-branch and the branch does not exist?
Aerror: pathspec 'non-existent-branch' did not match any file(s) known to git
Bfatal: You have uncommitted changes. Please commit or stash them first.
CSwitched to branch 'non-existent-branch'
DAlready on 'non-existent-branch'
Attempts:
2 left
💡 Hint
Git tells you when the branch name is unknown.
🔀 Workflow
advanced
1:30remaining
Creating and switching to a new branch in one command
Which command correctly creates a new branch named hotfix and switches to it immediately?
Agit switch hotfix -b
Bgit switch --new hotfix
Cgit switch -c hotfix
Dgit switch create hotfix
Attempts:
2 left
💡 Hint
The option to create a branch with git switch is a single letter.
Troubleshoot
advanced
1:30remaining
Error when switching branches with uncommitted changes
What error message will you get if you try to run git switch develop while having uncommitted changes that conflict with the develop branch?
Aerror: Your local changes to the following files would be overwritten by checkout:
Bfatal: You have uncommitted changes. Please commit or stash them first.
CSwitched to branch 'develop'
DAlready on 'develop'
Attempts:
2 left
💡 Hint
Git warns about overwriting local changes when switching branches.
🧠 Conceptual
expert
2:00remaining
Behavior of git switch with detached HEAD
What happens when you run git switch --detach without specifying a branch or commit?
AGit switches to the default branch (usually main or master).
BGit throws an error because a commit or branch must be specified.
CGit creates a new branch named 'detach' and switches to it.
DGit detaches HEAD at the current commit, allowing you to explore without moving any branch.
Attempts:
2 left
💡 Hint
Think about what 'detached HEAD' means in Git.

Practice

(1/5)
1. What does the command git switch feature do in a Git repository?
easy
A. It shows the list of all branches including 'feature'.
B. It creates a new branch named 'feature' without switching to it.
C. It changes the current branch to the branch named 'feature'.
D. It deletes the branch named 'feature'.

Solution

  1. Step 1: Understand the git switch command

    The command git switch <branch-name> is used to change the current working branch to the specified branch.
  2. Step 2: Apply to the given command

    Here, git switch feature changes the current branch to the existing branch named 'feature'.
  3. Final Answer:

    It changes the current branch to the branch named 'feature'. -> Option C
  4. Quick Check:

    git switch <branch> changes branch [OK]
Hint: Use git switch plus branch name to move branches fast [OK]
Common Mistakes:
  • Thinking git switch creates a branch without -c
  • Confusing git switch with git branch
  • Assuming git switch deletes branches
  • Believing git switch lists branches
2. Which of the following is the correct syntax to create and switch to a new branch named dev using git switch?
easy
A. git switch -c dev
B. git switch dev -c
C. git switch --new dev
D. git switch create dev

Solution

  1. Step 1: Recall the syntax for creating and switching branches

    The correct syntax to create and switch to a new branch is git switch -c <branch-name>.
  2. Step 2: Match the syntax with options

    git switch -c dev uses git switch -c dev, which is the correct form.
  3. Final Answer:

    git switch -c dev -> Option A
  4. Quick Check:

    git switch -c <branch> creates and switches [OK]
Hint: Use -c right after git switch to create and switch [OK]
Common Mistakes:
  • Placing -c after branch name
  • Using --new instead of -c
  • Typing create instead of -c
  • Omitting the -c flag
3. Given the following commands run in order:
git switch -c test
echo 'hello' > file.txt
git add file.txt
git commit -m 'Add file'
git switch main

What is the current branch and the status of file.txt after these commands?
medium
A. On branch 'main', file.txt is not present in main branch.
B. On branch 'test', file.txt is committed and present.
C. On branch 'main', file.txt is staged but not committed.
D. On branch 'test', file.txt is untracked.

Solution

  1. Step 1: Analyze branch creation and commit

    The command git switch -c test creates and switches to 'test' branch. Then file.txt is created, added, and committed on 'test'.
  2. Step 2: Switch back to main branch

    The command git switch main switches to 'main' branch. Since file.txt was committed only on 'test', it does not exist on 'main'.
  3. Final Answer:

    On branch 'main', file.txt is not present in main branch. -> Option A
  4. Quick Check:

    Switching branches changes files to that branch's state [OK]
Hint: Committed files stay only on their branch until merged [OK]
Common Mistakes:
  • Assuming committed files appear on all branches
  • Confusing staged and committed states
  • Thinking switching branches keeps uncommitted changes
  • Believing file.txt is tracked on main after switch
4. You try to run git switch feature but get the error: error: Your local changes to the following files would be overwritten by checkout:
What should you do to fix this and switch branches safely?
medium
A. Use git switch -f feature to force switch ignoring changes.
B. Commit or stash your changes before switching branches.
C. Delete the files manually and try again.
D. Run git reset --hard without saving changes.

Solution

  1. Step 1: Understand the error meaning

    The error means you have local changes that would be lost if you switch branches.
  2. Step 2: Safely save changes before switching

    You should either commit your changes or stash them to save work before switching branches.
  3. Final Answer:

    Commit or stash your changes before switching branches. -> Option B
  4. Quick Check:

    Save changes before switching to avoid overwrite errors [OK]
Hint: Commit or stash changes before switching branches [OK]
Common Mistakes:
  • Forcing switch and losing work
  • Deleting files manually causing data loss
  • Resetting hard without backup
  • Ignoring the error and expecting switch to work
5. You want to create a new branch release from main, switch to it, and keep your current uncommitted changes safe. Which sequence of commands achieves this correctly?
hard
A. git switch -c release
git stash
git stash pop
B. git commit -m 'temp'
git switch -c release
C. git switch release
git stash
git switch -c release
D. git stash
git switch -c release
git stash pop

Solution

  1. Step 1: Save uncommitted changes safely

    Use git stash to save current uncommitted changes temporarily.
  2. Step 2: Create and switch to new branch

    Run git switch -c release to create and switch to the 'release' branch.
  3. Step 3: Restore saved changes

    Use git stash pop to apply the saved changes to the new branch.
  4. Final Answer:

    git stash
    git switch -c release
    git stash pop
    -> Option D
  5. Quick Check:

    Stash changes, switch branch, then pop stash [OK]
Hint: Stash changes before switching, then pop after [OK]
Common Mistakes:
  • Switching branch before stashing causing errors
  • Committing temporary changes unnecessarily
  • Forgetting to pop stash after switching
  • Trying to switch without saving changes