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.
Git is a version control system - it allows you to:
Git vs GitHub:
Download from: https://git-scm.com/download/win
During installation choose:
Git is already installed! Check:
1git --versionIf not present, install via Homebrew:
1brew install git1sudo apt-get update
2sudo apt-get install gitAfter installation, configure Git with your details:
1git config --global user.name "Jack Sparrow"
2git 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:
1git config --listYou have two scenarios:
When you create a new Next.js project, Git is already initialized automatically!
create-next-app does it for you. Check:1ls -laYou'll see a
.git/ folder - that's the Git repository.If you have a folder without Git:
1cd my-project
2git initThis will create a
.git/ folder and initialize the repository.Checks what has changed:
1git statusYou'll see:
1On branch main
2Changes not staged for commit:
3 modified: app/page.tsx
4
5Untracked files:
6 app/pirates/page.tsxStaging area is a "waiting room" before commit.
1# Add one file
2git add app/page.tsx
3
4# Add all files
5git add .
6
7# Add all .tsx files
8git add *.tsxAfter
git add, files are staged - ready to be saved (committed).A commit is like a checkpoint in a game - you save the state of the code.
1git commit -m "Add pirate landing page"-m is message - a short description of what you changed.
Good commit messages:
Bad commit messages:
See the history of all commits:
1git logYou'll see:
1commit a3f5b8c (HEAD -> main)
2Author: Jack Sparrow <jack@blackpearl.com>
3Date: Mon Nov 24 10:30:00 2024 +0100
4
5 Add pirate landing page
6
7commit b2e4a9d
8Author: Jack Sparrow <jack@blackpearl.com>
9Date: Mon Nov 24 09:15:00 2024 +0100
10
11 Initial commitShortcuts:
1# Short version (1 line per commit)
2git log --oneline
3
4# Last 5 commits
5git log -5
6
7# With branch graph
8git log --graph --onelineCompares current files with staged/committed version:
1# Changes not added to stage
2git diff
3
4# Changes in staged files
5git diff --stagedYou'll see:
1diff --git a/app/page.tsx b/app/page.tsx
2--- a/app/page.tsx
3+++ b/app/page.tsx
4@@ -1,5 +1,5 @@
5 export default function Home() {
6 return (
7- <h1>Hello World</h1>
8+ <h1>Ahoy, Pirates!</h1>
9 )
10 }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 filesThe
.gitignore file tells Git what to ignore:1# .gitignore
2node_modules/
3.next/
4.env.local
5.env
6.DS_Store
7*.logNext.js automatically creates
- you already have this configured!.gitignore
A branch is a separate line of project development. You can work on a new feature without affecting the main code.
1# Create a new branch
2git branch feature/landing-page
3
4# Switch to the new branch
5git checkout feature/landing-page
6
7# Or one command (create + switch)
8git checkout -b feature/landing-pageNaming convention:
feature/name - new featurefix/name - bug fixrefactor/name - refactoringdocs/name - documentation1# See all branches
2git branch
3
4# Switch to main
5git checkout main
6
7# Switch to feature branch
8git checkout feature/landing-pageWhen you finish a feature, you merge changes into
main:1# Switch to main
2git checkout main
3
4# Merge changes from feature branch
5git 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.
After merging you can delete the branch:
1git branch -d feature/landing-pageGitHub is like a pirate island - a place where you store your treasures (code).
krakens-callGitHub will give you commands - copy them:
1# Add remote repository
2git remote add origin https://github.com/username/krakens-call.git
3
4# Rename branch to main (if needed)
5git branch -M main
6
7# Push code to GitHub
8git push -u origin mainExplanation:
origin - name of the remote repository (default convention)-u - set upstream (default branch for push/pull)main - branch nameEvery local commit → send to GitHub:
1# Push all commits to origin/main
2git push
3
4# Push a specific branch
5git push origin feature/landing-pageIf you work in a team, others may introduce changes:
1# Download changes from GitHub and merge into local branch
2git pull
3
4# Pull from a specific branch
5git pull origin mainpull = fetch + merge:
git fetch - downloads changesgit merge - merges them into your branchDownloading an existing project from GitHub:
1git clone https://github.com/username/krakens-call.git
2cd krakens-call
3npm install
4npm run devTypical pirate workflow:
1# Create branch
2git checkout -b feature/pirate-profile
3
4# Work on code...
5# Edit app/profile/page.tsx
6
7# Check changes
8git status
9git diff
10
11# Add to stage
12git add app/profile/
13
14# Commit
15git commit -m "Add pirate profile page with stats"
16
17# Push to GitHub
18git push origin feature/pirate-profilePull Request (PR) is a request to merge changes from your branch into
main. The team can do a code review.main1# Switch to main
2git checkout main
3
4# Download changes from GitHub
5git pull
6
7# Delete local feature branch
8git branch -d feature/pirate-profile1# Restore file to last commit
2git checkout -- app/page.tsx
3
4# Restore all files
5git checkout -- .1# Remove from stage, keep changes in file
2git reset app/page.tsx
3
4# Remove everything from stage
5git reset1# Undo commit, keep changes in files
2git reset --soft HEAD~1
3
4# Undo commit, delete changes in files (DANGER!)
5git reset --hard HEAD~1Warning:
--hard DELETES changes with no way to recover them!If the commit is already on GitHub:
1# Create a new commit that reverses the changes
2git revert a3f5b8c
3
4# Push
5git pushrevert creates a new commit - safer than reset.Cursor has built-in Git support!
AI Commit Messages: Cursor AI can suggest commit messages based on your changes!
Ask Cursor AI:
Prompt 1:
1How to undo the last commit in Git?Prompt 2:
1Write a good commit message for landing page changes - I added a hero section and cta buttonPrompt 3:
1How to resolve a merge conflict in app/page.tsx?✅ 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
✅ 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:
See you there! 🚀