2  Git and GitHub

Git is essential for tracking code changes and collaborating with others. Think of Git as a sophisticated “undo” system that remembers every change you’ve ever made to your code, allowing you to travel back in time or collaborate with others without losing work. We’ll use Git throughout this tutorial.

2.1 What is Git?

Git is a distributed version control system that acts like a time machine for your code: - Tracks changes - Every modification is recorded with who, what, and when - Allows reverting - Go back to any previous version instantly - Enables collaboration - Multiple people can work on the same project simultaneously - Keeps complete history - Never lose work, always see what changed

Real-world analogy: Git is like Google Docs’ version history, but for code. You can see every change, who made it, and why.

2.2 Installing Git

2.2.1 Windows

# Using winget
winget install Git.Git

# Using chocolatey
choco install git

2.2.2 macOS

# Using Homebrew
brew install git

# Using Xcode Command Line Tools
xcode-select --install

2.2.3 Linux

# Ubuntu/Debian
sudo apt install git

# Fedora
sudo dnf install git

# Arch Linux
sudo pacman -S git

2.3 Basic Git Configuration

Set up your identity so Git knows who makes each change:

# Set your name (appears in commit history)
git config --global user.name "Your Name"
# Set your email (links commits to your GitHub account)
git config --global user.email "your.email@example.com"
# Set default branch name to 'main' (modern standard)
git config --global init.defaultBranch main

Why this matters: Every Git commit includes author information. This helps track who made what changes and enables proper attribution in team projects.

2.4 Essential Git Commands

2.4.1 Repository Initialization

# Create new repository (turns current folder into Git repo)
git init

# Clone existing repository (downloads entire project + history)
git clone https://github.com/user/repo.git

What happens: git init creates a hidden .git folder that stores all version history. git clone downloads a complete copy of someone else’s repository, including all commits and branches.

2.4.2 Basic Workflow

# Check status (see what files changed)
git status

# Add files to staging (prepare for commit)
git add filename.py           # Add specific file
git add .                     # Add all changed files

# Commit changes (save snapshot with message)
git commit -m "Add new feature"

# View history (see all previous commits)
git log --oneline

The Git workflow explained: 1. Working Directory - Where you edit files 2. Staging Area - Files prepared for commit (like a shopping cart) 3. Repository - Permanent storage of commits (like purchase history)

Think of it as: edit → stage → commit → repeat

2.4.3 Branching

# Create new branch (alternative timeline)
git branch feature-name
git checkout feature-name     # Switch to new branch
# Or in one command:
git checkout -b feature-name  # Create and switch simultaneously

# Switch branches (change timeline)
git checkout main

# Merge branch (combine timelines)
git checkout main             # Go to main branch
git merge feature-name        # Bring changes from feature branch

# Delete branch (clean up)
git branch -d feature-name

Branching explained: Branches are like parallel universes where you can experiment without affecting the main codebase. You can switch between branches instantly and merge successful experiments back to main.

2.5 GitHub Essentials

GitHub hosts Git repositories in the cloud and adds collaboration features. Think of GitHub as a social network for code - it stores your projects online and provides tools for collaboration, issue tracking, and project management.

2.5.1 Creating a Repository

  1. Go to github.com
  2. Click “New repository”
  3. Choose repository name
  4. Add description
  5. Choose public/private
  6. Add README, .gitignore, license

2.5.2 Connecting Local to Remote

# Add remote origin
git remote add origin https://github.com/username/repo.git

# Push to GitHub
git push -u origin main

# Pull changes
git pull origin main

2.5.3 GitHub Workflow

# 1. Create feature branch
git checkout -b feature/new-functionality

# 2. Make changes and commit
git add .
git commit -m "feat: add new functionality"

# 3. Push branch
git push origin feature/new-functionality

# 4. Create Pull Request on GitHub
# 5. Review and merge
# 6. Delete feature branch
git checkout main
git pull origin main
git branch -d feature/new-functionality

2.6 Python-Specific Git Practices

2.6.1 .gitignore for Python

Create .gitignore file:

# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
env/
venv/
.venv/
.ENV/

# IDEs
.vscode/
.idea/
*.swp
*.swo

# OS
.DS_Store
Thumbs.db

# Project specific
docs/
.pytest_cache/

2.6.2 Commit Message Conventions

Use clear, descriptive commit messages:

# Good
git commit -m "feat: add user authentication system"
git commit -m "fix: resolve login validation bug"
git commit -m "docs: update API documentation"

# Bad
git commit -m "update stuff"
git commit -m "fix bug"
git commit -m "changes"

2.6.3 Common Prefixes:

  • feat: - New feature
  • fix: - Bug fix
  • docs: - Documentation
  • style: - Code formatting
  • refactor: - Code restructuring
  • test: - Adding tests
  • chore: - Maintenance tasks

2.7 Practical Exercise

Let’s practice with a real Python project:

# Create a simple calculator
# File: calculator.py

def add(a: float, b: float) -> float:
    """Add two numbers."""
    return a + b

def subtract(a: float, b: float) -> float:
    """Subtract b from a."""
    return a - b

def multiply(a: float, b: float) -> float:
    """Multiply two numbers."""
    return a * b

def divide(a: float, b: float) -> float:
    """Divide a by b."""
    if b == 0:
        raise ValueError("Cannot divide by zero")
    return a / b

if __name__ == "__main__":
    print("Calculator Demo")
    print(f"5 + 3 = {add(5, 3)}")
    print(f"5 - 3 = {subtract(5, 3)}")
    print(f"5 * 3 = {multiply(5, 3)}")
    print(f"5 / 3 = {divide(5, 3):.2f}")

2.7.1 Exercise Steps

  1. Initialize repository:

    mkdir python-calculator
    cd python-calculator
    git init
  2. Create calculator.py with the code above

  3. Add and commit:

    git add calculator.py
    git commit -m "feat: add basic calculator functions"
  4. Create GitHub repository and push:

    git remote add origin https://github.com/yourusername/python-calculator.git
    git push -u origin main
  5. Add a feature (create new branch):

    git checkout -b feature/power-function
  6. Add power function to calculator.py:

    def power(a: float, b: float) -> float:
        """Raise a to the power of b."""
        return a ** b
  7. Commit and push:

    git add calculator.py
    git commit -m "feat: add power function"
    git push origin feature/power-function
  8. Create Pull Request on GitHub

2.8 Git Best Practices

  1. Commit often - Small, logical commits
  2. Write clear messages - Explain what and why
  3. Use branches - Keep main branch stable
  4. Review before merging - Use pull requests
  5. Keep history clean - Avoid unnecessary merge commits
  6. Backup regularly - Push to remote frequently

2.9 Troubleshooting Common Issues

2.9.1 Undoing Changes

# Undo unstaged changes
git checkout -- filename.py

# Undo staged changes
git reset HEAD filename.py

# Undo last commit (keep changes)
git reset --soft HEAD~1

# Undo last commit (discard changes)
git reset --hard HEAD~1

2.9.2 Resolving Conflicts

When Git can’t automatically merge:

  1. Open conflicted files
  2. Look for conflict markers: <<<<<<<, =======, >>>>>>>
  3. Edit to resolve conflicts
  4. Remove conflict markers
  5. Add and commit resolved files

2.10 Next Steps

Now that you understand Git and GitHub, let’s dive into Python Syntax and start coding!

2.11 Resources