🔀 Week 3: Git & GitHub Mastery

Day 1: Git Basics - init, add, commit, status, log

Duration: 5 Hours

📚 Learning Objectives

By the end of this session, you will be able to:

  • Understand what version control is and why it matters in DevOps
  • Install and configure Git on your system
  • Initialize a new Git repository
  • Track changes with add, commit, and status
  • View commit history with git log

📖 Core Concepts (2 Hours)

What is Version Control?

Version control is a system that records changes to files over time, allowing you to recall specific versions later. It's essential for DevOps because it enables collaboration, tracks changes, and provides a safety net for code.

  • Why DevOps needs Git: Infrastructure as Code, CI/CD pipelines, collaboration
  • Why Git? Distributed, fast, industry standard, works offline
  • Key concepts: Repository, commit, staging area, working directory

Installing Git

# Check if Git is installed git --version # On Ubuntu/Debian sudo apt update sudo apt install git # On macOS brew install git # On Windows - Download from git-scm.com # Or use: winget install Git.Git

Configuring Git (First Time Setup)

# Set your identity (Required for commits) git config --global user.name "Your Name" git config --global user.email "your.email@example.com" # Set default branch name git config --global init.defaultBranch main # Set default editor git config --global core.editor "code --wait" # VS Code git config --global core.editor "vim" # Vim # Enable colored output git config --global color.ui auto # View all settings git config --list git config --global --list # View specific setting git config user.name

Git Repository Structure

project/ ├── .git/ # Git's internal database (don't touch!) │ ├── objects/ # All file versions stored here │ ├── refs/ # Branch and tag pointers │ ├── HEAD # Points to current branch │ └── config # Repository-specific settings ├── src/ # Your source code ├── docs/ # Your documentation └── README.md # Project description

Git Workflow Overview

Working Directory → Staging Area → Repository (edit) (add) (commit) 1. You EDIT files in working directory 2. You STAGE changes you want to commit (git add) 3. You COMMIT staged changes to repository (git commit)

git init - Initialize Repository

# Create a new project directory mkdir my-devops-project cd my-devops-project # Initialize Git repository git init # Output: Initialized empty Git repository in /path/to/my-devops-project/.git/ # Verify .git directory was created ls -la # You'll see: drwxr-xr-x .git # Check repository status git status

git status - Check Repository State

# Check status (do this often!) git status # Common status outputs: # "Untracked files" - New files Git doesn't know about # "Changes not staged" - Modified files not yet added # "Changes to be committed" - Files in staging area # "Nothing to commit" - Everything is saved # Short status format git status -s # M - Modified # A - Added # ?? - Untracked # Show branch info too git status -sb

git add - Stage Changes

# Create a file first echo "# My DevOps Project" > README.md # Stage a specific file git add README.md # Stage multiple files git add file1.txt file2.txt # Stage all files in current directory git add . # Stage all changes (including deletions) git add -A git add --all # Stage parts of a file (interactive) git add -p filename # Unstage a file (remove from staging) git restore --staged filename # or (older syntax) git reset HEAD filename

git commit - Save Changes

# Commit with message git commit -m "Initial commit: Add README" # Commit with detailed message (opens editor) git commit # Stage and commit in one step (only tracked files) git commit -am "Update README with description" # Amend last commit (fix message or add files) git commit --amend -m "Better commit message" git add forgotten-file.txt git commit --amend --no-edit # Keep same message # Good commit message format: # Line 1: Short summary (50 chars max) # Line 2: Blank # Lines 3+: Detailed explanation if needed

git log - View History

# View commit history git log # Output shows: # commit abc123... (hash) # Author: Your Name # Date: Mon Jan 1 12:00:00 2024 # # Commit message here # Compact one-line format git log --oneline # Show last N commits git log -5 # Show with file changes git log --stat # Show with actual diff git log -p # Show graph of branches git log --oneline --graph --all # Search commits by message git log --grep="fix" # Search commits by author git log --author="John" # Show commits in date range git log --since="2024-01-01" --until="2024-02-01"

git diff - See Changes

# See unstaged changes git diff # See staged changes git diff --staged git diff --cached # Compare with specific commit git diff HEAD~1 # Previous commit git diff abc123 # Specific commit hash # Compare two commits git diff abc123 def456 # See changes for specific file git diff README.md git diff --staged README.md

🔬 Hands-on Lab (2.5 Hours)

Lab 1: Git Setup and Configuration

  • Install Git on your system
  • Configure your identity
  • Set up preferred editor
# Lab 1: Git configuration # Verify installation git --version # Configure identity git config --global user.name "Your Name" git config --global user.email "your.email@example.com" # Set default branch git config --global init.defaultBranch main # Set VS Code as editor git config --global core.editor "code --wait" # Verify settings git config --list

Lab 2: Create Your First Repository

  • Create a new project directory
  • Initialize Git repository
  • Create and commit files
# Lab 2: First repository # Create project directory mkdir ~/git-practice cd ~/git-practice # Initialize repository git init # Check status git status # Create README cat > README.md << 'EOF' # Git Practice Project This is my first Git repository! ## What I'm Learning - Git basics - Version control concepts - DevOps workflows EOF # Check status - see untracked file git status # Stage the file git add README.md # Check status - see staged file git status # Commit with message git commit -m "Initial commit: Add README" # View commit history git log git log --oneline

Lab 3: Practice the Git Workflow

  • Make multiple changes and commits
  • Practice staging individual files
  • View and understand commit history
# Lab 3: Multiple commits practice cd ~/git-practice # Create source directory mkdir src # Create first script cat > src/hello.sh << 'EOF' #!/bin/bash echo "Hello from Git!" echo "Today is $(date)" EOF chmod +x src/hello.sh # Stage and commit git add src/hello.sh git commit -m "Add hello script" # Create config file cat > config.txt << 'EOF' APP_NAME=GitPractice VERSION=1.0 DEBUG=false EOF # Create .gitignore cat > .gitignore << 'EOF' # Ignore log files *.log # Ignore temp files *.tmp temp/ # Ignore environment files .env .env.local EOF # Stage both files git add config.txt .gitignore git status # Commit together git commit -m "Add config and gitignore files" # Make a change to existing file echo "AUTHOR=YourName" >> config.txt # See the change git diff # Stage and commit git add config.txt git commit -m "Add author to config" # View full history git log --oneline # View detailed history git log --stat

Lab 4: Exploring Git History

  • Use different git log formats
  • View file changes between commits
  • Search through history
# Lab 4: Git history exploration cd ~/git-practice # View history different ways git log # Full format git log --oneline # Compact git log --oneline --graph # With branch graph git log -3 # Last 3 commits git log --stat # With file stats git log -p # With full diff # View specific commit git show HEAD # Latest commit git show HEAD~1 # Previous commit git show abc123 # Specific commit (use your hash) # See who changed what git log --author="Your Name" # Search commit messages git log --grep="Add" # View history of specific file git log -- README.md git log -p -- README.md # With changes

📝 Practice Exercises (30 Minutes)

  1. Create a new repository for a "devops-scripts" project
  2. Add at least 3 different files with meaningful commits
  3. Practice using git status before and after each operation
  4. Create a .gitignore file that ignores common patterns
  5. Use git log with different options to explore history

✅ Day 1 Checklist

  • Git installed and configured
  • Understand version control concepts
  • Can initialize a new repository (git init)
  • Can check repository status (git status)
  • Can stage files (git add)
  • Can commit changes (git commit)
  • Can view history (git log)
  • Created .gitignore file