Bird
Raised Fist0
Gitdevops~5 mins

Working in multiple branches simultaneously in Git - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes you need to work on different features or fixes at the same time. Git branches let you keep these changes separate so you can switch between tasks without mixing code.
When you want to fix a bug while working on a new feature without losing your progress.
When you need to test different ideas in separate branches before deciding which one to keep.
When collaborating with others, and each person works on their own branch to avoid conflicts.
When you want to prepare a release branch while continuing development on the main branch.
When you want to review or merge changes from one branch to another safely.
Commands
This command creates a new branch called 'feature-login' to work on the login feature separately.
Terminal
git branch feature-login
Expected OutputExpected
No output (command runs silently)
Switches your working directory to the 'feature-login' branch so you can start making changes there.
Terminal
git checkout feature-login
Expected OutputExpected
Switched to branch 'feature-login'
Creates another branch named 'bugfix-header' to fix a header issue without disturbing the login feature work.
Terminal
git branch bugfix-header
Expected OutputExpected
No output (command runs silently)
Switches to the 'bugfix-header' branch so you can work on the header fix independently.
Terminal
git checkout bugfix-header
Expected OutputExpected
Switched to branch 'bugfix-header'
Switches back to the main branch to see the stable code or prepare for merging your changes.
Terminal
git checkout main
Expected OutputExpected
Switched to branch 'main'
Merges the changes from 'bugfix-header' into the current branch (main) to include the header fix.
Terminal
git merge bugfix-header
Expected OutputExpected
Updating abc1234..def5678 Fast-forward header.html | 2 ++ 1 file changed, 2 insertions(+)
Key Concept

If you remember nothing else from this pattern, remember: branches let you work on different tasks separately and switch between them without mixing changes.

Common Mistakes
Trying to work on two branches at the same time without switching branches.
Git only lets you work on one branch at a time in your working directory, so changes can get mixed or lost.
Use 'git checkout branch-name' to switch to the branch you want to work on before making changes.
Creating a branch but forgetting to switch to it before starting work.
Your changes will go to the current branch, not the new one, causing confusion and mixing code.
After creating a branch with 'git branch', always run 'git checkout branch-name' to switch to it.
Merging branches without checking for conflicts or testing.
Conflicts can cause broken code or lost changes if not handled carefully.
Always review and test your code after merging, and resolve conflicts carefully if they appear.
Summary
Create separate branches for different tasks using 'git branch branch-name'.
Switch between branches with 'git checkout branch-name' to work on them independently.
Merge finished work back into main branches carefully using 'git merge branch-name'.

Practice

(1/5)
1. What is the main purpose of working with multiple branches simultaneously in Git?
easy
A. To develop features independently without affecting the main code
B. To delete all previous commits
C. To merge all branches into one automatically
D. To speed up the internet connection

Solution

  1. Step 1: Understand branches in Git

    Branches allow you to work on different versions of your project independently.
  2. Step 2: Purpose of multiple branches

    Working on multiple branches lets you develop features or fixes without disturbing the main code.
  3. Final Answer:

    To develop features independently without affecting the main code -> Option A
  4. Quick Check:

    Branches isolate work = B [OK]
Hint: Branches isolate work so main code stays safe [OK]
Common Mistakes:
  • Thinking branches delete commits
  • Believing branches merge automatically
  • Confusing branches with internet speed
2. Which command correctly creates and switches to a new branch named feature in one step?
easy
A. git checkout feature
B. git switch -c feature
C. git create branch feature
D. git branch feature && git checkout feature

Solution

  1. Step 1: Identify command to create and switch branch

    The command git switch -c <branch> creates and switches to the branch in one step.
  2. Step 2: Check other options

    git checkout feature switches but does not create if branch doesn't exist. git branch feature && git checkout feature is two commands combined. git create branch feature is invalid syntax.
  3. Final Answer:

    git switch -c feature -> Option B
  4. Quick Check:

    Create and switch = git switch -c [OK]
Hint: Use 'git switch -c' to create and switch branch fast [OK]
Common Mistakes:
  • Using 'git checkout' without creating branch
  • Trying invalid commands like 'git create branch'
  • Combining commands incorrectly
3. Given the commands below, what will be the output of git branch?
git branch feature
git switch feature
git branch bugfix
git switch bugfix
git branch
medium
A. * bugfix feature main
B. * feature bugfix main
C. * main feature bugfix
D. * bugfix main feature

Solution

  1. Step 1: Track branch creation and switching

    Branches created: feature, bugfix. Switched last to bugfix, so bugfix is current.
  2. Step 2: Understand git branch output

    It lists all branches, marking current with '*'. Order is alphabetical by default: bugfix, feature, main.
  3. Final Answer:

    * bugfix feature main -> Option A
  4. Quick Check:

    Current branch marked * = C [OK]
Hint: Current branch shows *; branches listed alphabetically [OK]
Common Mistakes:
  • Assuming last created branch is current
  • Ignoring the * mark for current branch
  • Misordering branch list
4. You tried to switch to a branch named dev using git switch dev but got an error: error: pathspec 'dev' did not match any branch. What is the likely cause?
medium
A. The branch dev is already checked out
B. You are not inside a Git repository
C. You have uncommitted changes blocking the switch
D. The branch dev does not exist yet

Solution

  1. Step 1: Analyze the error message

    The error says the branch name was not found, meaning it does not exist locally.
  2. Step 2: Check other causes

    Not being in a repo gives a different error. Uncommitted changes usually warn differently. Already checked out branch won't error.
  3. Final Answer:

    The branch dev does not exist yet -> Option D
  4. Quick Check:

    Branch missing = error pathspec not matched [OK]
Hint: Error means branch missing locally, create it first [OK]
Common Mistakes:
  • Assuming uncommitted changes cause this error
  • Thinking you are outside a Git repo
  • Believing branch is already checked out
5. You want to work on two features at the same time: feature1 and feature2. You want to keep your changes separate but test both locally without switching branches repeatedly. Which Git tool helps you do this?
hard
A. Git merge to combine branches
B. Git stash to save changes temporarily
C. Git worktree to create multiple working directories
D. Git rebase to rewrite history

Solution

  1. Step 1: Understand the problem

    You want to work on two branches simultaneously without switching back and forth.
  2. Step 2: Identify the Git tool

    Git worktree allows multiple working directories linked to different branches, enabling simultaneous work.
  3. Step 3: Check other options

    Git stash saves changes temporarily but doesn't allow simultaneous workspaces. Merge and rebase change history or combine branches, not simultaneous work.
  4. Final Answer:

    Git worktree to create multiple working directories -> Option C
  5. Quick Check:

    Multiple workspaces = git worktree [OK]
Hint: Use git worktree for multiple branches open at once [OK]
Common Mistakes:
  • Using stash to try simultaneous work
  • Confusing merge with parallel work
  • Thinking rebase helps with multiple working copies
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard