Branches — Branch · Merge · Rebase · Conflict · Stash
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. Create — git checkout -b feature/login (branch off from main)
2. Work & Commit — Edit code + git commit. No impact on main
3. Push — git 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?
4 Options for Resolving Conflicts
When the same line is modified differently → Git inserts conflict markers:
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:
> 💡 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:
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.
🤖 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.