Git is a version control system. It tracks every change you make to your code, lets you undo mistakes, and makes it possible for multiple people to work on the same project without overwriting each other’s work.
Every professional developer uses Git. It’s not optional — it’s as fundamental as knowing how to use a text editor.
The problem Git solves
Without Git:
project/
app.js
app_backup.js
app_backup2.js
app_FINAL.js
app_FINAL_v2.js
app_FINAL_ACTUALLY_FINAL.js
With Git:
project/
app.js ← Git tracks every version internally
Git remembers every change, who made it, and when. You can go back to any previous version at any time.
Key concepts
Repository (repo) — a project folder tracked by Git. Contains your code plus a hidden .git folder with the full history.
Commit — a snapshot of your code at a point in time. Like a save point in a video game.
Branch — a parallel version of your code. Work on a feature without affecting the main code. Merge it back when it’s ready.
Remote — a copy of your repo on a server (like GitHub). Lets you share code and collaborate.
Getting started
# Install Git
# macOS: brew install git
# Ubuntu: sudo apt install git
# Windows: download from git-scm.com
# Configure your identity
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# Create a new repo
mkdir my-project
cd my-project
git init
# Or clone an existing repo
git clone https://github.com/user/repo.git
The basic workflow
# 1. Make changes to your files
echo "Hello World" > index.html
# 2. Stage the changes (tell Git what to include)
git add index.html
git add . # Stage everything
# 3. Commit (save a snapshot)
git commit -m "Add homepage"
# 4. Push to GitHub
git push
That’s the daily loop: change → add → commit → push.
Checking status and history
# What's changed?
git status
# What exactly changed in each file?
git diff
# View commit history
git log
git log --oneline # Compact view
Branches
Branches let you work on features without breaking the main code:
# Create and switch to a new branch
git checkout -b feature/login
# Make changes, commit them
git add .
git commit -m "Add login page"
# Switch back to main
git checkout main
# Merge the feature branch
git merge feature/login
# Delete the branch (it's merged, you don't need it)
git branch -d feature/login
Working with GitHub
# Push your local repo to GitHub
git remote add origin https://github.com/user/repo.git
git push -u origin main
# Get changes from GitHub
git pull
# Push your changes
git push
The typical team workflow:
- Create a branch
- Make changes and commit
- Push the branch to GitHub
- Open a Pull Request (PR)
- Team reviews the code
- Merge the PR
Undoing things
# Undo changes to a file (before staging)
git checkout -- file.txt
git restore file.txt # Modern syntax
# Unstage a file (after git add, before commit)
git reset HEAD file.txt
git restore --staged file.txt # Modern syntax
# Undo the last commit (keep changes)
git reset --soft HEAD~1
# Undo the last commit (discard changes)
git reset --hard HEAD~1
.gitignore
Tell Git to ignore certain files:
# .gitignore
node_modules/
.env
dist/
*.log
.DS_Store
See: .gitignore cheat sheet for templates.
Common issues
- Git merge conflict — two people changed the same line
- Permission denied (publickey) — SSH key not set up
- fatal: not a git repository — you’re not in a Git repo
- git push rejected — someone pushed before you
Next steps
- Create a GitHub account
- Make a repo, push some code
- Learn branching — it’s the most powerful feature
- Check out the full Git cheat sheet for every command you’ll need