Git is the most important tool for every developer. Here is everything you need to know.
Basics¶
git init
git clone URL
git add .
git commit -m “message”
git push origin main
Branches¶
git branch feature/login
git checkout feature/login
or: git checkout -b feature/login¶
git merge feature/login
git branch -d feature/login
Merge vs Rebase¶
Merge creates a merge commit and preserves history. Rebase rewrites history to be linear. Rule: rebase local branches, merge shared ones.
Remote¶
git remote add origin URL
git fetch origin
git pull origin main
git push origin feature/login
Stash¶
git stash push -m “WIP”
git stash list
git stash pop
Tags¶
git tag -a v1.0.0 -m “Release 1.0”
git push origin –tags
Git Flow vs Trunk-based¶
- Git Flow: main, develop, feature/, release/, hotfix/ — for release-based projects
- Trunk-based: main + short-lived feature branches — for CI/CD, recommended
.gitignore¶
node_modules/
.env
*.log
dist/
.DS_Store
Git Hooks¶
.husky/pre-commit¶
npm run lint
npm run test
Useful Aliases¶
[alias]
co = checkout
br = branch
st = status
lg = log –oneline –graph –all
Mastery¶
You learn Git by practice. Don’t be afraid to experiment — reflog will always save you.