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.

GitGitHub
TypeSoftware (runs locally)Web platform (cloud)
PurposeVersion control, commit historyRemote storage, collaboration
Works offline?YesNo
AlternativesMercurial, SVNGitLab, 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 --list

Core 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:

AreaWhat it isHow to move files here
Working DirectoryYour actual files on diskJust edit files normally
Staging Area (Index)Files you've marked to include in the next commitgit add <file>
RepositoryPermanently 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:

1
Initialize or Clone
git init or git clone <url>
Start a new repo or download an existing one from GitHub.
2
Make Changes
edit files
Write your code, edit files, delete files — anything.
3
Check Status
git status
See which files are modified, staged, or untracked.
4
Stage Files
git add .
Add all changed files to the staging area.
5
Commit
git commit -m "message"
Save a snapshot with a descriptive message.
6
Push to GitHub
git push origin main
Upload your commits to the remote repository on GitHub.

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 changes

Staging & 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 file

Remote 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 merging

First 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/login

Branch 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 repo

Pull 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.

  1. Create a branch: git switch -c feature/new-feature
  2. Make commits on the branch
  3. Push the branch: git push origin feature/new-feature
  4. Open GitHub → click "Compare & pull request"
  5. Fill in title + description → click "Create pull request"
  6. After review → merge the PR
  7. 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 main

Common 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~1

Never 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/
*.sqlite

Remember: Add .gitignore before your first commit. If you've already committed files that should be ignored, use git rm --cached <file> to untrack them.