Beginner20 min

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

Terminal
# Initialize a new repository
git init

# Clone an existing repository
git clone https://github.com/username/repo.git

Checking Status

Terminal
# See what's changed
git status

# See detailed changes
git diff

Saving Changes

Terminal
# 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

Terminal
# Push changes to GitHub
git push origin main

# Pull latest changes
git pull origin main

# Fetch without merging
git fetch origin

Viewing History

Terminal
# 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.

Terminal
# 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

Terminal
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:

Terminal
# 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

Terminal
type(scope): description

Types:
- feat: New feature
- fix: Bug fix
- docs: Documentation
- style: Formatting
- refactor: Code restructuring
- test: Adding tests
- chore: Maintenance

Examples

Terminal
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

Terminal
# 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

Terminal
# 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

Terminal
# Create a new commit that undoes a previous one
git revert abc1234

.gitignore for Next.js Projects

Never commit these files:

Terminal
# 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

Terminal
"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

  1. Go to github.com/new
  2. Name your repository
  3. Choose public or private
  4. Initialize with README (optional)
  5. Clone locally:
Terminal
git clone https://github.com/yourusername/your-repo.git
cd your-repo

Connecting Local to Remote

Terminal
# 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:

  1. Create a feature branch
  2. Make your changes
  3. Push the branch
  4. Open a Pull Request on GitHub
  5. Request review
  6. Merge after approval

Common Issues and Fixes

Merge Conflicts

When Git can't automatically merge changes:

Terminal
# 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

Terminal
# 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

Terminal
# 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:

  1. Initialize a repo: git init
  2. Create a file: echo "# My Project" > README.md
  3. Stage it: git add README.md
  4. Commit: git commit -m "docs: initial commit"
  5. Make a change: Edit the README
  6. Check status: git status
  7. View diff: git diff
  8. Commit: git commit -am "docs: update README"
  9. 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.

Mark this lesson as complete to track your progress