Creating Branches
What
Creating a new branch tells Git to set up a new mobile pointer referencing the exact commit you are currently standing on.
How
You can create a branch using two different methods depending on whether you want to immediately switch your active working directory to it:
-
Create only: Creates the branch pointer but leaves you on your current active branch:
git branch my_feature_branch -
Create and switch: Creates the branch pointer and immediately switches your active working directory to the new branch:
git switch -c my_feature_branch
Note: The -c flag stands for create. In older versions of Git, this was accomplished using the git checkout -b <name> syntax, but git switch -c is the modern standard.
Why
Using git switch -c is a highly efficient best practice. When you decide to start working on a new task, you almost always want to write your code immediately on that new branch rather than writing code on main and having to clean it up or migrate it later. Creating and switching in a single step prevents accidental commits to the wrong branch.
Summary
Use git branch <name> to create a branch pointer, or use git switch -c <name> to create it and switch your working directory to it in one action.