Bird
Raised Fist0
Gitdevops~10 mins

Creating and switching in one step in Git - Visual Walkthrough

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
Process Flow - Creating and switching in one step
Start on current branch
Run git switch -c <new-branch>
Create new branch named <new-branch>
Switch HEAD to <new-branch>
Now on new branch
Done
This flow shows how git creates a new branch and switches to it immediately using one command.
Execution Sample
Git
git switch -c feature-x
Creates a new branch named 'feature-x' and switches to it in one step.
Process Table
StepCommandActionResultCurrent Branch
1git switch -c feature-xCheck if 'feature-x' existsDoes not existmain
2git switch -c feature-xCreate branch 'feature-x'Branch createdmain
3git switch -c feature-xSwitch HEAD to 'feature-x'Switched to new branch 'feature-x'feature-x
4-End of commandNow working on 'feature-x'feature-x
💡 New branch 'feature-x' created and switched to successfully.
Status Tracker
VariableStartAfter Step 2After Step 3Final
Current Branchmainmainfeature-xfeature-x
Branch 'feature-x' Exists?NoYesYesYes
Key Moments - 2 Insights
Why does the command create a new branch instead of switching to an existing one?
Because the '-c' option tells git to create a new branch. If the branch already existed, git would show an error. See execution_table step 1 and 2.
What happens if you omit '-c' and just run 'git switch feature-x'?
Git tries to switch to an existing branch named 'feature-x'. If it doesn't exist, it errors out. The '-c' option is needed to create and switch in one step.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the current branch after step 3?
Amain
Bfeature-x
Cmaster
Ddevelop
💡 Hint
Check the 'Current Branch' column in execution_table row for step 3.
At which step does git create the new branch?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Action' and 'Result' columns in execution_table for branch creation.
If the branch 'feature-x' already existed, what would happen at step 2?
AGit shows an error and stops
BGit creates a new branch anyway
CGit switches to the existing branch without error
DGit deletes the existing branch and creates a new one
💡 Hint
Recall the explanation in key_moments about the '-c' option behavior.
Concept Snapshot
git switch -c <branch-name>
Creates a new branch and switches to it immediately.
If branch exists, command errors out.
Saves time vs separate create and switch commands.
Use to start work on a new feature branch quickly.
Full Transcript
This visual execution shows how the git command 'git switch -c feature-x' works step-by-step. First, git checks if the branch 'feature-x' exists. Since it does not, git creates the new branch. Then git switches the HEAD pointer to the new branch, making it the current working branch. The variable tracker shows the current branch changing from 'main' to 'feature-x'. Key moments clarify that the '-c' option means create and switch, and if the branch already exists, git will error. The quiz tests understanding of when the branch is created, what the current branch is after switching, and what happens if the branch exists already. This command is a shortcut to create and switch branches in one step, saving time and commands.

Practice

(1/5)
1. What does the command git switch -c feature do?
easy
A. Lists all branches including 'feature'
B. Creates a new branch named 'feature' and switches to it
C. Switches to the existing branch named 'feature' without creating it
D. Deletes the branch named 'feature'

Solution

  1. Step 1: Understand the git switch -c command

    The -c option means "create" a new branch and switch to it immediately.
  2. Step 2: Apply to the branch name 'feature'

    The command creates a new branch called 'feature' and switches the working directory to it.
  3. Final Answer:

    Creates a new branch named 'feature' and switches to it -> Option B
  4. Quick Check:

    git switch -c = create and switch [OK]
Hint: Remember: -c means create and switch in one step [OK]
Common Mistakes:
  • Thinking it only switches without creating
  • Confusing with branch deletion commands
  • Assuming it lists branches
2. Which of the following is the correct git switch syntax to create and switch to a new branch named dev?
easy
A. git switch -c dev
B. git switch dev -c
C. git checkout -b dev
D. git branch -c dev

Solution

  1. Step 1: Recall the correct order of options in git switch

    The option -c must come before the branch name to create and switch.
  2. Step 2: Check each option

    git switch -c dev is the correct syntax. git switch dev -c has wrong order. git checkout -b dev uses checkout instead of switch. git branch -c dev is invalid syntax.
  3. Final Answer:

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

    Correct syntax = git switch -c branch [OK]
Hint: Option -c always comes before branch name in git switch [OK]
Common Mistakes:
  • Placing -c after branch name
  • Confusing git switch with git branch
  • Using git branch -c which is invalid
3. What will be the output of the following commands?
git switch -c test-branch
git branch --show-current
medium
A. Error: branch does not exist
B. main
C. No output
D. test-branch

Solution

  1. Step 1: Create and switch to 'test-branch'

    The first command creates a new branch named 'test-branch' and switches to it.
  2. Step 2: Show current branch

    The second command prints the name of the current branch, which is now 'test-branch'.
  3. Final Answer:

    test-branch -> Option D
  4. Quick Check:

    Current branch after switch -c = new branch name [OK]
Hint: After switch -c, current branch is the new branch [OK]
Common Mistakes:
  • Assuming it stays on main branch
  • Expecting no output from show-current
  • Thinking it causes an error
4. You run git switch -c featureX but get an error: fatal: A branch named 'featureX' already exists. What should you do to switch to that branch?
medium
A. Run git switch featureX
B. Run git switch -c featureX again
C. Run git branch featureX
D. Run git checkout -b featureX

Solution

  1. Step 1: Understand the error

    The error means the branch 'featureX' already exists, so you cannot create it again.
  2. Step 2: Switch to existing branch

    Use git switch featureX without -c to switch to the existing branch.
  3. Final Answer:

    Run git switch featureX -> Option A
  4. Quick Check:

    Use switch without -c to switch existing branch [OK]
Hint: Use git switch without -c to switch existing branch [OK]
Common Mistakes:
  • Trying to create branch again with -c
  • Using git branch without switching
  • Using checkout -b which creates new branch
5. You want to create a new branch named release-1.0 and immediately start working on it. Which command correctly does this and also sets the upstream to origin/release-1.0 in one step?
hard
A. git checkout -b release-1.0 origin/release-1.0
B. git branch release-1.0 && git switch release-1.0
C. git switch -c release-1.0 --track origin/release-1.0
D. git switch release-1.0 -c --set-upstream origin/release-1.0

Solution

  1. Step 1: Understand creating and switching with upstream

    The git switch -c command can create and switch to a branch. The --track option sets the upstream branch.
  2. Step 2: Analyze options

    git switch -c release-1.0 --track origin/release-1.0 creates the branch, switches to it, and sets upstream in one step. git branch release-1.0 && git switch release-1.0 requires two commands. git checkout -b release-1.0 origin/release-1.0 creates and switches from the remote commit and sets upstream automatically. git switch release-1.0 -c --set-upstream origin/release-1.0 has wrong option order and syntax.
  3. Final Answer:

    git switch -c release-1.0 --track origin/release-1.0 -> Option C
  4. Quick Check:

    Use switch -c with --track to create, switch, and set upstream [OK]
Hint: Use --track with switch -c to set upstream in one step [OK]
Common Mistakes:
  • Not setting upstream in one step
  • Using git branch and switch separately
  • Wrong option order or syntax