SyntaxStudy
Sign Up
Git Branching and Merging
Git Beginner 12 min read

Branching and Merging

Branches allow you to develop features in isolation from the main codebase. Git branching is lightweight and fast. The typical workflow is to create a branch for each feature or bug fix, then merge it back when complete.

Example
# Create and switch to a new branch
git branch feature/login     # create branch
git switch feature/login     # switch to it
git switch -c feature/login  # create AND switch (shortcut)

# List branches
git branch          # local branches
git branch -a       # local + remote branches

# Merge a branch
git switch main              # go back to main
git merge feature/login      # merge the feature branch

# Delete a branch
git branch -d feature/login  # safe delete (merged only)
git branch -D feature/login  # force delete

# Resolve merge conflicts
# 1. Git marks conflict in the file:
#    <<<<<<< HEAD
#    your changes
#    =======
#    incoming changes
#    >>>>>>> feature/login
# 2. Edit the file to resolve
# 3. git add the resolved file
# 4. git commit

# Rebase (alternative to merge — linear history)
git switch feature/login
git rebase main     # replay commits on top of main