Vibe coding with AI course Β· Module 2: Next.js Setup
Git and GitHub - Version Control for Pirates
In this lesson14
Every programmer needs to know Git - a version control system. It's like a ship's log - you record the history of changes in your code.
What is Git?
Git is a version control system - it allows you to:
- Save the history of code changes
- Go back to previous versions
- Collaborate with other developers
- Branching - work on different features simultaneously
- Track who changed what and when
Git vs GitHub:
- Git - local tool (on your computer)
- GitHub - online platform for storing code (like Dropbox for developers)
Installing Git
Windows:
Download from: https://git-scm.com/install/windows
During installation choose:
- Git Bash
- Git from the command line
- Use Vim (or Nano if you prefer)
Mac:
Check whether you already have Git:
git --versionIf it's missing, macOS offers to install the Xcode Command Line Tools (Git is part of them). You can also type xcode-select --install yourself or install Git via Homebrew:
brew install gitLinux (Ubuntu/Debian):
sudo apt-get update
sudo apt-get install gitConfiguring Git - First Steps
After installation, configure Git with your details:
git config --global user.name "Jack Sparrow"
git config --global user.email "jack@blackpearl.com"Why is this important? Every commit (saved change) will have your name and email - it's like a signature in the ship's log.
Check configuration:
git config --listInitializing a Git Project
You have two scenarios:
Scenario 1: New project (local)
When you create a new Next.js project, Git is already initialized automatically!
create-next-app does it for you. Check:
ls -laYou'll see a .git/ folder - that's the Git repository.
Scenario 2: Existing folder (without Git)
If you have a folder without Git:
cd my-project
git initThis will create a .git/ folder and initialize the repository.
Basic Git Commands
1. git status - Repository state
Checks what has changed:
git statusYou'll see:
- Untracked files - new files that Git doesn't track
- Modified files - changed files
- Staged files - files ready for commit
On branch main
Changes not staged for commit:
modified: app/page.tsx
Untracked files:
app/pirates/page.tsx2. git add - Adding files to staging
Staging area is a "waiting room" before commit.
# Add one file
git add app/page.tsx
# Add all files
git add .
# Add all .tsx files, also from subfolders (quotes: Git expands the pattern, not the terminal)
git add "*.tsx"After git add, files are staged - ready to be saved (committed).
3. git commit - Saving changes
A commit is like a checkpoint in a game - you save the state of the code.
git commit -m "Add pirate landing page"-m is message - a short description of what you changed.
Good commit messages:
- "Add hero section to landing page"
- "Fix button hover animation"
- "Update Pirate interface with level field"
Bad commit messages:
- "changes"
- "fix"
- "asdfghjk"
- "WIP" (Work In Progress - only temporarily)
4. git log - Commit history
See the history of all commits:
git logYou'll see:
commit a3f5b8c (HEAD -> main)
Author: Jack Sparrow <jack@blackpearl.com>
Date: Sun Nov 24 10:30:00 2024 +0100
Add pirate landing page
commit b2e4a9d
Author: Jack Sparrow <jack@blackpearl.com>
Date: Sun Nov 24 09:15:00 2024 +0100
Initial commitShortcuts:
# Short version (1 line per commit)
git log --oneline
# Last 5 commits
git log -5
# With branch graph
git log --graph --oneline5. git diff - See changes
Compares current files with staged/committed version:
# Changes not added to stage
git diff
# Changes in staged files
git diff --stagedYou'll see:
diff --git a/app/page.tsx b/app/page.tsx
--- a/app/page.tsx
+++ b/app/page.tsx
@@ -1,5 +1,5 @@
export default function Home() {
return (
- <h1>Hello World</h1>
+ <h1>Ahoy, Pirates!</h1>
)
}.gitignore - Ignoring Files
Some files should NOT be in Git:
node_modules/- huge folder (hundreds of MB).env.local- secrets (API keys, passwords).next/- build outputdist/,build/- compiled files
The .gitignore file tells Git what to ignore:
# .gitignore
node_modules/
.next/
.env.local
.env
.DS_Store
*.logNext.js automatically creates .gitignore - you already have this configured!
Branching - Work Branches
A branch is a separate line of project development. You can work on a new feature without affecting the main code.
Creating a branch
# Create a new branch
git branch feature/landing-page
# Switch to the new branch
git checkout feature/landing-page
# Or one command (create + switch)
git checkout -b feature/landing-pageNaming convention:
feature/name- new featurefix/name- bug fixrefactor/name- refactoringdocs/name- documentation
Switching between branches
# See all branches
git branch
# Switch to main
git checkout main
# Switch to feature branch
git checkout feature/landing-pageMerging branches
When you finish a feature, you merge changes into main:
# Switch to main
git checkout main
# Merge changes from feature branch
git merge feature/landing-pageFast-forward merge:
If main hasn't changed, Git simply moves the pointer - this is the easiest merge.
3-way merge: If both branches have new commits, Git creates a new merge commit.
Deleting a branch
After merging you can delete the branch:
git branch -d feature/landing-pageGitHub - Remote Repository
GitHub is like a pirate island - a place where you store your treasures (code).
Creating a Repository on GitHub
- Go to: https://github.com
- Click the green "New" button (or "New repository")
- Fill in:
- Repository name:
krakens-call - Description: "Epic pirate game built with Next.js"
- Public or Private (your choice)
- DO NOT check "Initialize with README" (you already have a local project)
- Repository name:
- Click "Create repository"
Connecting Local Project with GitHub
GitHub will give you commands - copy them:
# Add remote repository
git remote add origin https://github.com/username/krakens-call.git
# Rename branch to main (if needed)
git branch -M main
# Push code to GitHub
git push -u origin mainExplanation:
origin- name of the remote repository (default convention)-u- set upstream (default branch for push/pull)main- branch name
git push - Sending changes
Every local commit β send to GitHub:
# Push all commits to origin/main
git push
# Push a specific branch
git push origin feature/landing-pagegit pull - Downloading changes
If you work in a team, others may introduce changes:
# Download changes from GitHub and merge into local branch
git pull
# Pull from a specific branch
git pull origin mainpull = fetch + merge:
git fetch- downloads changesgit merge- merges them into your branch
git clone - Cloning a repository
Downloading an existing project from GitHub:
git clone https://github.com/username/krakens-call.git
cd krakens-call
npm install
npm run devWorkflow with Git and GitHub
Typical pirate workflow:
1. New feature
# Create branch
git checkout -b feature/pirate-profile
# Work on code...
# Edit app/profile/page.tsx
# Check changes
git status
git diff
# Add to stage
git add app/profile/
# Commit
git commit -m "Add pirate profile page with stats"
# Push to GitHub
git push origin feature/pirate-profile2. Pull Request on GitHub
- Go to GitHub
- You'll see a banner: "feature/pirate-profile had recent pushes"
- Click "Compare & pull request"
- Fill in:
- Title: "Add pirate profile page"
- Description: What you did, why, screenshots
- Click "Create pull request"
Pull Request (PR) is a request to merge changes from your branch into main. The team can do a code review.
3. Review and Merge
- Other developers review the code
- They leave comments
- You make fixes (new commits on the same branch)
- When everything's OK, someone clicks "Merge pull request"
- The branch gets merged into
main
4. Sync local main
# Switch to main
git checkout main
# Download changes from GitHub
git pull
# Delete local feature branch
git branch -d feature/pirate-profileUndoing Changes - When Something Goes Wrong
Undo changes in a file (not committed)
# Restore file to last commit
git checkout -- app/page.tsx
# Restore all files
git checkout -- .Remove files from stage (git add)
# Remove from stage, keep changes in file
git reset app/page.tsx
# Remove everything from stage
git resetUndo last commit (locally)
# Undo commit, keep changes in files
git reset --soft HEAD~1
# Undo commit, delete changes in files (DANGER!)
git reset --hard HEAD~1Warning: --hard DELETES changes with no way to recover them!
Revert commit (already pushed)
If the commit is already on GitHub:
# Create a new commit that reverses the changes
git revert a3f5b8c
# Push
git pushrevert creates a new commit - safer than reset.
Git in Cursor
Cursor has built-in Git support!
Source Control panel
- Click the Source Control icon (on the left, 3 branching circles)
- You'll see:
- Changed files
- + button (git add)
- Field for commit message
- Commit button
Workflow in Cursor:
- Edit files
- Open Source Control (Ctrl + Shift + G, on a Mac also Control)
- Click + next to files (stage)
- Type commit message
- Click Commit
- Click "..." β Push
AI Commit Messages: Cursor AI can suggest commit messages based on your changes!
Using AI for Git
Ask Cursor AI:
Prompt 1:
How to undo the last commit in Git?Prompt 2:
Write a good commit message for landing page changes - I added a hero section and cta buttonPrompt 3:
How to resolve a merge conflict in app/page.tsx?Git Best Practices
Commit often - small, frequent commits > rare, large ones
Good commit messages - explain WHAT and WHY
Branch per feature - one feature = one branch
Pull before push - always git pull before git push
Don't commit secrets - .env in .gitignore
Code review - use Pull Requests
Main always works - don't merge broken code into main
Don't commit node_modules/ - huge, unnecessary Don't push --force on main (unless you absolutely must) Don't commit WIP without description - describe what's in progress
Summary
Git - version control system, the ship's log of code GitHub - platform for storing code online git init - repository initialization git add - adding files to stage git commit - saving changes (snapshot) git push - sending commits to GitHub git pull - downloading changes from GitHub git branch - creating branches git merge - merging changes between branches Pull Request - request for review and merge .gitignore - files ignored by Git Cursor Source Control - GUI for Git in the editor
Next steps: In the following modules we'll use Git to:
- Save project progress
- Create feature branches
- Deploy to Vercel (Vercel connects with GitHub!)
See you there!
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. What does the command 'git commit -m "..."' do?
2. What is the difference between Git and GitHub?
Hands-on tasks in the game
- Vertical ordering
Order the steps to save changes and send them to GitHub:
- Horizontal ordering
Arrange the command that saves changes with a message: