DEV Community

Pheak Pheasa
Pheak Pheasa

Posted on • Edited on

git create branch from main

Method 1: Create branch from main, then switch

git branch <new-branch-name> main
git checkout <new-branch-name>
Enter fullscreen mode Exit fullscreen mode

Method 2: Create and switch in one command

git checkout -b <new-branch-name> main
Enter fullscreen mode Exit fullscreen mode

Method 3: Using newer git switch command

git switch -c <new-branch-name> main
Enter fullscreen mode Exit fullscreen mode

Examples:

# Create a new feature branch from main
git checkout -b feature-user-authentication main

# Create a bug fix branch from main
git switch -c fix-login-issue main

# Create a hotfix branch from main
git checkout -b hotfix-critical-bug main
Enter fullscreen mode Exit fullscreen mode

After created new brach you can't push/pull your code let's try here first:

git push --set-upstream origin <branch-name-you-created>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)