← Back to Library Developer Concepts Intermediate 8 min read

What Is Git Branching? Workflows, Merging, and Conflict Resolution

Git branching allows developers to diverge from the main line of development to work on new features, bug fixes, or experiments in complete isolation without risking production stability.

💡 Plain-English Analogy

Imagine you are writing an essay and want to test a crazy new introduction. Instead of messing up your main draft, you photocopy the paper, write your experimental paragraphs on the copy, and if you like it, paste it back onto your main document. That is branching and merging.

⚙️ Architecture & Under the Hood

In Git, a branch is not a duplicated folder of files; it is simply a lightweight, movable pointer (a 41-byte text file in `.git/refs/heads/`) to a specific commit SHA. Switching branches takes milliseconds because Git merely updates the HEAD pointer and swaps files in the working tree to match the target commit tree.

Essential Branching Commands

Modern Git uses git switch and git branch to create and navigate branches cleanly.

main:     ──●──●──────────●──●── (Merged)
                ╲        ╱
feature:         ●──────● (feature/user-authentication)
# Create and switch to a new feature branch
git switch -c feature/user-authentication

# Make your changes and commit them
git commit -am "feat: add JWT auth endpoint"

# Switch back to the main branch
git switch main

# Merge the feature branch into main
git merge feature/user-authentication

# Delete the feature branch after merging
git branch -d feature/user-authentication

Frequently Asked Questions

What is the difference between git merge and git rebase?

git merge creates a new merge commit joining two histories together, preserving the exact historical timeline. git rebase rewires your branch's commits onto the tip of another branch, producing a clean, linear commit history.