Back
5/65
Lesson 5
10 min
Git Essentials
Version control and recognizing dangerous operations
Ready to level up?
Mark complete to track your learning progress.
Version control and recognizing dangerous operations
Mark complete to track your learning progress.
Master git basics and recognize dangerous operations before they happen.
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.
Every change is recorded and reversible
Work on features without affecting main code
Multiple people can work on the same project
Recover from mistakes (if you know how)
A project folder tracked by git (contains a .git directory)
Example: git init creates a new repository
A snapshot of your code at a point in time
Example: Like saving a checkpoint in a game
Where you prepare changes before committing
Example: git add puts files in staging
An independent line of development
Example: main, feature/login, bugfix/auth
A copy of your repository on a server (like GitHub)
Example: origin is the default remote name
Working Dir
Staged
Committed
Step 1/6: Start: main branch with initial commits
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 datamain stable.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.
git status to see current stategit stash to save uncommitted workWhen 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>
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.
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