Script Valley
Git and GitHub Complete Course: From Beginner to Advanced
Branching, Merging, and Resolving ConflictsLesson 2.2

Creating and Switching Branches

git branch, git checkout, git switch, create branch, delete branch, branch naming conventions

Creating and Switching Branches

Creating and switching branches are the most frequent Git operations in professional development. Git provides two commands for switching branches: the older git checkout and the newer git switch. Both work, but git switch is more explicit and safer.

Diagramgit switch vs git checkout

IMAGE PROMPT (replace this block with your generated image):

Flat side-by-side command comparison card on white background. Title: Creating and Switching Branches. Top section: a two-column table showing equivalent old vs new commands. Column header left: Old Syntax (checkout) (gray badge). Column header right: New Syntax (switch) (#3A5EFF badge). Row 1: git checkout -b feature/x | git switch -c feature/x โ€” label: Create + switch. Row 2: git checkout main | git switch main โ€” label: Switch to existing. Row 3: git checkout - | git switch - โ€” label: Switch to previous branch. Below table: a branch naming convention card with four color-coded pills. Pill 1: feature/user-login (green fill). Pill 2: fix/null-pointer-bug (red fill). Pill 3: docs/api-reference (blue fill). Pill 4: chore/upgrade-deps (gray fill). Delete branch note at bottom: git branch -d feature/x (safe) vs git branch -D feature/x (force). White background.

Creating a Branch

To create a new branch without switching to it:

git branch feature/user-login

To create and immediately switch to it:

git switch -c feature/user-login
# or the older syntax:
git checkout -b feature/user-login

Switching Between Branches

To switch to an existing branch:

git switch main
# or:
git checkout main

Before switching, ensure your working directory is clean. Either commit your changes or stash them with git stash.

Branch Naming Conventions

Consistent naming makes large repositories manageable. Common patterns include: feature/feature-name for new features, fix/bug-description for bug fixes, docs/section-name for documentation, and chore/task-name for maintenance tasks.

Deleting a Branch

After a feature branch is merged, delete it to keep the repository clean:

git branch -d feature/user-login

The -d flag only deletes a branch that has been fully merged. Use -D to force-delete an unmerged branch.

Up next

Merging Branches

Sign in to track progress

Creating and Switching Branches โ€” Branching, Merging, and Resolving Conflicts โ€” Git and GitHub Complete Course: From Beginner to Advanced โ€” Script Valley โ€” Script Valley