Git is a distributed version control system that tracks changes to your files over time. Every developer needs to know Git — it's the foundation of modern software collaboration.
Initializing a Repository
To start tracking a project with Git, navigate to your project folder and run:
git initThis creates a hidden .git directory that stores all of Git's tracking data. Your project is now a Git repository.
To clone an existing repository instead:
git clone https://github.com/username/repo.git
The Three Areas
Git has three main areas you need to understand:
- Working Directory — the files you see and edit on disk
- Staging Area (Index) — a snapshot of what will go into your next commit
- Repository (.git) — the permanent history of all committed snapshots
The flow is: edit files -> stage them -> commit them.
Checking Status
The git status command shows you what's going on:
git statusThis tells you which files are modified, which are staged, and which are untracked. Run it frequently — it's your best friend in Git.
Staging Changes
Use git add to move changes from the working directory to the staging area:
# Stage a specific file
git add index.html
# Stage multiple files
git add src/app.js src/utils.js
# Stage all changes in the current directory
git add .To unstage a file without losing changes:
git restore --staged index.htmlCommitting
A commit is a permanent snapshot of your staged changes:
git commit -m "Add user login form"Write meaningful commit messages. A good message explains why the change was made, not just what changed. Keep the first line under 72 characters.
# Multi-line commit message
git commit -m "Fix login redirect loop
Users were being redirected back to the login page after
successful authentication because the session cookie was
not being set before the redirect."Viewing History
Use git log to see the commit history:
# Full log
git log
# Compact one-line format
git log --oneline
# Show the last 5 commits
git log --oneline -5
# Show a graph of branches
git log --oneline --graph --allViewing Differences
The git diff command shows exactly what changed:
# Changes in working directory (unstaged)
git diff
# Changes that are staged
git diff --staged
# Diff between two commits
git diff abc123 def456
# Diff for a specific file
git diff src/app.jsIgnoring Files with .gitignore
Create a .gitignore file in your project root to tell Git which files to skip:
# Dependencies
node_modules/
# Environment variables
.env
.env.local
# Build output
dist/
build/
.next/
# OS files
.DS_Store
Thumbs.db
# IDE files
.vscode/
.idea/The .gitignore file itself should be committed to the repository so all team members share the same ignore rules.
If a file is already tracked and you add it to .gitignore, Git will keep tracking it. To stop tracking it:
git rm --cached .env
git commit -m "Stop tracking .env file"Undoing Mistakes
Discard changes to a file in the working directory:
git restore index.htmlAmend the last commit (e.g., to fix a typo in the message or add a forgotten file):
git add forgotten-file.js
git commit --amend -m "Add user login form and validation"Only amend commits that haven't been pushed yet.