Intro to Git Version Control

Session Overview

  • Why Version Control?
  • Git Fundamentals
  • Hands-on Practice
  • Essential Commands & Tips

Why Use Version Control?

“Piled Higher and Deeper”, http://www.phdcomics.com

  • Track changes systematically
  • Experiment safely with code
  • Collaborate effectively
  • Maintain history of your work

Git Fundamentals

Changes are saved sequentially.

  • Base version + sequential changes
  • Each change is a “commit”
  • Full history in “.git” directory
  • Multiple versions can coexist

Version Control: The Basics

Different versions can be tracked simultaneously.

  • Track changes to your code
  • Rewind to previous versions
  • Work on multiple versions simultaneously
  • Merge different versions together

Git’s Key Components

  • Repository (.git directory)
    • Stores all versions and history
  • Commit
    • Snapshot of your changes
  • Staging Area
    • Preview changes before committing

Setting Up Git

# Configure your identity
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# Configure your editor
git config --global core.editor "vim"  # or "nano -w", "emacs"

Note

These settings are used to track who made which changes

The Git Workflow

The Git staging area.

  • Edit files in working directory
  • Stage changes with git add
  • Commit changes with git commit

Learn by doing!

Let’s Practice!

Follow along with these commands in your terminal

Creating Your First Repository

# Create and navigate to project directory
cd ~/Desktop
mkdir crypto_notes
cd crypto_notes

# Initialize git repository
git init

# Switch to main branch
git checkout -b main
  • Creates new directory for our project
  • Initializes empty Git repository
  • Creates main branch

.git directory

What do you see when you run ls -a?

Your First Commit

# Create a new file
echo "Public key cryptography relies on mathematical problems that are easy to compute in one direction but difficult to reverse" > rsa_notes.txt

# Check status
git status

# Stage the file
git add rsa_notes.txt

# Commit with message
git commit -m "Initial notes on public key cryptography fundamentals"

Commit Messages

  • Short (<50 chars) and descriptive
  • Complete the sentence: “If applied, this commit will…”

Tracking Changes

image/svg+xml.git FILE1.txt FILE2.txt git commit staging area repository git add FILE2.txt git add FILE1.txt

The Git Commit Workflow

  1. Edit your files
  2. Stage changes (git add)
  3. Commit changes (git commit)
  4. Repeat!

Viewing Changes

# See what's changed
git diff

# See staged changes
git diff --staged

# View history
git log

Practice Time

  1. Add another line to rsa_notes.txt
  2. View the changes with git diff
  3. Stage and commit your changes

Making More Changes

  • Edit rsa_notes.txt again:

    echo "The RSA algorithm specifically uses the difficulty of factoring large numbers into their prime components" >> rsa_notes.txt
  • Check status and differences

    git status
    git diff
  • Stage and commit

    git add rsa_notes.txt
    git status
    git commit -m "Added specific details about RSA's mathematical foundation"

Best Practices

  • Commit related changes together
  • Commit often
  • Write meaningful commit messages
  • Don’t commit generated files
  • Test before you commit

Warning

Remember: you can always check status with git status

Quick Reference Guide

Essential Git Commands:

git status          # Check repository status
git add <file>      # Stage changes
git commit -m ""    # Commit with message
git log            # View history
git diff           # View changes

Tip

Keep this slide handy for future reference!

Common Scenarios

  • Want to undo staged changes?

    git restore --staged <file>
  • Want to discard changes?

    git restore <file>
  • Need to see specific commit?

    git show <commit-hash>

Best Practices Recap

.git git add git commit staging area repository

The Git Staging Area

  • Small, focused commits
  • Clear commit messages
  • Regular commits
  • Check status often
  • Review changes before committing

Next Steps

  • Branch management
  • Remote repositories
  • Collaboration
  • GitHub/GitLab basics