STEM & Coding

Git for Beginners: Branches Without Fear

Git for Beginners: Branches Without Fear


🧭 What & Why

What is a branch?
A Git branch is a movable pointer to a line of commits. It lets you diverge from the main line, do work safely, and later bring your changes back without disturbing the main code. Branches in Git are extremely lightweight, which encourages frequent use. git-scm.com

Why use branches as a beginner?

  • Safety: Try ideas without breaking main.

  • Clarity: Keep features, fixes, and experiments separate.

  • Focus: Smaller pull requests and easier reviews.

  • Speed: Switch tasks without losing work.

Key idea: Keep branches short-lived. Merge (or discard) quickly to avoid painful conflicts and stale code. This matches modern DevOps practice and continuous integration. Atlassian


✅ Quick Start (copy-paste steps)

Try these in a demo folder. You’ll see how branching, committing, and merging feel in minutes.

# 1) Make a demo repo
mkdir ghf-branches-demo && cd ghf-branches-demo
git init
echo "Hello main" > app.txt
git add app.txt && git commit -m "init: main greeting"
# 2) Create and switch to a feature branch
git switch -c feat/wave-emoji
echo “👋 Hello world” >> app.txt
git add app.txt && git commit -m “feat: add wave emoji”# 3) Switch back and make a different change on main
git switch main
echo “Main line edit” >> app.txt
git add app.txt && git commit -m “chore: edit on main”

# 4) Merge the feature branch into main
git merge feat/wave-emoji # creates merge commit if histories diverged

# 5) Delete the merged branch (safe; history is in main)
git branch -d feat/wave-emoji

Notes for smooth sailing

  • git switch -c <name> creates and checks out a branch.

  • To set an upstream when pushing a new branch:
    git push -u origin <name> — your branch now “tracks” its remote, so future pull/push work without extra arguments. git-scm.com

  • git merge integrates changes from another branch into the current one; Git will do a fast-forward when possible, or a three-way merge if there’s divergence. git-scm.com+1


🗓️ 7-Day Branching Starter Plan

Goal: Build confidence with daily 15–25-minute reps.

Day 1 — Hello Branches

  • Create a repo, commit once on main, then git switch -c feat/first-try.

  • Make 2 small commits; merge back to main. Delete the feature branch.

Day 2 — Two Features in Parallel

  • Create feat/header and feat/footer.

  • Make 1–2 commits on each. Merge feat/header, then feat/footer. Resolve any trivial conflicts.

Day 3 — Tracking & Upstream

  • Add a remote (e.g., GitHub). Push main and a feature branch with -u to set upstream. Practice git pull and git push without extra args. git-scm.com

Day 4 — Fast-Forward vs Merge Commit

  • Create feat/tagline, commit once, and merge. Observe a fast-forward.

  • Create feat/logo, diverge main in parallel, then merge to see a merge commit. git-scm.com

Day 5 — Clean Personal History with Rebase (solo)

  • On your branch only, run git rebase main to place your commits on top of the latest main. Avoid rebasing shared branches. git-scm.com+1

Day 6 — Name Branches Clearly

  • Practice naming: feat/, fix/, chore/, docs/. Keep names short and descriptive, e.g., feat/signup-form.

Day 7 — Review & Prune

  • List branches (git branch --merged, --no-merged), delete locals you no longer need. Keep main green and up-to-date.


🛠️ Techniques & Frameworks

Merge vs Rebase (Beginner’s View)

  • Merge: Combines histories; keeps the true timeline with a merge commit when needed. Great for shared branches. Default choice for teams and beginners. git-scm.com

  • Rebase: Moves (replays) your commits, creating a cleaner, linear history. Best for your private branches before you open a pull request; avoid rebasing published/shared branches. git-scm.com+1

Practical rule of thumb

  • On a shared branch: merge.

  • On your personal feature branch: git fetchgit rebase origin/main → open PR.

Popular Branching Strategies

  • Trunk-Based Development (TBD): Commit small, frequent changes to main with short-lived branches; integrate early and often. Ideal for fast feedback and CI/CD. Atlassian

  • Gitflow: Uses multiple long-lived branches (develop, release, hotfix). Good for strict release trains but heavier for beginners and fast-moving teams. Atlassian

  • General Branching Guidelines: Choose a strategy intentionally; keep main deployable; use naming conventions; keep branches small; prefer early integration. Atlassian


🧠 Audience Variations

Students / Solo Beginners

  • Use TBD-style short branches; merge back daily.

  • Focus on: git switch, git add, commit, merge, push -u.

Professionals / Small Teams

  • Default to TBD with feature branches + PRs.

  • Require checks: automated tests and code review before merging.

Larger Teams

  • Start TBD; if release coordination is complex, selectively add Gitflow elements (e.g., a temporary release/* branch). Keep feature branches short to avoid painful merges. Atlassian

Seniors Mentoring Beginners

  • Pair frequently on merges and conflict resolution. Normalize small, tidy PRs and prune old branches weekly.


⚠️ Mistakes & Myths to Avoid

  • Myth: “Branches are heavy or risky.”
    Reality: Git makes branches extremely cheap; use them liberally. git-scm.com

  • Mistake: Rebasing shared branches.
    Fix: Only rebase your private branches; merge shared ones. git-scm.com+1

  • Mistake: Long-running branches.
    Fix: Keep branches small and integrate often (TBD mindset). Atlassian

  • Mistake: Not setting upstream.
    Fix: git push -u origin <branch> once; then git pull/git push are frictionless. git-scm.com


🗣️ Real-Life Examples & Scripts (copy-paste)

Create a feature branch, push, open PR

git switch -c feat/search-box
# ...edit files...
git add -A && git commit -m "feat: add search box UI"
git push -u origin feat/search-box
# open PR on your platform (GitHub/GitLab/Bitbucket)

Keep your feature branch up-to-date with main (solo rebase)

git fetch origin
git rebase origin/main
# resolve any conflicts, then continue
git rebase --continue
# push updated branch (force-with-lease protects others)
git push --force-with-lease

(Rebase adjusts your branch’s history for a cleaner PR; don’t do this to branches others pull from.) GitHub Docs

Merge a reviewed feature into main

git switch main
git pull
git merge --no-ff feat/search-box
git push
git branch -d feat/search-box

--no-ff forces a merge commit so the feature is clearly grouped in history (team preference). git-scm.com

Find and prune stale branches

# local, merged
git branch --merged main
# local, not merged
git branch --no-merged main
# delete local safely
git branch -d old/finished-thing
# delete remote
git push origin --delete old/finished-thing

Name branches clearly

feat/<thing> # new user-facing feature
fix/<thing> # bug fix
chore/<thing> # refactors, tooling, deps
docs/<thing> # documentation-only

🧰 Tools, Apps & Resources (quick pros/cons)

  • VS Code (built-in Git)

    • Pros: Ubiquitous, good UI for staging/hunks, easy branch switching.

    • Cons: Git concepts still matter—learn the CLI too.

  • GitHub Desktop / GitKraken / Sourcetree

    • Pros: Visual diffs, drag-and-drop staging, simple branch flows.

    • Cons: GUI can hide what’s happening; keep mental models clear.

  • Platform Docs (read when stuck)


📌 Key Takeaways

  • Branches are cheap and safe—use them to isolate work and learn fast. git-scm.com

  • Prefer short-lived branches; integrate early/often (TBD spirit). Atlassian

  • Merge shared branches; rebase only your private branches before PR. git-scm.com+2git-scm.com+2

  • Set an upstream once to streamline pull/push. git-scm.com

  • Practice the 7-Day plan to make these moves automatic.


❓ FAQs

1) What exactly happens when I create a branch?
Git makes a new pointer to a commit; no file copies are duplicated. That’s why branching is fast and cheap. git-scm.com

2) Should I use merge or rebase?
Merge for shared branches; rebase your private branch to tidy history before a PR. Avoid rebasing anything others may be using. git-scm.com+1

3) What’s a fast-forward merge?
When the target branch hasn’t diverged, Git can simply move the pointer forward without creating a merge commit—called a fast-forward. git-scm.com

4) What is “upstream” and why set it?
An upstream (tracking) branch lets git status, pull, and push know which remote branch your local branch corresponds to—less typing, fewer mistakes. git-scm.com

5) How should I name branches?
Use short, consistent prefixes like feat/, fix/, chore/, docs/ plus a brief slug, e.g., feat/signup-form. Teams can add issue IDs.

6) Do I need Gitflow?
Not as a beginner. Start with short-lived feature branches and frequent merges to main. Add Gitflow elements only if your release process demands it. Atlassian

7) How do I undo a merge mistake?
If it’s the latest commit and unpushed, git reset --hard HEAD~1. If pushed, prefer a new commit that reverts the merge (git revert -m 1 <merge-sha>), then fix forward.

8) What is a detached HEAD?
You’re pointing directly at a commit, not a branch pointer. Create a branch (git switch -c tmp/…) before committing if you want to keep the work.

9) How many branches is “too many”?
It’s not the number; it’s the age. Prune stale branches and keep work small and flowing back to main.

10) Can I practice safely without affecting my real project?
Yes—use a local demo repo (like the one above). When comfortable, apply the same steps to your real project.


📚 References

  1. Pro Git, Ch. 3: Branching — Branches in a Nutshell & Basic Branching and Merging. git-scm.com. git-scm.com+1

  2. git-branch — Git documentation. git-scm.com. git-scm.com

  3. git-merge — Git documentation. git-scm.com. git-scm.com

  4. git-rebase — Git documentation. git-scm.com. git-scm.com

  5. About Git rebase. GitHub Docs. GitHub Docs

  6. Trunk-Based Development. Atlassian. Atlassian

  7. A Guide to Optimal Branching Strategies in Git. Atlassian. Atlassian

  8. Git merge (tutorial). Atlassian. Atlassian

  9. Git rebase (tutorial). Atlassian. Atlassian