Intermediate Git Course - Tobias Günther

Jul 10, 2024

Intermediate Git Course - Tobias Günther

Overview

  • Instructor: Tobias Günther
  • Goal: Move beyond Git basics, improve workflow, build confidence, and become an advanced Git user
  • Provided by Free Code Camp

Instructor's Background

  • Part of the team behind Tower (Git desktop GUI for Mac and Windows)
  • Over 10 years of experience
  • Helped over 100,000 developers and designers

Key Topics

  1. Perfect Commits
  2. Branching Strategies
  3. Pull Requests
  4. Merge Conflicts
  5. Integrating Branches (Merge vs. Rebase)

Perfect Commits

Adding the Right Changes

  • Commit should include changes from a single topic
  • Use Git's staging area to be selective
  • git add <filename> for specific files
  • git diff to see changes in a file
  • git add -p to add changes at the patch level

Writing a Good Commit Message

  • Subject line: < 80 characters, concise summary
  • If the subject is hard to write concisely, the commit might have too many topics
  • Body of the message: Detailed explanation
    • What’s different?
    • Reason for the change?
    • Special notes?

Branching Strategies

Importance & Conventions

  • Git leaves branching strategy up to the user/team
  • Determine conventions for your team and document them
  • Supports various workflows depending on team size, project type, release strategies

Branching Models

  • Always Be Integrating (Mainline Development) Model
    • Few branches, constant integration
    • Requires high-quality testing environment
  • Complex Branching Model
    • Multiple branch types (e.g., feature, release, production)
    • More structured but depends on team/project needs

Types of Branches

  • Long-Running Branches
    • E.g., main, develop
    • Exist throughout the project lifecycle
    • Usually, changes are merged/rebased
  • Short-Lived Branches
    • Created for specific purposes (features, bug fixes, etc.)
    • Deleted after integration

Popular Branching Strategies

  • GitHub Flow
    • Single long-running branch (main)
    • Separate short-lived branches for active work
  • Git Flow
    • Main branch = current production state
    • Develop branch as integration branch
    • Feature branches from develop, merge back after completion
    • Release branches for testing, bug fixing, and final integration into main

Pull Requests

Basics

  • Provided by Git hosting platforms, not core Git feature
  • Important for code review and collaboration
  • Allows others to review and approve changes
  • Contributing to repositories without write access
    • Fork repository, make changes, open pull request

Merge Conflicts

When They Happen

  • During merges, rebases, cherry-picks, pulls, or stash reapplications
  • Occur when there are contradictory changes
    • E.g., same line of code changed, file modified in one branch and deleted in another

Detection and Resolution

  • Git notifies you of conflicts immediately
  • Use git status to find conflicts
  • Options: Resolve conflict manually or undo merge (git merge --abort)

Resolving Conflicts

  • Edit conflicted files manually in a text editor, or use a graphical merge tool
  • Commit resolved files to indicate completion of conflict resolution

Integrating Branches: Merge vs. Rebase

Merge

  • Combines branches by creating a merge commit
  • Merge commit connects two branches but adds complexity to history

Rebase

  • Replays commits from one branch onto another
  • Results in a linear history without merge commits
  • Rewrites commit history, making changes to already-pushed commits problematic
  • Rule: Do not rewrite commits already pushed to a shared repository
  • Useful for cleaning local commit history

Additional Resources

  • Access Tobias Günther's advanced Git kit
  • Free, includes short videos on advanced Git topics (interactive rebase, branching strategies, merge conflicts, submodules, etc.)