Master git basics and recognize dangerous operations before they happen.
What Is Git?
Git is a version control system that tracks changes to your code over time. Think of it like a detailed history of every change you've ever made, with the ability to go back to any point.
Full History
Every change is recorded and reversible
Branching
Work on features without affecting main code
Collaboration
Multiple people can work on the same project
Safety Net
Recover from mistakes (if you know how)
Core Concepts
Repository
A project folder tracked by git (contains a .git directory)
Example: git init creates a new repository
Commit
A snapshot of your code at a point in time
Example: Like saving a checkpoint in a game
Staging Area
Where you prepare changes before committing
Example: git add puts files in staging
Branch
An independent line of development
Example: main, feature/login, bugfix/auth
Remote
A copy of your repository on a server (like GitHub)
Example: origin is the default remote name
Essential Commands
Understanding .gitignore
The .gitignore file tells git which files to ignore. This is critical for security because you never want to commit:
.envContains API keys and secretsSecurity Risknode_modules/Dependencies (huge, regeneratable).venv/Python virtual environments*.logLog files with potentially sensitive datacredentials.jsonService account keysSecurity Risk.claude/Claude Code session dataWorking with Branches
main stable.Dangerous Operations
AI agents may propose these commands. Know what they do before approving them:
git reset --hardEffect: Destroys all uncommitted changes permanently
Alternative: git stash (saves changes for later)
git clean -fdEffect: Deletes all untracked files permanently
Alternative: Review with git clean -n first (dry run)
git push --forceEffect: Overwrites remote history, can delete teammates' work
Alternative: git push --force-with-lease (safer)
git checkout .Effect: Discards all uncommitted changes to tracked files
Alternative: git stash or commit first
git rebase mainEffect: Rewrites commit history (can cause conflicts)
Alternative: Only rebase unpushed commits
rm -rf .gitEffect: Destroys entire repository history forever
Alternative: There is no alternative. Never do this.
1. Run
git status to see current state2. Run
git stash to save uncommitted work3. Only then proceed with destructive commands
Recovery Tools
When things go wrong, these commands can help:
# Lost a commit after reset --hard? Find it!$ git reflog# Look for your commit hash, then:$ git checkout <hash># Or create a branch at that commit:$ git branch recovery <hash>
Best Practices with Agents
Commit early and often
Small commits are easier to revert. Agents work better with clear history.
Always run git status before destructive commands
Know what you're about to lose before you lose it.
Use git stash as your safety net
Stash uncommitted work before letting agents run risky operations.
Review agent-proposed git commands
Agents may suggest --force or reset --hard. Always understand before approving.
Push your work frequently
Remote backups protect against local disasters.
Try It Now
1# Check your repository status2$ git status34# View recent commits5$ git log --oneline -567# See what's in your .gitignore8$ cat .gitignore910# Practice stashing (safe to try!)11$ echo "test" > temp.txt12$ git stash13$ git stash pop14$ rm temp.txt