What is Git? What is GitHub?
Git is a version control system — it tracks every change you make to your code, lets you go back in time, and enables multiple people to work on the same project simultaneously.
GitHub is a cloud platform that hosts Git repositories. It adds features like pull requests, code review, issue tracking, and CI/CD on top of Git.
| Git | GitHub | |
|---|---|---|
| Type | Software (runs locally) | Web platform (cloud) |
| Purpose | Version control, commit history | Remote storage, collaboration |
| Works offline? | Yes | No |
| Alternatives | Mercurial, SVN | GitLab, Bitbucket |
Analogy: Git is like Google Docs history (tracks changes). GitHub is like Google Drive (stores and shares your documents).
Installation & Setup
# Install Git (download from git-scm.com)
# Then verify installation:
git --version
# Configure your identity (done once globally)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# Set default branch name to 'main'
git config --global init.defaultBranch main
# Check your config
git config --listCore Concepts
Repository (repo)
A repository is a folder tracked by Git. It contains your project files plus a hidden .git folder that stores the entire history.
The Three Areas
Understanding these three areas is the key to understanding Git:
| Area | What it is | How to move files here |
|---|---|---|
| Working Directory | Your actual files on disk | Just edit files normally |
| Staging Area (Index) | Files you've marked to include in the next commit | git add <file> |
| Repository | Permanently saved snapshots (commits) | git commit |
Commit
A commit is a permanent snapshot of your staged changes. Each commit has a unique hash (like a3f9b2c), a message, an author, and a timestamp.
Basic Workflow
Every day with Git follows this loop:
Essential Commands Reference
Setup & Inspection
git init # Initialize new repo in current folder
git clone <url> # Download remote repo
git status # Show working tree status
git log # Show commit history
git log --oneline # Compact log
git log --oneline --graph # Visual branch graph
git diff # Show unstaged changes
git diff --staged # Show staged changesStaging & Committing
git add <file> # Stage a specific file
git add . # Stage all changes
git add -p # Interactively stage chunks
git commit -m "message" # Commit with message
git commit -am "message" # Stage tracked files + commit in one step
git restore <file> # Discard working directory changes
git restore --staged <file> # Unstage a fileRemote Repositories
git remote add origin <url> # Connect local repo to GitHub
git remote -v # List remotes
git push origin main # Push main branch to GitHub
git push -u origin main # Push + set upstream (first time)
git pull # Fetch + merge remote changes
git fetch # Fetch without mergingFirst push tip: Use git push -u origin main the first time. After that, just git push is enough.
Branching
Branches let you work on features or fixes without affecting the main codebase. The main branch is the production-ready code; new branches are where development happens.
# Create and switch to a new branch
git checkout -b feature/login
# or (modern syntax):
git switch -c feature/login
# List all branches
git branch
# Switch to an existing branch
git checkout main
git switch main
# Merge branch into main
git checkout main
git merge feature/login
# Delete merged branch
git branch -d feature/login
# Delete unmerged branch (force)
git branch -D feature/loginBranch naming conventions:
feature/user-auth — new functionality
fix/login-bug — bug fixes
hotfix/critical-issue — urgent production fixes
chore/update-deps — maintenance tasks
GitHub Workflow
Connecting to GitHub
# Option 1: Create new repo on GitHub, then connect:
git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/username/repo.git
git push -u origin main
# Option 2: Clone existing repo:
git clone https://github.com/username/repo.git
cd repoPull Requests (PRs)
A pull request is how you propose changes to a repository on GitHub. It lets others review your code before it's merged.
- Create a branch:
git switch -c feature/new-feature - Make commits on the branch
- Push the branch:
git push origin feature/new-feature - Open GitHub → click "Compare & pull request"
- Fill in title + description → click "Create pull request"
- After review → merge the PR
- Pull the merged main:
git pull origin main
Keeping your fork in sync
# Add the original repo as "upstream"
git remote add upstream https://github.com/original-owner/repo.git
# Fetch updates
git fetch upstream
# Merge upstream into your local main
git checkout main
git merge upstream/main
# Push to your fork
git push origin mainCommon Mistakes & Fixes
"I committed to the wrong branch"
# Move last commit to correct branch
git branch correct-branch # create correct branch
git reset HEAD~1 # undo commit on wrong branch
git stash # stash the changes
git switch correct-branch
git stash pop # restore changes
git commit -m "message" # commit on correct branch"I need to undo my last commit"
# Keep changes but undo commit (soft reset)
git reset --soft HEAD~1
# Keep changes in working dir but unstage (mixed reset — default)
git reset HEAD~1
# Discard changes completely (DESTRUCTIVE)
git reset --hard HEAD~1Never force-push to shared branches. git reset --hard and git push --force on a branch others are using will cause serious problems for your team.
"I want to undo a pushed commit safely"
# Use git revert — creates a new "undo" commit, keeps history intact
git revert <commit-hash>
git push.gitignore — What to exclude
# .gitignore file in project root
node_modules/
.env
.env.local
dist/
build/
*.log
.DS_Store
.vscode/
*.sqliteRemember: Add .gitignore before your first commit. If you've already committed files that should be ignored, use git rm --cached <file> to untrack them.