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
- git init & git clone — git init creates a new repository. git clone URL downloads an existing one. Every project starts with one of these. Your .git folder stores all the history.
- git add & git commit — git add stages files (selects what to save). git commit -m 'message' saves a snapshot. Think: add = put items in a box, commit = seal and label the box.
- git push & git pull — git push uploads your commits to GitHub. git pull downloads others' commits. Push your work, pull your teammates' work. Stay in sync!
- Branching — git branch feature-login creates a parallel timeline. Work on features without affecting main. When done, merge back. Main stays clean, experiments stay safe.
- Pull Requests (PRs) — The team workflow: create a branch, make changes, push, open a PR on GitHub. Teammates review your code, suggest changes, then approve. Merge when ready!
- .gitignore — List files Git should ignore: node_modules/, .env, dist/, *.log. These files shouldn't be in your repository — they're generated, temporary, or secret.
- Commit Message Best Practices — Good: 'Add user authentication with JWT guards'. Bad: 'fixed stuff'. Use present tense, be specific, explain WHY not just WHAT. Your future self will thank you.
- Git Stash — Working on something but need to switch branches? git stash saves your changes temporarily. git stash pop brings them back. Like a clipboard for uncommitted work.
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. Initialize a new project
- 2. Git command
- 3. Git command
- 4. Git command
- 5.
- 6. Connect to GitHub
- 7. Git command
- 8. Git command
- 9.
- 10. Daily workflow
- 11. Git command
- 12. Git command
- 13. Git command
- 14. Git command
- 15.
- 16. Branching workflow
- 17. Git command
- 18. Git command
- 19. ... make changes ...
- 20. Git command
- 21. Git command
- 22. Git command
- 23. -> Open Pull Request on GitHub
- 24. -> Team reviews and approves
- 25. -> Merge into main
- 26.
- 27. Useful commands
- 28. Git command
- 29. Git command
- 30. Git command
- 31. Git command
- 32. Git command
- 33. Git command
- 34.
- 35. .gitignore
- 36.
- 37.
- 38. Method chaining on the previous expression
- 39. Method chaining on the previous expression
- 40. .log
- 41. Method chaining on the previous expression
- 42.
Spot the bug
git add .
git commit -m "initial commit"
git push origin mainNeed 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
- Git Official Documentation (Git Official)
- Atlassian Git Tutorial (Atlassian)
- Git Explained in 100 Seconds (Fireship)