Switching Branches

What

Switching branches updates the files in your local working directory to match the exact snapshot (commit) currently pointed to by the target branch.

How

You can switch to any pre-existing local branch using the git switch command:

# Switch to an existing branch named 'main'
git switch main

# Switch to a branch named 'feature'
git switch feature

Note: In older repositories or tutorials, you will frequently see git checkout used to change branches (e.g., git checkout main). While git checkout still works, it is a multi-purpose command that can also discard local edits, making it potentially dangerous. git switch was introduced in Git 2.23 as a dedicated, safer alternative.

Why

Isolating tasks means you will constantly jump back and forth between different lines of code. For example, if you are halfway through a new feature on feature_branch and a critical bug arises on main, you can quickly switch back to main, write a fix, commit it, and then switch back to your feature branch to resume your work without mixing up the changes.

Summary

Use git switch <branch> to navigate between active lines of development, updating your local working directory to match the target branch pointer.