- Learn
- Foundations
- Version Control Basics
Essential Git commands and workflows for AI development. Learn to track changes, undo mistakes, and collaborate effectively.
Version Control Basics
Version control is essential for any software project. Git tracks every change you make, lets you undo mistakes, and enables collaboration. Most AI tools integrate with Git and GitHub, making version control knowledge even more important.
Why Git Matters for AI Development
- Track all changes: See exactly what AI modified
- Undo mistakes: Revert bad AI suggestions easily
- Compare versions: Review before and after AI changes
- Collaborate: Share work with others
- Required: GitHub Copilot, Claude Code, and many tools need Git
Essential Commands
Starting a Project
# Initialize a new repository
git init
# Clone an existing repository
git clone https://github.com/username/repo.git
Checking Status
# See what's changed
git status
# See detailed changes
git diff
Saving Changes
# Stage files for commit
git add . # All files
git add filename.tsx # Specific file
git add src/components/ # Entire directory
# Commit with a message
git commit -m "Add login form component"
Working with Remote
# Push changes to GitHub
git push origin main
# Pull latest changes
git pull origin main
# Fetch without merging
git fetch origin
Viewing History
# View commit history
git log --oneline
# View who changed what
git blame filename.tsx
Working with Branches
Branches let you work on features without affecting the main code.
# Create and switch to a new branch
git checkout -b feature/new-feature
# Switch to an existing branch
git checkout main
# List all branches
git branch
# Merge a branch into main
git checkout main
git merge feature/new-feature
# Delete a branch after merging
git branch -d feature/new-feature
The Git Workflow for AI Development
1. git pull # Get latest code
2. Make changes (with AI) # Edit files
3. git diff # Review AI changes
4. git add . # Stage changes
5. git commit -m "message" # Commit
6. git push # Push to remote
Reviewing AI Changes
Before committing AI-generated code, always review:
# See what changed
git diff
# See which files changed
git status
# See changes in a specific file
git diff src/components/LoginForm.tsx
Commit Message Convention
Good commit messages help you understand your project history.
Format
type(scope): description
Types:
- feat: New feature
- fix: Bug fix
- docs: Documentation
- style: Formatting
- refactor: Code restructuring
- test: Adding tests
- chore: Maintenance
Examples
git commit -m "feat(auth): add password reset flow"
git commit -m "fix(api): handle null response from user service"
git commit -m "docs(readme): update installation steps"
git commit -m "refactor(components): extract Button into shared module"
Undoing Changes
Git makes it easy to undo mistakes—crucial when AI generates unexpected code.
Undo Uncommitted Changes
# Discard changes in a specific file
git checkout -- filename.tsx
# Discard all uncommitted changes (careful!)
git checkout -- .
# Unstage a file (keep changes)
git reset HEAD filename.tsx
Undo Commits
# Undo last commit, keep changes staged
git reset --soft HEAD~1
# Undo last commit, keep changes unstaged
git reset HEAD~1
# Undo last commit completely (careful!)
git reset --hard HEAD~1
Revert a Specific Commit
# Create a new commit that undoes a previous one
git revert abc1234
.gitignore for Next.js Projects
Never commit these files:
# Dependencies
node_modules/
.pnpm-store/
# Build
.next/
out/
build/
# Environment variables (IMPORTANT!)
.env
.env.local
.env*.local
# IDE
.vscode/
.idea/
.cursor/
# OS
.DS_Store
Thumbs.db
# Debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# TypeScript
*.tsbuildinfo
# Testing
coverage/
AI + Git Integration
Many AI tools can help with Git:
Claude Code
"Commit these changes with a descriptive message"
"Create a new branch for the authentication feature"
"Show me the recent commits"
Cursor
- Auto-generates commit messages
- Shows diff preview
- Git integration in sidebar
Best Practices
- Review AI commits: Never let AI commit without reviewing
- Keep commits focused: One feature or fix per commit
- Don't commit secrets: Use .gitignore and .env files
- Pull before pushing: Avoid merge conflicts
Working with GitHub
Creating a Repository
- Go to github.com/new
- Name your repository
- Choose public or private
- Initialize with README (optional)
- Clone locally:
git clone https://github.com/yourusername/your-repo.git
cd your-repo
Connecting Local to Remote
# Add remote to existing project
git remote add origin https://github.com/yourusername/your-repo.git
# Push and set upstream
git push -u origin main
Pull Requests
For team projects or open source contributions:
- Create a feature branch
- Make your changes
- Push the branch
- Open a Pull Request on GitHub
- Request review
- Merge after approval
Common Issues and Fixes
Merge Conflicts
When Git can't automatically merge changes:
# After a conflict, you'll see
<<<<<<< HEAD
your changes
=======
their changes
>>>>>>> branch-name
# Edit the file to resolve
# Then mark as resolved
git add filename.tsx
git commit -m "Resolve merge conflict"
Forgot to Pull First
# If you have local commits and remote has new ones
git pull --rebase origin main
# Or create a merge commit
git pull origin main
Committed to Wrong Branch
# Move commit to new branch
git branch new-branch
git reset --hard HEAD~1
git checkout new-branch
Exercise
Set up a new Git repository and practice:
- Initialize a repo:
git init - Create a file:
echo "# My Project" > README.md - Stage it:
git add README.md - Commit:
git commit -m "docs: initial commit" - Make a change: Edit the README
- Check status:
git status - View diff:
git diff - Commit:
git commit -am "docs: update README" - View history:
git log --oneline
Summary
- git status/diff: See what's changed
- git add/commit: Save changes
- git push/pull: Sync with remote
- git checkout -b: Create branches
- git reset: Undo mistakes
- Always review AI changes before committing
Next Steps
Now you understand version control. Let's set up your complete development environment with all the tools you need.