How to create a branch in Git

· Category: Git

Short answer

Use git branch <name> to create a branch, or git checkout -b <name> to create and switch to it immediately.

Steps

  1. Create a branch:
git branch feature-login
  1. Create and switch:
git checkout -b feature-login
  1. With newer Git:
git switch -c feature-login
  1. Push the new branch to remote:
git push -u origin feature-login

Tips

  • Use descriptive branch names like feature/user-auth or fix/login-bug.
  • Delete old branches with git branch -d feature-login after merging.
  • List all branches with git branch -a.

Common issues

  • Creating a branch from the wrong base: switch to the correct parent branch first.
  • Branch name conflicts: choose unique names to avoid confusion.