Git & GitHub Interview Question #24

What is the difference between git branch and git switch?

Git Fundamentals, Repository, Working Tree, Staging, Commits & Branch Basics Junior Beginner

Quick Interview Answer

git branch primarily creates, lists, renames, or deletes branch references, while git switch changes the currently checked-out branch and can also create a branch with -c.

Detailed Explanation

git branch primarily creates, lists, renames, or deletes branch references, while git switch changes the currently checked-out branch and can also create a branch with -c.

Separating branch management from switching makes commands clearer. For example, git branch shows branches, while git switch feature/login moves the working tree to that branch.

Code Example

git branch
git branch feature/login
git switch feature/login