Git is the most widely used version control system in the world. It tracks changes to your code, enables collaboration with other developers, and provides a safety net for your projects.
What is Version Control?
Version control is a system that records changes to files over time. It allows you to:
- Track history: See who changed what and when
- Revert changes: Go back to any previous version
- Collaborate: Work with others without overwriting each other's work
- Branch: Experiment safely without affecting the main code
- Backup: Your code is stored in multiple places
Why Git?
- Distributed: Every developer has a full copy of the repository
- Fast: Most operations are local and instant
- Industry standard: Used by almost every tech company
- Free and open source: Works with GitHub, GitLab, Bitbucket, etc.
Getting Started
Installing Git
# Windows: Download from git-scm.com or use:
winget install Git.Git
# macOS:
brew install git
# Linux (Debian/Ubuntu):
sudo apt install git
# Verify installation
git --version Initial Configuration
# Set your identity (required for commits)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# Set default branch name
git config --global init.defaultBranch main
# Enable colored output
git config --global color.ui auto
# View your configuration
git config --list Creating a Repository
# Create a new repository
mkdir my-project
cd my-project
git init
# Or clone an existing repository
git clone https://github.com/username/repo.git
git clone git@github.com:username/repo.git # SSH Basic Commands
The Git Workflow
Git has three main areas:
- Working Directory: Your actual files
- Staging Area: Files ready to be committed
- Repository: Committed history
Working Directory → Staging Area → Repository
(edit) (git add) (git commit) Checking Status
# See the state of your working directory
git status
# Short status format
git status -s Staging Changes
# Stage a specific file
git add filename.txt
# Stage multiple files
git add file1.txt file2.txt
# Stage all changes in current directory
git add .
# Stage all changes everywhere
git add -A
# Interactive staging
git add -p # Choose which changes to stage Committing Changes
# Commit with a message
git commit -m "Add user login feature"
# Stage and commit in one step (tracked files only)
git commit -am "Fix navigation bug"
# Commit with detailed message (opens editor)
git commit Viewing History
# View commit history
git log
# Compact one-line format
git log --oneline
# Show changes in each commit
git log -p
# Show last 5 commits
git log -5
# Visual branch graph
git log --oneline --graph --all Viewing Changes
# Changes not yet staged
git diff
# Changes staged for commit
git diff --staged
# Changes in a specific file
git diff filename.txt
# Compare two commits
git diff abc123 def456 Branching
Branches let you work on different features or fixes independently. The main branch (usually
called main or master) contains your stable code.
Branch Commands
# List all branches
git branch
# List remote branches
git branch -r
# List all branches (local and remote)
git branch -a
# Create a new branch
git branch feature-login
# Switch to a branch
git checkout feature-login
# Or (Git 2.23+)
git switch feature-login
# Create and switch in one command
git checkout -b feature-login
# Or
git switch -c feature-login
# Rename current branch
git branch -m new-name
# Delete a branch (must not be checked out)
git branch -d feature-login
# Force delete (unmerged branch)
git branch -D feature-login Common Branching Strategy
main ●───●───●───●───●───●
\ /
feature-login ●───●───●
# 1. Create feature branch
git checkout -b feature-login
# 2. Work on your feature (add, commit, repeat)
git add .
git commit -m "Add login form"
git commit -m "Add validation"
git commit -m "Add authentication logic"
# 3. Merge back to main
git checkout main
git merge feature-login
# 4. Delete feature branch
git branch -d feature-login Merging & Conflicts
Merging Branches
# Merge feature branch into current branch
git merge feature-login
# Merge with a commit message
git merge feature-login -m "Merge login feature"
# Abort a merge in progress
git merge --abort Handling Merge Conflicts
Conflicts occur when the same lines were changed in both branches. Git marks these conflicts in your files:
<<<<<<< HEAD
Your changes on the current branch
=======
Changes from the branch being merged
>>>>>>> feature-login To resolve conflicts:
- Open the conflicted file(s)
- Find the conflict markers (
<<<<<<<,=======,>>>>>>>) - Edit the file to keep the correct code (remove markers)
- Stage the resolved file:
git add filename - Complete the merge:
git commit
Rebasing (Alternative to Merging)
# Rebase current branch onto main
git rebase main
# Interactive rebase (edit, squash, reorder commits)
git rebase -i main
# Abort a rebase
git rebase --abort
# Continue after resolving conflicts
git rebase --continue Remote Repositories
Remote repositories (like GitHub, GitLab) store your code online, enabling backup and collaboration.
Managing Remotes
# View remotes
git remote -v
# Add a remote
git remote add origin https://github.com/user/repo.git
# Change remote URL
git remote set-url origin https://github.com/user/new-repo.git
# Remove a remote
git remote remove origin Fetching and Pulling
# Fetch changes (download but don't merge)
git fetch origin
# Pull changes (fetch + merge)
git pull origin main
# Pull with rebase instead of merge
git pull --rebase origin main Pushing Changes
# Push to remote
git push origin main
# Push and set upstream (first push of a branch)
git push -u origin feature-login
# After setting upstream, just use:
git push
# Force push (use with caution!)
git push --force # Overwrites remote history Collaboration Workflows
Feature Branch Workflow
# 1. Update main branch
git checkout main
git pull origin main
# 2. Create feature branch
git checkout -b feature/add-search
# 3. Work on feature
git add .
git commit -m "Implement search functionality"
# 4. Push feature branch
git push -u origin feature/add-search
# 5. Create Pull Request on GitHub/GitLab
# 6. After PR is merged, clean up
git checkout main
git pull origin main
git branch -d feature/add-search Fork and Pull Request Workflow
# 1. Fork repository on GitHub
# 2. Clone your fork
git clone https://github.com/YOUR-USER/repo.git
# 3. Add upstream remote
git remote add upstream https://github.com/ORIGINAL/repo.git
# 4. Keep fork synced
git fetch upstream
git checkout main
git merge upstream/main
# 5. Create branch, make changes, push to your fork
git checkout -b fix/typo
git commit -am "Fix typo in readme"
git push origin fix/typo
# 6. Create Pull Request from your fork to original repo Best Practices
Write Good Commit Messages
# Good commit messages
git commit -m "Add user authentication with JWT"
git commit -m "Fix memory leak in image processor"
git commit -m "Refactor database queries for performance"
# Bad commit messages
git commit -m "fix"
git commit -m "updates"
git commit -m "WIP" Commit Message Format
# Short (50 chars or less) summary
Add user authentication with JWT
# Longer explanation if needed (wrap at 72 chars)
# Explain what and why, not how
Implement JWT-based authentication to replace session cookies.
This improves security and enables stateless API design.
- Add login/logout endpoints
- Create JWT middleware
- Update user model with password hashing Useful Shortcuts
# Undo last commit (keep changes)
git reset --soft HEAD~1
# Undo last commit (discard changes)
git reset --hard HEAD~1
# Discard changes in working directory
git checkout -- filename.txt
# Or (Git 2.23+)
git restore filename.txt
# Unstage a file
git reset HEAD filename.txt
# Or
git restore --staged filename.txt
# Stash changes temporarily
git stash
git stash pop # Restore stashed changes
# View a file from another branch
git show branch-name:path/to/file Git Ignore
Create a .gitignore file to exclude files from tracking:
# .gitignore example
# Dependencies
node_modules/
vendor/
# Build output
dist/
build/
# Environment files
.env
.env.local
# IDE files
.vscode/
.idea/
# OS files
.DS_Store
Thumbs.db
# Logs
*.log
logs/ Quick Reference
| Task | Command |
|---|---|
| Initialize repo | git init |
| Clone repo | git clone url |
| Check status | git status |
| Stage changes | git add . |
| Commit | git commit -m "message" |
| View history | git log --oneline |
| Create branch | git checkout -b name |
| Switch branch | git checkout name |
| Merge branch | git merge name |
| Push changes | git push origin branch |
| Pull changes | git pull origin branch |
Next Steps
- HTML & CSS - Build web pages to version control
- JavaScript - Add interactivity to your projects
- Developer Tools - Explore Git GUIs and integrations