Contents

Why branches matter

A branch is an isolated copy of the code. You build a new feature on your branch without touching the working code in main. If something goes wrong — just delete the branch. (Before your first commit, check your .gitignore — junk in the repository is hard to clean up later.)

Main branches

main (or master)

The primary branch. Always working, production-ready code. Never commit directly — only through merge or PR.

develop

The development branch. All completed features merge into it. When a release is ready — merge into main. Used by teams; for solo projects often unnecessary.

How to name branches

# New feature
feature/user-authentication
feature/dark-mode

# Bug fix
fix/login-redirect
fix/mobile-menu

# Urgent production hotfix
hotfix/payment-crash

# Refactoring
refactor/api-layer

Typical workflow

# 1. Sync with main
git checkout main
git pull origin main

# 2. Create a branch for the new feature
git checkout -b feature/new-navbar

# 3. Work, commit
git add .
git commit -m "feat: add responsive navbar"

# 4. Push the branch
git push -u origin feature/new-navbar

# 5. Open a Pull Request on GitHub
# 6. After review — merge into main, delete the branch

Merge vs Rebase

Merge — creates a merge commit, preserves full history. Good for public branches. (A deeper comparison with real-project examples is in rebase vs merge.)

git checkout main
git merge feature/new-navbar

Rebase — moves your feature commits on top of main, history stays linear. Good for local branches before pushing.

git checkout feature/new-navbar
git rebase main

⚠️ Never rebase public branches that others have pushed to — it rewrites history and creates conflicts for everyone.

Deleting branches

After merging it's better to delete branches — they clutter the list.

# Delete locally
git branch -d feature/new-navbar

# Delete on remote
git push origin --delete feature/new-navbar

💡 GitHub repo settings have an "Automatically delete head branches" option — branches are removed automatically after PR merge. All commands from this series on one page — in the git cheat sheet.

Share: