Skip to article frontmatterSkip to article content

Version Control with Git and GitHub

Mastering Modern Development Workflows

Utah Valley University

Version Control with Git and GitHub

Welcome to the world of modern software collaboration! Today we’ll explore how Git and GitHub revolutionize the way developers work together.

Why Version Control?

The Problem
The Solution
“If that doesn’t fix it, git.txt contains the phone number of a friend of mine who understands git. Just wait through a few minutes of ‘It’s really pretty simple, just think of branches as...’ and eventually you’ll learn the commands that will fix everything.” - XKCD

Figure 1:“If that doesn’t fix it, git.txt contains the phone number of a friend of mine who understands git. Just wait through a few minutes of ‘It’s really pretty simple, just think of branches as...’ and eventually you’ll learn the commands that will fix everything.” - XKCD

Before Version Control:

  • project_final.zip
  • project_final_v2.zip
  • project_final_REALLY_final.zip
  • project_final_use_this_one.zip

Git vs GitHub: Understanding the Difference

Git - Local Version Control

Local Version Control System

  • Runs on your computer
  • Tracks file changes
  • Manages project history
  • Works offline
  • Command-line tool
GitHub - Cloud Platform

Cloud-Based Git Hosting

  • Remote repository storage
  • Web-based interface
  • Collaboration features
  • Issue tracking
  • Project management tools

Git Fundamentals

The Three Trees of Git

Git organizes your work into three main areas, each serving a specific purpose in the version control workflow.

Working Directory

text-white

Your current project files

What it contains:

  • Modified files
  • New files
  • Deleted files
Staging Area

text-white

Prepared changes for commit

What it contains:

  • Files ready to be committed
  • Snapshot of selected changes
Repository

text-white

Permanent project history

What it contains:

  • All committed changes
  • Complete project timeline
  • Branch information

Essential Git Commands

Table 1:Git Command Reference

CommandPurposeExampleWhen to Use
git initInitialize repositorygit init my-projectStarting a new project
git cloneCopy remote repositorygit clone https://github.com/user/repo.gitWorking on existing project
git addStage changesgit add filename.txtBefore committing changes
git commitSave changes permanentlygit commit -m "Add new feature"After staging changes
git pushUpload to remotegit push origin mainSharing your work
git pullDownload from remotegit pull origin mainGetting latest changes

Hands-On Exercise: Your First Repository

:class: important

Follow along as we create your first Git repository. Open your terminal and let’s go!


### Step 1: Initial Setup

```{code-block} bash
:caption: Configure Git (do this once per computer)
:linenos:

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

# Check your configuration
git config --list

:class: tip

Use the same email address you’ll use for GitHub to link your commits to your GitHub profile!


### Step 2: Create Your First Repository

```{code-block} bash
:caption: Initialize a new repository
:linenos:
:emphasize-lines: 3,6

# Create a new directory
mkdir my-first-repo
cd my-first-repo

# Initialize Git repository
git init

# Check status
git status
Expected Output
$ git init
Initialized empty Git repository in /path/to/my-first-repo/.git/

$ git status
On branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track)

Step 3: Add and Commit Files

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Create a README file
echo "# My First Repository" > README.md
echo "This is my introduction to Git!" >> README.md

# Check what changed
git status

# Stage the file
git add README.md

# Check status again
git status

# Commit the changes
git commit -m "Initial commit: Add README"

Create and track your first file

🌿 Understanding Branches

Branching Workflow Visualization

Common Branching Commands

Creating Branches
Working with Branches
Cleaning Up
# Create a new branch
git branch feature/new-feature

# Switch to the branch
git checkout feature/new-feature

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

# List all branches
git branch

Branch creation and switching

🐙 GitHub: Collaboration Supercharged

Repository Features

Issues & Project Management

text-white

  • Bug tracking
  • Feature requests
  • Project boards
  • Milestone planning
## Bug Report

**Description:** Brief description of the bug

**Steps to Reproduce:**
1. Go to '...'
2. Click on '....'
3. See error

**Expected behavior:** What should happen
**Actual behavior:** What actually happens

Sample Issue Template

Pull Requests

text-white

  • Code review process
  • Discussion threads
  • Automated testing
  • Merge management
## Changes Made
- [ ] Added login functionality
- [ ] Updated documentation  
- [ ] Added unit tests

## Testing
- All tests pass ✅
- Manual testing completed ✅

Sample PR Template

GitHub Workflow Exercise

  1. Add a README file with your name and course information
  2. Push your changes:
    git add README.md
    git commit -m "Add personal README"
    git push origin main

## 🤝 Collaboration Workflows

### The GitHub Flow

```{mermaid}
graph TD
    A[Create Branch] --> B[Make Changes]
    B --> C[Create Pull Request]
    C --> D[Code Review]
    D --> E{Approved?}
    E -->|Yes| F[Merge to Main]
    E -->|No| B
    F --> G[Deploy]
    
    style A fill:#e1f5fe
    style F fill:#c8e6c9
    style G fill:#fff3e0

Best Practices for Collaboration

Commit Message Best Practices

Good commit messages are:

type(scope): short description

Longer description explaining the change
and why it was necessary.

- Bullet points for multiple changes
- Reference issues: Fixes #123

Commit Message Format

Examples:

feat(auth): add user login functionality

fix(api): resolve null pointer exception in user service

docs(readme): update installation instructions

test(auth): add unit tests for login validation

Good Commit Messages

Pull Request Workflow

Step-by-Step PR Process:

  1. Create a feature branch:

    git checkout -b feature/user-authentication
  2. Make your changes and commit:

    git add .
    git commit -m "feat(auth): implement user login system"
  3. Push to GitHub:

    git push origin feature/user-authentication
  4. Create Pull Request on GitHub

  5. Request reviewers

  6. Address feedback

  7. Merge when approved

🚨 Common Git Scenarios & Solutions

Merge Conflicts
Undoing Changes
Viewing History

Example conflict:

<<<<<<< HEAD
function greet() {
    return "Hello World!";
}
=======
function greet() {
    return "Hi there!";
}
>>>>>>> feature-branch

Merge conflict in code

Resolution steps:

  1. Open the conflicted file
  2. Choose which version to keep (or combine them)
  3. Remove conflict markers (<<<<<<<, =======, >>>>>>>)
  4. Add and commit the resolved file

Interactive Quiz

Quiz Answers

Answer 1: B) git checkout -- filename - This discards uncommitted changes in the working directory.

Answer 2: A) git status - Shows the status of files in working directory and staging area.

Answer 3: D) All approaches can work, but the choice depends on your team’s workflow preferences:

  • git merge main creates a merge commit
  • git rebase main replays your commits on top of main
  • git pull origin main gets latest changes first

Advanced GitHub Features

GitHub Actions (CI/CD Preview)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
name: Test and Deploy

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Setup Node.js
      uses: actions/setup-node@v2
      with:
        node-version: '16'
    - run: npm install
    - run: npm test

Simple GitHub Actions workflow

Project Management with GitHub

GitHub Project Features

FeaturePurposeBest For
IssuesTrack bugs and featuresTask management
ProjectsKanban-style boardsSprint planning
MilestonesGroup related issuesRelease planning
DiscussionsCommunity conversationsQ&A and brainstorming

Practice Challenges

📚 Resources & Next Steps

Essential Resources

Cheat Sheets

📄 Git Command Cheat Sheet

Repository Setup:

git init                    # Initialize repository
git clone <url>            # Clone remote repository

Basic Workflow:

git status                 # Check status
git add <file>            # Stage file
git add .                 # Stage all files
git commit -m "message"   # Commit changes
git push origin main      # Push to remote
git pull origin main      # Pull from remote

Branching:

git branch                # List branches
git branch <name>         # Create branch
git checkout <name>       # Switch branch
git checkout -b <name>    # Create and switch
git merge <branch>        # Merge branch
git branch -d <name>      # Delete branch

## **Key Takeaways**

```{admonition} 🏁 What You've Learned Today
:class: success

✅ **Version Control Fundamentals**
- Why version control is essential for software development
- The difference between Git and GitHub

✅ **Git Basics**
- Essential Git commands and workflows
- The three trees: Working Directory, Staging Area, Repository

✅ **Branching Strategies**
- How to create, switch, and merge branches
- Best practices for branch naming and management

✅ **GitHub Collaboration**
- Pull Request workflow
- Code review process
- Project management features

✅ **Real-World Skills**
- Handling merge conflicts
- Writing good commit messages
- Using GitHub for team collaboration

🔜 Coming Up Next

In our next lecture, we’ll explore the Software Development Life Cycle (SDLC) and see how version control fits into the bigger picture of software engineering processes.


“Git is not just a tool, it’s a way of thinking about collaboration and code evolution.”