QA
Beginner LevelGit & GitHub

How do git branch, git checkout, and git switch manage local branch navigation?

❓ Interview Prompt: Compare git branch, legacy git checkout, and modern git switch for creating and navigating branches.

Branches represent isolated pointers to specific commits in the Git DAG history:

1. 'git branch':

•
'git branch': Lists all local branches (* indicates active branch).
•
'git branch feature-auth': Creates a new branch named 'feature-auth' without switching to it.

2. Legacy 'git checkout':

•
Historically handled both branch switching ('git checkout feature-auth') and file restoration ('git checkout -- file.js'). Overloaded responsibility caused user confusion!

3. Modern 'git switch' (Recommended):

•
Dedicated command introduced in Git 2.23 specifically for branch navigation:
•
'git switch feature-auth': Switch to branch.
•
'git switch -c feature-auth': Create and switch to new branch in a single command!

Code Implementation Example

bash
# List all local branches
git branch

# Modern approach: Create and switch to new feature branch
git switch -c feature/payment-gateway

# Switch back to main branch
git switch main
💡Modern branch navigation and creation using git switch -c.
🎯 Interview Key Takeaway

Use modern git switch -c <branch> to create and switch branches cleanly instead of overloaded git checkout.

#git branch#git switch#git checkout#Beginner
Back to all Git & GitHub Questions→

More Git & GitHub Interview Questions

Beginner LevelGit & GitHub

Compare git rebase versus git merge and explain how git bisect finds regressions.

Beginner LevelGit & GitHub

What is the difference between git init and git clone?

Beginner LevelGit & GitHub

What are the Three Trees in Git (Working Directory, Staging Area / Index, Commit History)?