How to create a new Git branch and switch to it

· Category: Git

Short answer

Use git checkout -b new-branch-name to create and switch to a new branch in one command. Alternatively, use git branch new-name followed by git checkout new-name. For resolving conflicts when merging branches, see how to resolve Git merge conflicts. For moving specific commits between branches, see how to cherry-pick a commit in Git.

Steps

  1. Ensure you are on the correct base branch: git checkout main
  2. Pull latest changes: git pull origin main
  3. Create and switch: git checkout -b feature/user-login
  4. Make your changes and commit
  5. Push the branch: git push -u origin feature/user-login

Tips