Git Essentials: Stop Breaking the Build
Git is the most important tool you use. You use it every day.
Yet, most developers are terrified of it.
They memorize 3 commands (add, commit, push) and pray nothing goes wrong.
When things go wrong, they delete the folder and re-clone. We can do better.
Here is what we'll cover:
- Atomic Commits: The golden rule of history.
- Merge vs. Rebase: The religious war, settled.
- The "Safety Net": How to undo anything (
reflog). - Squashing: Keeping the history clean.
1. Atomic Commits
Bad Commit:
Commit: "Fix stuff"
- Contains: A bug fix, a new feature, and some CSS changes.
- Why it's bad: If the bug fix is wrong, I have to revert the CSS changes too.
Atomic Commit: One logical change per commit.
fix(auth): handle null user tokenfeat(ui): add new button stylerefactor(api): clean up routes
If I need to revert the auth fix, I can rely on a clean history.
2. Merge vs. Rebase
- Merge: Preserves history exactly as it happened.
- Pros: Honest.
- Cons: Creates "Spaghetti" history with messy "Merge branch 'master'" commits.
- Rebase: Rewrites history to look linear.
- Pros: Clean, straight line. Easy to debug.
- Cons: Dangerous if you rebase shared branches.
The Professional Workflow:
- Rebase locally. (
git pull --rebase origin main). Keep your branch up to date. - Squash and Merge into main.
This gives you a linear history on Main, which makes git bisect (debugging) much easier.
3. The "Oh No" Button: Reflog
You accidentally deleted a branch? You reset to the wrong commit? Don't panic. Git never deletes anything immediately.
git reflog
This logs every move you made. Even the ones you undid.
Find the hash before you messed up, and git reset --hard <hash>.
You are saved.
4. Writing Good Commit Messages
Use the Conventional Commits standard.
type(scope): message
feat: A new feature.fix: A bug fix.docs: Documentation only.chore: Build process, deps.
Why? You can automate your Changelog. Tools can read these commits and generate: "Version 1.2.0: Added 3 features, Fixed 2 bugs."
Summary
- Commit small. (Atomic).
- Rebase often. (Keep it linear).
- Learn Reflog. (Safety net).
- Write meaningful messages. (Be kind to future you).
