C
Collaboration & Git/Branches/Lesson 03

Branches — Branch · Merge · Rebase · Conflict · Stash

45 min·theory

Branches — Branch · Merge · Rebase · Conflict · Stash

🎯 After reading this lesson

Once you finish this lesson, you will be able to confidently do the following three things.

  • ✅ Git Flow vs Trunk-based Development
  • ✅ Difference between rebase and merge + when to use each
  • ✅ Squash merge + commit cleanup

Keep the learning objectives as a checklist and close the lesson once you can answer all of them.

6-Step Branch Workflow

1. Creategit checkout -b feature/login (branch off from main)
2. Work & Commit — Edit code + git commit. No impact on main
3. Pushgit push -u origin feature/login — creates a tracking branch on the remote
4. Open PR — Create a Pull Request on GitHub. Request a peer review
5. Approve → Merge — Review passes + CI passes → merge into main
6. Delete Branch — No longer needed after merging. git branch -d or GitHub UI

> 💡 A branch is a parallel universe. main holds production code, feature holds experimental code. If the experiment fails, just discard the branch.

Merge vs Rebase — Two Ways to Integrate

Merge — Combine into main with git merge feature

  • Result: A new Merge Commit is created. The branch history is preserved
  • Pros: Safe. Original commits are preserved
  • Cons: Complex history (diamond shape)

Rebase — Move feature on top of main with git rebase main

  • Result: Linear history. Clean
  • Pros: Better readability. Single-line timeline
  • Cons: Commit hashes are regenerated. Dangerous for pushed branches

When to use which?

SituationChoice
Shared branchMerge (safe)
Local feature branchRebase (clean up)
Integrating into mainFollow team policy
After pushingNever Rebase

4 Options for Resolving Conflicts

When the same line is modified differently → Git inserts conflict markers:

code
<<<<<<< HEAD
my change
=======
incoming change
>>>>>>> feature

Resolution options:
1. Accept Current — Keep only the top code; delete markers and bottom
2. Accept Incoming — Keep only the bottom code; delete markers and top
3. Accept Both — Keep both sides; remove only the markers
4. Rewrite — Ignore both and write fresh (most common when neither is sufficient)

After resolving:

bash
git add <conflicted file>
git commit                    # merge message is auto-generated
# or to abort:
git merge --abort             # revert to the previous state

> 💡 IDE tip: VS Code and IntelliJ have built-in conflict resolution GUIs. The 'Accept Both' button is the safest option.

Git Stash — A Temporary Work Drawer

Scenario: You're working on feature/login when a production bug hits — it needs to be fixed now

Solution:

bash
git stash push -m "WIP login"    # 1. Clean the working tree + save to drawer
git checkout main                # 2. Switch to main
git checkout -b hotfix/critical  # 3. Branch hotfix + fix + merge + deploy
git checkout feature/login       # 4. Return to original branch
git stash pop                    # 5. Restore from drawer back to working tree

Other commands:

  • git stash list — View drawer contents
  • git stash show -p — Preview changes
  • git stash drop — Empty the drawer (irreversible)
  • git stash apply — Same as pop but leaves the entry in the drawer

> ⚠️ If stashes pile up, they get forgotten. It is recommended to pop immediately after use or clean up with a commit.

💻 📌 Branch Command Cheatsheet
# === Branch Creation·Movement ===
git branch                          # Local branch list
git branch -a                       # All, including remote
git checkout -b feature/x           # Create new branch + move
git switch -c feature/x             # Same (modern command)
git checkout main                   # Move only
git branch -d feature/x             # Delete branch (only merged ones)
git branch -D feature/x             # Force delete

# === Merge ===
git merge feature/x                 # Merge feature into main (create merge commit)
git merge --no-ff feature/x         # Always create merge commit
git merge --abort                   # Abort merge on conflict

# === Rebase ===
git rebase main                     # Rebase feature onto main (linear)
git rebase --continue               # Continue after resolving conflict
git rebase --abort                  # Revert to original state
git rebase -i HEAD~3                # Interactive rebase last 3 commits

# === Stash (Temporary Save) ===
git stash push -m "WIP"             # Store changes in drawer
git stash list                      # Drawer list
git stash pop                       # Most recent drawer → working tree (remove from drawer)
git stash apply stash@{1}           # Apply specific drawer (keep in drawer)
git stash drop stash@{0}            # Delete drawer

# === Remote Branch ===
git push -u origin feature/x        # First push, set up tracking
git push origin --delete feature/x  # Delete remote branch
git fetch --prune                   # Clean up vanished remote branches

🤖 Try Asking AI Like This

Knowing the concepts in this lesson lets you give AI specific instructions. Instead of a vague 'fix this,' you make requests with vocabulary — that is where token savings begin.

  • "Walk me through the safe git revert procedure to merge this hotfix into main"
  • "Clean up the commits on this feature branch using squash + rebase"

Why This Cuts Tokens

When you don't know the concepts, even after getting an AI response you end up asking "What does that mean?" again. Those follow-up questions are what burn tokens. Learn the concepts once and the conversation ends in one round.

Branches — Branch · Merge · Rebase · Conflict · Stash - Collaboration & Git