Bird
Raised Fist0
Gitdevops~15 mins

Switching branches with git switch - Mini Project: Build & Apply

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
Switching Branches with git switch
📖 Scenario: You are working on a project with multiple branches. You need to switch between branches to work on different features.
🎯 Goal: Learn how to use the git switch command to change branches safely and easily.
📋 What You'll Learn
Use git switch to change branches
Create a new branch using git switch -c
List branches with git branch
Check current branch with git branch --show-current
💡 Why This Matters
🌍 Real World
Switching branches is a daily task for developers to work on different features or fixes without mixing code.
💼 Career
Knowing how to switch branches efficiently helps you collaborate with teams and manage code versions safely.
Progress0 / 4 steps
1
Create initial branches
Create two branches named feature1 and feature2 using git branch.
Git
Hint

Use git branch branch_name to create a new branch.

2
Switch to feature1 branch
Use git switch feature1 to change to the feature1 branch.
Git
Hint

Use git switch branch_name to switch branches.

3
Create and switch to a new branch
Create and switch to a new branch named feature3 using git switch -c feature3.
Git
Hint

Use git switch -c new_branch_name to create and switch to a new branch in one step.

4
Show current branch
Use git branch --show-current to display the name of the current branch.
Git
Hint

Use git branch --show-current to see which branch you are on.

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