Creating Branches

12.1 Creating a New Branch

To create a branch:
1git branch <branch-name>

Example:
1git branch feature/login-auth

This only creates the branch, it does not switch to it.

12.2 Creating and Switching in One Step

The most common way:
1git switch -c <branch-name>

or the older syntax:
1git checkout -b <branch-name>

Example:
1git switch -c feature/cart-ui

12.3 Creating a Branch from a Specific Commit

Sometimes you want your branch to start from a particular commit instead of the current one.
1git branch <branch-name> <commit-hash>

Example:
1git branch legacy-fix 3a9f122

Useful for:
Fixing older versions
Starting a patch on historical code
Creating branches from tags or stable release points

12.4 Listing All Branches

To see all local branches:
1git branch

To see remote + local branches:
1git branch -a

Remote branches appear as:
1remotes/origin/main 2remotes/origin/feature/login-auth

12.5 Pushing a New Branch to Remote

After creating a local branch, push it to GitHub:
1git push -u origin <branch-name>

Example:
1git push -u origin feature/user-profile

-u sets upstream tracking, so next time you can simply run:
1git push 2git pull

12.6 Key Takeaways

git branch <name> creates a branch; git switch -c <name> creates and switches to it.
Follow clean naming conventions like feature/, bugfix/, hotfix/, and release/.
You can create branches from any commit or tag, not just the latest one.
Use git branch -a to view local and remote branches.
Use git push -u origin <branch> to publish a new branch and set upstream tracking.