Lesson 31 of 49 beginner

Git & Version Control

A Time Machine for Your Code

Open interactive version (quiz + challenge)

Real-world analogy

Imagine writing a novel without Ctrl+Z. Every change is permanent — delete a chapter and it's gone forever. Git is like having an infinite undo history for your entire project. You can save snapshots (commits), create alternate storylines (branches), and even combine different versions (merge). If chapter 5 was better yesterday, just go back in time!

What is it?

Git is a distributed version control system that tracks changes to your code over time. It lets you save snapshots (commits), work on parallel versions (branches), collaborate with teams (push/pull), and review changes (pull requests). GitHub is the most popular platform for hosting Git repositories.

Real-world relevance

Every software company uses Git. Open source projects like React, NestJS, and Linux are built by thousands of developers collaborating through Git. Your GitHub profile is effectively your developer resume — employers look at your commits, contributions, and code quality.

Key points

Code example

# Initialize a new project
git init
git add .
git commit -m "Initial commit"

# Connect to GitHub
git remote add origin https://github.com/you/your-repo.git
git push -u origin main

# Daily workflow
git status                    # What changed?
git add src/user.service.ts   # Stage specific file
git commit -m "Add user creation endpoint"
git push                      # Upload to GitHub

# Branching workflow
git branch feature-auth       # Create branch
git checkout feature-auth     # Switch to it
# ... make changes ...
git add .
git commit -m "Add JWT authentication"
git push -u origin feature-auth  # Push branch
# -> Open Pull Request on GitHub
# -> Team reviews and approves
# -> Merge into main

# Useful commands
git log --oneline             # See commit history
git diff                      # See uncommitted changes
git stash                     # Temporarily save changes
git stash pop                 # Restore stashed changes
git checkout -- file.ts       # Discard changes to a file
git reset HEAD~1              # Undo last commit (keep changes)

# .gitignore
node_modules/
dist/
.env
.env.local
*.log
.DS_Store
coverage/

Line-by-line walkthrough

  1. 1. Initialize a new project
  2. 2. Git command
  3. 3. Git command
  4. 4. Git command
  5. 5.
  6. 6. Connect to GitHub
  7. 7. Git command
  8. 8. Git command
  9. 9.
  10. 10. Daily workflow
  11. 11. Git command
  12. 12. Git command
  13. 13. Git command
  14. 14. Git command
  15. 15.
  16. 16. Branching workflow
  17. 17. Git command
  18. 18. Git command
  19. 19. ... make changes ...
  20. 20. Git command
  21. 21. Git command
  22. 22. Git command
  23. 23. -> Open Pull Request on GitHub
  24. 24. -> Team reviews and approves
  25. 25. -> Merge into main
  26. 26.
  27. 27. Useful commands
  28. 28. Git command
  29. 29. Git command
  30. 30. Git command
  31. 31. Git command
  32. 32. Git command
  33. 33. Git command
  34. 34.
  35. 35. .gitignore
  36. 36.
  37. 37.
  38. 38. Method chaining on the previous expression
  39. 39. Method chaining on the previous expression
  40. 40. .log
  41. 41. Method chaining on the previous expression
  42. 42.

Spot the bug

git add .
git commit -m "initial commit"
git push origin main
Need a hint?
What files might be included when you use 'git add .'?
Show answer
Without a .gitignore, 'git add .' commits EVERYTHING including .env secrets and node_modules. Fix: create .gitignore with node_modules/, .env, dist/ BEFORE git add.

Explain like I'm 5

Git is like a time machine for your homework. Every time you finish a page, you take a photo (commit). If you mess up page 3, go back to page 2's photo! You can also make a copy to try something wild (branch), and if it works, put it back (merge).

Fun fact

Linus Torvalds (creator of Linux) built Git in just 10 days in 2005 because he was frustrated with existing version control tools. He named it 'git' — British slang for an annoying person — saying 'I name all my projects after myself.'

Hands-on challenge

Initialize a new Git repo, create a .gitignore (ignore node_modules and .env), make your first commit. Then create a 'feature-hello' branch, add a hello.ts file, commit it, and merge back into main. Check git log to see the history!

More resources

Open interactive version (quiz + challenge) ← Back to course: Full-Stack Playbook