Bird
Raised Fist0
Gitdevops~10 mins

Creating branches with git branch - 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 branches with git branch
Start in main branch
Run 'git branch <new-branch>'
New branch created locally
Check branches with 'git branch'
New branch listed, current branch unchanged
This flow shows how creating a new branch with 'git branch' adds it locally without switching to it.
Execution Sample
Git
git branch feature1
git branch
Creates a new branch named 'feature1' and lists all branches.
Process Table
StepCommandActionResultCurrent Branch
1git branch feature1Create new branch 'feature1'Branch 'feature1' created locallymain
2git branchList all branches* main feature1main
💡 No branch switch occurs; current branch remains 'main'.
Status Tracker
VariableStartAfter Step 1After Step 2
Branchesmainmain, feature1main, feature1
Current Branchmainmainmain
Key Moments - 2 Insights
Does 'git branch <name>' switch to the new branch?
No, it only creates the branch locally. The current branch stays the same as shown in the execution_table step 1 and 2.
How can I see the new branch after creating it?
Use 'git branch' to list all branches. The new branch will appear but is not checked out yet, as shown in execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the current branch after running 'git branch feature1'?
Amain
Bfeature1
CNo branch
DDetached HEAD
💡 Hint
Check the 'Current Branch' column in execution_table row 1.
At which step does the new branch 'feature1' appear in the branch list?
AStep 1
BBefore Step 1
CStep 2
DIt never appears
💡 Hint
Look at the 'Result' column in execution_table for branch listing.
If you want to switch to the new branch immediately after creating it, what should you do?
ARun 'git branch feature1' again
BRun 'git checkout feature1'
CRun 'git branch -d feature1'
DRun 'git merge feature1'
💡 Hint
Creating a branch does not switch to it; checkout is needed to switch branches.
Concept Snapshot
git branch <name> creates a new branch locally without switching.
Use 'git branch' to list all branches.
Current branch remains unchanged after creation.
To switch, use 'git checkout <name>' or 'git switch <name>'.
Full Transcript
Creating a branch with 'git branch <name>' adds a new branch locally but does not change your current branch. After running the command, you can list all branches with 'git branch' and see the new branch listed. However, you remain on the original branch until you explicitly switch using 'git checkout' or 'git switch'. This helps you prepare branches without moving away from your current work.

Practice

(1/5)
1. What does the command git branch new-feature do?
easy
A. Creates a new branch named 'new-feature' without switching to it
B. Creates and switches to a new branch named 'new-feature'
C. Deletes the branch named 'new-feature'
D. Merges 'new-feature' branch into the current branch

Solution

  1. Step 1: Understand the git branch command

    The command git branch <branch-name> creates a new branch but does not switch to it.
  2. Step 2: Analyze the given command

    git branch new-feature creates a branch called 'new-feature' but stays on the current branch.
  3. Final Answer:

    Creates a new branch named 'new-feature' without switching to it -> Option A
  4. Quick Check:

    git branch creates branch only [OK]
Hint: git branch creates branch but does not switch [OK]
Common Mistakes:
  • Thinking it switches to the new branch automatically
  • Confusing branch creation with branch deletion
  • Assuming it merges branches
2. Which of the following is the correct syntax to create a branch named feature1 using git?
easy
A. git branch -c feature1
B. git create branch feature1
C. git branch feature1
D. git new branch feature1

Solution

  1. Step 1: Recall the correct git branch creation syntax

    The correct syntax to create a branch is git branch <branch-name>.
  2. Step 2: Check each option

    git branch feature1 matches the correct syntax exactly: git branch feature1. Others are invalid commands.
  3. Final Answer:

    git branch feature1 -> Option C
  4. Quick Check:

    Correct syntax = git branch <name> [OK]
Hint: Use 'git branch branch-name' to create branch [OK]
Common Mistakes:
  • Using incorrect flags like -c
  • Adding extra words like 'create' or 'new'
  • Confusing branch creation with checkout
3. Given the commands:
git branch test-branch
git branch
What will be the output of git branch?
medium
A. main\ntest-branch
B. main\n* test-branch
C. * test-branch
D. * main\n test-branch

Solution

  1. Step 1: Understand branch creation and listing

    git branch test-branch creates a new branch but does not switch to it. The current branch remains the same.
  2. Step 2: Check the output of git branch

    The output lists all branches. The current branch is marked with an asterisk (*). Since we did not switch, the current branch is still 'main'.
  3. Final Answer:

    * main\n test-branch -> Option D
  4. Quick Check:

    Current branch marked *; new branch listed but not active [OK]
Hint: New branch created but current branch stays same [OK]
Common Mistakes:
  • Assuming new branch is active immediately
  • Missing the asterisk for current branch
  • Listing branches without indentation or markers
4. You run git branch new-feature but then try git checkout new-feature and get an error. What is the most likely cause?
medium
A. You misspelled the branch name when checking out
B. You need to use git switch instead of git checkout
C. The branch was not created because of a syntax error
D. You are not in a git repository

Solution

  1. Step 1: Understand the error context

    If git checkout new-feature fails after creating the branch, the branch might not exist under that exact name.
  2. Step 2: Check common causes

    Most often, the branch name is misspelled or has a typo when checking out. Other options are less likely if branch creation succeeded.
  3. Final Answer:

    You misspelled the branch name when checking out -> Option A
  4. Quick Check:

    Branch name typo causes checkout error [OK]
Hint: Check branch name spelling before checkout [OK]
Common Mistakes:
  • Assuming checkout command is deprecated
  • Ignoring typos in branch names
  • Not verifying current directory is a git repo
5. You want to create a new branch featureX and immediately start working on it. Which sequence of commands correctly achieves this?
hard
A. git checkout -b featureX
B. All of the above
C. git branch featureX\ngit switch featureX
D. git branch featureX\ngit checkout featureX

Solution

  1. Step 1: Understand branch creation and switching

    Creating a branch and switching to it can be done in multiple ways: using separate commands or combined commands.
  2. Step 2: Analyze each option

    git branch featureX\ngit checkout featureX creates the branch then switches using checkout. git checkout -b featureX creates and switches in one step. git branch featureX\ngit switch featureX creates then switches using the newer 'git switch' command. All are valid.
  3. Final Answer:

    All of the above -> Option B
  4. Quick Check:

    Multiple valid ways to create and switch branch [OK]
Hint: Use 'git checkout -b' or 'git switch -c' to create and switch [OK]
Common Mistakes:
  • Thinking only one command works
  • Confusing 'git switch' with 'git checkout'
  • Not knowing combined commands exist