COMP 305: Object-Oriented Software Design

University of San Diego, Fall 2025

Git

Git is a distributed version control system that tracks changes in source code during software development. It enables collaboration, maintains history, and allows multiple developers to work on the same project simultaneously without conflicts.

Why Git?

Interactive Git Workflow Simulator

Simulate common Git operations and see how your repository state changes:

Git Commands

Repository State

Command Output

Essential Git Commands

Setup & Configuration

# Configure Git (first time setup)
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# Check configuration
git config --list

# Initialize a new repository
git init

# Clone an existing repository
git clone https://github.com/username/repo.git

Basic Workflow

# Check status of your files
git status

# Add files to staging area
git add filename.txt           # Add specific file
git add .                      # Add all files
git add *.js                   # Add all .js files

# Commit changes
git commit -m "Your commit message"

# Commit with detailed message
git commit -m "Short description" -m "Longer explanation of changes"

# View commit history
git log
git log --oneline              # Condensed view
git log --graph --all          # Visual branch graph

Branching & Merging

# Create a new branch
git branch feature-name

# Switch to a branch
git checkout feature-name

# Create and switch in one command
git checkout -b feature-name

# Modern alternative (Git 2.23+)
git switch feature-name
git switch -c feature-name

# List branches
git branch                     # Local branches
git branch -a                  # All branches (local + remote)

# Merge a branch
git checkout main
git merge feature-name

# Delete a branch
git branch -d feature-name     # Safe delete
git branch -D feature-name     # Force delete

Working with Remotes

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

# View remotes
git remote -v

# Push to remote
git push origin main           # Push main branch
git push -u origin main        # Push and set upstream

# Pull from remote
git pull origin main

# Fetch changes without merging
git fetch origin

# Push a new branch
git push -u origin feature-name

Undoing Changes

# Undo changes in working directory
git checkout -- filename.txt   # Discard changes to file

# Unstage a file
git reset filename.txt

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

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

# Amend last commit
git commit --amend -m "New message"

# Revert a commit (creates new commit)
git revert commit-hash

# Stash changes temporarily
git stash
git stash list
git stash apply
git stash pop

Advanced Commands

# Cherry-pick a specific commit
git cherry-pick commit-hash

# Rebase (reapply commits on top of another branch)
git rebase main

# Interactive rebase (edit commit history)
git rebase -i HEAD~3

# Show changes
git diff                       # Working directory changes
git diff --staged              # Staged changes
git diff main feature          # Compare branches

# Search commits
git log --grep="bug fix"       # Search commit messages
git log -S "function name"     # Search code changes

# Tags
git tag v1.0.0                 # Create lightweight tag
git tag -a v1.0.0 -m "Version 1.0.0"  # Annotated tag
git push origin v1.0.0         # Push tag

Git Workflow Strategies

Feature Branch Workflow

Gitflow

GitHub Flow

Best Practices

Commit Message Guidelines

Follow the conventional commits specification:

feat: add user authentication
fix: resolve login redirect bug
docs: update API documentation
style: format code with prettier
refactor: simplify validation logic
test: add unit tests for auth service
chore: update dependencies

# Detailed example:
feat: implement password reset functionality

- Add password reset email template
- Create reset token generation
- Add expiration handling for tokens
- Update user model with resetToken field

Closes #123

Common Pitfalls to Avoid

Useful Git Aliases

Add these to your ~/.gitconfig to save time:

[alias]
    st = status
    co = checkout
    br = branch
    ci = commit
    unstage = reset HEAD --
    last = log -1 HEAD
    visual = log --graph --oneline --all
    aliases = config --get-regexp alias

Resources