Setting up Git and GitHub

Week 1, Day 1 - Lecture 3

What is Version Control?

Imagine writing a novel. You'd want to:

Version control is like having a time machine for your code! Git is the most popular version control system, and GitHub is like a social network for code.

graph TD A[Working Directory] -->|git add| B[Staging Area] B -->|git commit| C[Local Repository] C -->|git push| D[Remote Repository/GitHub] D -->|git pull| C C -->|git checkout| A style A fill:#f9f,stroke:#333,stroke-width:2px style B fill:#ff9,stroke:#333,stroke-width:2px style C fill:#9ff,stroke:#333,stroke-width:2px style D fill:#9f9,stroke:#333,stroke-width:2px

Git vs GitHub

Think of it this way:

flowchart LR subgraph "Your Computer" A[Git] B[Local Repository] C[Project Files] end subgraph "Internet" D[GitHub] E[Remote Repository] F[Collaboration Features] end B <--> E A --> B C --> B E --> F style A fill:#f96,stroke:#333,stroke-width:2px style D fill:#6f9,stroke:#333,stroke-width:2px

Installing Git

Step 1: Check if Git is Already Installed

Open your terminal (Command Prompt on Windows, Terminal on Mac/Linux) and type:

git --version

If you see a version number, Git is already installed!

Step 2: Installation by Operating System

Windows Installation

  1. Download Git from git-scm.com
  2. Run the installer
  3. Important settings during installation:
    • Select Components: Keep all defaults
    • Default editor: Choose "Use Visual Studio Code as Git's default editor"
    • PATH environment: Select "Git from the command line and also from 3rd-party software"
    • Line ending conversions: Choose "Checkout Windows-style, commit Unix-style line endings"
    • Terminal emulator: Select "Use Windows' default console window"
  4. Click through remaining options (defaults are fine)
  5. Verify installation: Open new Command Prompt and type: git --version

Mac Installation

Option 1: Xcode Command Line Tools (Recommended)

  1. Open Terminal
  2. Type: git --version
  3. If not installed, you'll be prompted to install Xcode Command Line Tools
  4. Click "Install" and agree to the license

Option 2: Homebrew

  1. Install Homebrew if not already installed: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  2. Install Git: brew install git
  3. Verify installation: git --version

Linux Installation

Ubuntu/Debian:

sudo apt update
sudo apt install git

Fedora:

sudo dnf install git

Arch Linux:

sudo pacman -S git

Verify installation: git --version

Configuring Git

Now let's set up your Git identity. This is like creating your signature that will appear on all your code contributions.

Essential Configuration

# Set your name
git config --global user.name "Your Name"

# Set your email (use the same email for GitHub)
git config --global user.email "your.email@example.com"

# Set default branch name to 'main'
git config --global init.defaultBranch main

# Set VS Code as default editor
git config --global core.editor "code --wait"

# Enable color output
git config --global color.ui auto

Verify Your Configuration

# View all settings
git config --list

# View specific setting
git config user.name

Creating a GitHub Account

  1. Visit github.com
  2. Click "Sign up"
  3. Enter your details:
    • Use the same email as your Git configuration
    • Choose a professional username (employers will see this)
    • Use a strong password
  4. Complete the verification process
  5. Choose the free plan
  6. Customize your profile (add a photo, bio, location)

Connecting Git to GitHub

We'll use SSH keys for secure authentication. Think of SSH keys like a special pair of keys: one private (kept on your computer) and one public (shared with GitHub).

sequenceDiagram participant You participant Your Computer participant GitHub You->>Your Computer: Generate SSH keys Your Computer->>Your Computer: Creates public/private key pair You->>GitHub: Add public key GitHub->>GitHub: Stores public key You->>Your Computer: Attempt to push code Your Computer->>GitHub: Sends encrypted request GitHub->>GitHub: Verifies with public key GitHub->>Your Computer: Accepts connection Your Computer->>GitHub: Pushes code successfully

Step 1: Check for Existing SSH Keys

# Check if you already have SSH keys
ls -al ~/.ssh

Look for files named id_rsa.pub or id_ed25519.pub

Step 2: Generate New SSH Key

# Generate a new SSH key (use your GitHub email)
ssh-keygen -t ed25519 -C "your.email@example.com"

# If you're using a legacy system that doesn't support Ed25519:
ssh-keygen -t rsa -b 4096 -C "your.email@example.com"

When prompted:

Step 3: Add SSH Key to SSH Agent

# Start the ssh-agent
eval "$(ssh-agent -s)"

# Add your SSH key
ssh-add ~/.ssh/id_ed25519

Step 4: Add SSH Key to GitHub

  1. Copy your public key:
    # Copy to clipboard (Mac)
    pbcopy < ~/.ssh/id_ed25519.pub
    
    # Copy to clipboard (Linux)
    xclip -sel clip < ~/.ssh/id_ed25519.pub
    
    # Or just display it to copy manually
    cat ~/.ssh/id_ed25519.pub
  2. Go to GitHub → Settings → SSH and GPG keys
  3. Click "New SSH key"
  4. Give it a title (e.g., "My Laptop")
  5. Paste your key
  6. Click "Add SSH key"

Step 5: Test Your Connection

ssh -T git@github.com

You should see: "Hi username! You've successfully authenticated..."

Your First Repository

Let's create your first repository! A repository (or "repo") is like a project folder that Git tracks.

graph TD A[Create Repository] --> B[Clone to Computer] B --> C[Add Files] C --> D[Commit Changes] D --> E[Push to GitHub] subgraph "Local Development" C D end subgraph "GitHub" A E end style A fill:#9f9,stroke:#333,stroke-width:2px style E fill:#9f9,stroke:#333,stroke-width:2px

Create a Repository on GitHub

  1. Click the "+" icon in top right → "New repository"
  2. Name it "my-first-repo"
  3. Add a description: "Learning Git and GitHub"
  4. Check "Add a README file"
  5. Choose "MIT License"
  6. Click "Create repository"

Clone Repository to Your Computer

# Navigate to where you want to store your projects
cd ~/Documents

# Clone your repository
git clone git@github.com:YOUR_USERNAME/my-first-repo.git

# Enter the repository
cd my-first-repo

Make Your First Commit

# Create a new file
echo "# Hello Git!" > hello.md

# Check status
git status

# Add file to staging area
git add hello.md

# Commit with message
git commit -m "Add hello.md file"

# Push to GitHub
git push origin main

Essential Git Commands

Basic Workflow Commands

Command What it does When to use it
git status Check current state Frequently! Always check before committing
git add . Stage all changes When you want to commit all modified files
git add filename Stage specific file When you want to commit only certain files
git commit -m "message" Save changes locally After staging files, describe what changed
git push Upload to GitHub Share your commits with others
git pull Download changes Get latest updates from team members

The Git Workflow

flowchart LR A[Working Directory] -->|git add| B[Staging Area] B -->|git commit| C[Local Repository] C -->|git push| D[Remote Repository] D -->|git pull| C style A fill:#f9f,stroke:#333,stroke-width:2px style B fill:#ff9,stroke:#333,stroke-width:2px style C fill:#9ff,stroke:#333,stroke-width:2px style D fill:#9f9,stroke:#333,stroke-width:2px

Best Practices

Commit Messages

Good commit messages are like good code comments. They explain the "why", not just the "what".

Commit Frequency

Commit early, commit often! Think of commits as save points in a video game.

.gitignore File

Create a .gitignore file to tell Git which files to ignore:

# Node modules
node_modules/

# Environment variables
.env

# OS generated files
.DS_Store
Thumbs.db

# IDE files
.vscode/
.idea/

# Build outputs
dist/
build/

Troubleshooting Common Issues

Authentication Errors

If you get "Permission denied (publickey)":

Merge Conflicts

When the same file is changed by multiple people:

Forgot to Pull Before Pushing

# Get the latest changes first
git pull origin main

# If there are conflicts, resolve them
# Then push your changes
git push origin main

Assignment: Git and GitHub Setup

  1. Install Git on your computer
  2. Configure Git with your name and email
  3. Create a GitHub account
  4. Generate SSH keys and add to GitHub
  5. Create a new repository called "fullstack-course"
  6. Clone the repository to your computer
  7. Create these files:
    • README.md with "# My Full Stack Journey"
    • notes/day1.md with today's learnings
    • .gitignore with common exclusions
  8. Commit and push your changes
  9. Add a screenshot of your repository on GitHub to the README

Bonus: Create a personal profile README on GitHub!

Next Steps

Congratulations! You now have a professional development environment set up. You're ready to start your coding journey!

Tomorrow, we'll dive into HTML fundamentals and start building web pages. Make sure your environment is working properly and all your accounts are set up.