Bird
Raised Fist0
Gitdevops~5 mins

Switching branches with git switch - Cheat Sheet & Quick Revision

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
Recall & Review
beginner
What does the command git switch <branch-name> do?
It changes your current working branch to the specified branch named <branch-name>.
Click to reveal answer
beginner
How do you create and switch to a new branch named feature using git switch?
Use git switch -c feature. The -c flag creates the new branch and switches to it immediately.
Click to reveal answer
beginner
What happens if you try git switch to a branch that does not exist without the -c option?
Git will show an error saying the branch does not exist and will not switch branches.
Click to reveal answer
intermediate
Why might you prefer git switch over git checkout for switching branches?
git switch is simpler and only focuses on switching branches, making it easier and less error-prone for beginners.
Click to reveal answer
intermediate
What command would you use to switch back to the previous branch you were on?
Use git switch - to quickly go back to the last branch you had checked out.
Click to reveal answer
Which command switches to an existing branch named dev?
Agit switch -c dev
Bgit switch new dev
Cgit switch --create dev
Dgit switch dev
How do you create and switch to a new branch called feature1?
Agit switch feature1
Bgit switch -c feature1
Cgit switch --new feature1
Dgit switch -n feature1
What will happen if you run git switch unknown-branch and the branch does not exist?
AGit shows an error and does not switch
BGit switches to the default branch
CGit creates the branch and switches to it
DGit deletes the current branch
Which command switches back to the previous branch you were on?
Agit switch -
Bgit switch last
Cgit switch back
Dgit switch previous
Why is git switch recommended over git checkout for switching branches?
AIt deletes old branches automatically
BIt is faster to type
CIt only switches branches and avoids confusion
DIt merges branches automatically
Explain how to switch to an existing branch and how to create and switch to a new branch using git switch.
Think about the difference between switching and creating branches.
You got /2 concepts.
    Describe what happens if you try to switch to a branch that does not exist without creating it.
    Consider what git expects when switching branches.
    You got /3 concepts.

      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