Git Basics
Installation, first repo, essential commands
What is Git?
Git is a time machine for your code. It tracks every change you make, lets you go back in time, and enables multiple people to work on the same project without chaos.
Installation
Windows
# Download from https://git-scm.com/download/win
# Run installer, use default options
# Verify installation:
git --version
macOS
# Using Homebrew (recommended)
brew install git
# Or install Xcode Command Line Tools
xcode-select --install
Linux
# Ubuntu/Debian
sudo apt update && sudo apt install git
# Fedora
sudo dnf install git
First-Time Setup
Tell Git who you are (do this once):
# Set your name
git config --global user.name "Your Name"
# Set your email
git config --global user.email "you@example.com"
# Verify settings
git config --list
Your First Repository
A repository (repo) is a folder that Git tracks. Let’s create one.
# Create a new folder
mkdir my-project
cd my-project
# Initialize Git (creates hidden .git folder)
git init
That’s it! Your folder is now a Git repository.
The Three States
Every file in Git exists in one of three states:
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Working │ │ Staging │ │ Repository │
│ Directory │───▶│ Area │───▶│ (History) │
│ │ │ │ │ │
│ Your files │ │ Ready to │ │ Saved │
│ as you see │ │ commit │ │ forever │
│ them │ │ │ │ │
└─────────────┘ └─────────────┘ └─────────────┘
▲ │
└───────────────────┘
checkout / reset
| State | What It Means | Command |
|---|---|---|
| Modified | You changed the file | (automatic) |
| Staged | Marked for next commit | git add |
| Committed | Saved in history | git commit |
Basic Workflow
Step 1: Create a File
# Create a simple file
echo "Hello, Git!" > hello.txt
Step 2: Check Status
git status
# Output:
# Untracked files:
# hello.txt
Untracked means Git sees the file but isn’t tracking it yet.
Step 3: Stage the File
# Add specific file
git add hello.txt
# Or add all files
git add .
Step 4: Commit
git commit -m "Add hello.txt file"
# Output:
# [main abc1234] Add hello.txt file
# 1 file changed, 1 insertion(+)
Essential Commands
Check Status
# See what's changed
git status
# Short version
git status -s
View History
# See all commits
git log
# One line per commit
git log --oneline
# With graph (shows branches)
git log --oneline --graph
See Changes
# Changes not yet staged
git diff
# Changes that are staged
git diff --staged
# Changes in a specific file
git diff hello.txt
Undo Changes
# Discard changes in working directory
git checkout -- hello.txt
# Unstage a file (keep changes)
git reset HEAD hello.txt
# Undo last commit (keep changes)
git reset --soft HEAD~1
# Undo last commit (discard changes) ⚠️
git reset --hard HEAD~1
Ignoring Files (.gitignore)
Some files shouldn’t be tracked: logs, dependencies, secrets. Use .gitignore.
# Create .gitignore file
touch .gitignore
Common Patterns
# .gitignore
# Dependencies
node_modules/
vendor/
target/
# Build output
dist/
build/
*.class
# IDE files
.idea/
.vscode/
*.iml
# Logs
*.log
logs/
# Environment/Secrets
.env
*.pem
credentials.json
# OS files
.DS_Store
Thumbs.db
Quick Reference
# Setup
git init # Initialize new repo
git config --global user.name "Name"
git config --global user.email "email"
# Daily workflow
git status # Check status
git add . # Stage all changes
git commit -m "message" # Commit with message
git log --oneline # View history
# Viewing changes
git diff # Unstaged changes
git diff --staged # Staged changes
git show <commit> # View specific commit
# Undoing things
git checkout -- <file> # Discard file changes
git reset HEAD <file> # Unstage file
git reset --soft HEAD~1 # Undo commit, keep changes
git reset --hard HEAD~1 # Undo commit, lose changes
Practice Exercise
Try this hands-on exercise:
# 1. Create and initialize a project
mkdir git-practice && cd git-practice
git init
# 2. Create some files
echo "# My Project" > README.md
echo "console.log('Hello');" > app.js
# 3. Stage and commit
git add .
git commit -m "Initial commit"
# 4. Make changes
echo "More content" >> README.md
# 5. See the diff
git diff
# 6. Stage and commit
git add README.md
git commit -m "Update README"
# 7. View history
git log --oneline