Git Snippets
About Git
Git tracks changes, supports team workflows, and helps you recover safely from mistakes. These snippets cover daily work plus small project release habits.
Initial Git Setup
Configure your identity and sensible defaults once.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --global pull.rebase trueStart a Repository and First Commit
Initialize repository history and create a clean baseline commit.
git init
git add .
git commit -m "chore: initial project scaffold"Feature Branch Workflow
Always build features on isolated branches to keep main stable.
git switch -c feat/task-filter
# make changes
git add src/components/TaskList.jsx
git commit -m "feat: add task filter buttons"
git push -u origin feat/task-filterUseful Commit Message Pattern
Use conventional prefixes so history is searchable and release notes are easier.
feat: add pomodoro timer
fix: handle empty task submission
docs: add setup instructions
refactor: split task reducer logic
test: add TaskForm interaction testRebase Your Branch on Latest Main
Keep your branch current and avoid large merge commits in pull requests.
git fetch origin
git switch feat/task-filter
git rebase origin/mainResolve a Merge Conflict
After fixing conflict markers, continue the rebase cleanly.
git status
# edit conflicted files and remove <<<<<<< markers
git add path/to/file
git rebase --continueStash Work in Progress
Temporarily park local changes when you need to switch context fast.
git stash push -m "wip: timer styles"
git switch main
git stash list
git stash popMini Project: Release with Tags
For learning projects, practice simple releases. Tag versions so you can reference exact snapshots.
git switch main
git pull origin main
git tag -a v1.0.0 -m "First stable release"
git push origin v1.0.0Find Regressions with git bisect
Binary search your history to locate the commit that introduced a bug.
git bisect start
git bisect bad
git bisect good <known-good-commit>
# run tests at each step, then mark good/bad
git bisect resetUndo Safely Without Losing History
Prefer reversible commands over history-destroying operations.
git restore path/to/file
git restore --staged path/to/file
git revert <commit-hash>Daily Command Checklist
Simple routine for reliable local collaboration.
1. git switch main && git pull
2. git switch -c feat/your-change
3. git add -p and commit in small chunks
4. git fetch && git rebase origin/main
5. push branch and open pull request