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
- Create a branch:
git branch feature-login
- Create and switch:
git checkout -b feature-login
- With newer Git:
git switch -c feature-login
- Push the new branch to remote:
git push -u origin feature-login
Tips
- Use descriptive branch names like
feature/user-authorfix/login-bug. - Delete old branches with
git branch -d feature-loginafter 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.