12 min read
Git and GitHub Essentials
Learn the 20% of Git that covers 95% of what you'll need — then push your project to GitHub.
Why Git Matters
Your project is sitting on your machine right now. If your hard drive dies, it's gone. If you make a bad change, you can't undo it. If you want to collaborate, there's no way to share it.
Git solves all of this. It tracks every change you make, lets you rewind to any point in time, and syncs your code to the cloud via GitHub.
The good news: you only need a handful of commands to be productive. Let's cover them.
The Core Commands
There are really only seven Git commands you'll use constantly. Everything else is edge cases.
This creates a hidden .git/ folder in your project that stores the entire history. You only run this once per project.
cd nerdsystem-app
git initBefore you can save a snapshot, you tell Git which changes to include. This is called "staging."
# Stage a specific file
git add src/app/page.tsx
# Stage everything that changed
git add .A commit is a saved checkpoint. Each one gets a unique ID and your message describing what changed.
git commit -m "update home page with custom heading"Push sends your commits from your machine to a remote repository on GitHub.
git push origin mainPull grabs the latest commits from GitHub — useful when you work from multiple machines or with other people.
git pull origin mainBranches let you work on a feature without touching the main codebase. If things go wrong, main is untouched.
git branch new-featureMove between branches to work on different things.
git checkout new-feature
# Shortcut: create AND switch in one command
git checkout -b another-featureYou can ask Claude Code to handle git operations: "Create a new branch called add-about-page, make this change, commit it, and push to GitHub." It's especially useful for complex git operations like rebasing or resolving merge conflicts.
The Workflow in Action
Here's what the full cycle looks like when you make a change:
$ cd nerdsystem-app $ git init Initialized empty Git repository in /home/you/nerdsystem-app/.git/ $ git add . $ git commit -m "initial commit: scaffold Next.js project" [main (root-commit) a1b2c3d] initial commit: scaffold Next.js project 18 files changed, 3847 insertions(+) create mode 100644 .gitignore create mode 100644 package.json create mode 100644 src/app/layout.tsx create mode 100644 src/app/page.tsx ...
That's it — your project is now tracked. Every future change gets layered on top of this snapshot.
Push to GitHub
Now let's back it up to the cloud. You'll need the GitHub repo created first.
Go to github.com/new. Name it nerdsystem-app. Leave it empty — don't add a README or .gitignore (we already have those).
Before you can push, GitHub needs to know who you are. The easiest method is the GitHub CLI:
brew install ghThen log in:
$ gh auth login ? What account do you want to log into? GitHub.com ? What is your preferred protocol for Git operations? HTTPS ? Authenticate Git with your GitHub credentials? Yes ? How would you like to authenticate GitHub CLI? Login with a web browser ! First copy your one-time code: XXXX-XXXX Press Enter to open github.com in your browser... ✓ Authentication complete. - gh config set -h github.com git_protocol https ✓ Configured git protocol ✓ Logged in as your-username
Alternative: SSH key authentication
If you prefer SSH keys over HTTPS, follow GitHub's SSH setup guide. SSH uses key pairs instead of browser-based login, and is common in professional environments.
GitHub will show you the commands after you create the repo. Here's what they do:
git remote add origin https://github.com/YOUR-USERNAME/nerdsystem-app.git
git branch -M main
git push -u origin mainRefresh your GitHub repo page. You should see all your files there.
$ git remote add origin https://github.com/your-user/nerdsystem-app.git $ git branch -M main $ git push -u origin main Enumerating objects: 24, done. Counting objects: 100% (24/24), done. Delta compression using up to 8 threads Compressing objects: 100% (21/21), done. Writing objects: 100% (24/24), 50.12 KiB | 6.27 MiB/s, done. Total 24 (delta 0), reused 0 (delta 0) To https://github.com/your-user/nerdsystem-app.git * [new branch] main -> main branch 'main' set up to track 'origin/main'.
From now on, after making changes, the cycle is just: git add . then git commit -m "message" then git push.
Understanding .gitignore
Not everything should be tracked. The .gitignore file tells Git which files and folders to skip. create-next-app generated one for you — here's what's in it and why:
# dependencies — huge folder, reinstalled from package.json
/node_modules
# build output — regenerated on every build
/.next
/out
# environment variables — contains secrets!
.env
.env.local
.env.production.local
.env.development.local
# debug logs
npm-debug.log*
# editor and OS files
.DS_Store
*.swpThe biggest ones to remember: node_modules/ is massive and reproducible (anyone can run npm install to get it back), and .env* files contain secrets.
Never commit .env files, API keys, database passwords, or any secret to Git. Once something is in your Git history, it's extremely difficult to fully remove — even if you delete the file later. Treat your Git history as public.
Quick Reference
Here's the mental model. Git has three "zones" your code moves through:
- Working directory — your files on disk, with unsaved changes
- Staging area — changes you've marked for the next commit (
git add) - Repository — committed snapshots stored in
.git/(git commit)
And git push copies your repository to GitHub. That's the whole system.
What does 'git add .' do?
Why should you never commit .env files?
What's the purpose of creating a branch?
Troubleshooting common errors
fatal: not a git repository
You're running git commands outside your project directory. Make sure you cd into your project folder first:
cd nerdsystem-app
git statusgit push rejected (non-fast-forward)
Someone (or you, from another machine) pushed changes you don't have locally. Pull first, then push:
git pull --rebase origin main
git push origin mainGitHub authentication failure
If git push asks for a password and fails, re-run gh auth login and make sure you selected HTTPS and authenticated via the browser. Check that you're logged in with gh auth status.
Create a new branch called add-about-page, make any small change (like adding a comment to page.tsx), commit it, and push it to GitHub. Then switch back to main.
Show hint
Use git checkout -b add-about-page to create and switch to the new branch. Then git add ., git commit -m "your message", git push -u origin add-about-page, and finally git checkout main to switch back.
Show solution
# Create and switch to new branch
git checkout -b add-about-page
# Make a change (e.g., add a comment)
echo "// About page coming soon" >> src/app/page.tsx
# Stage, commit, and push
git add src/app/page.tsx
git commit -m "chore: prepare for about page"
git push -u origin add-about-page
# Switch back to main
git checkout mainMerging Your Branch
After finishing work on a branch, you need to merge it back into main:
git checkout main
git merge add-about-pageThis brings your branch's changes into main. On real projects, you'll open a Pull Request on GitHub instead of merging locally — this lets teammates review your code before it's merged. You can create PRs using the GitHub CLI (gh pr create) or ask Claude Code to do it for you.
Next Up
You've got a project, it's tracked with Git, and it's backed up on GitHub. Now let's talk about the thing that makes this whole course different — how to actually work with AI as your development partner. The next lesson covers the AI-first workflow you'll use for the rest of this course.