Objectives

Quick Recap from Day 1

Git Basics

  • git init - Initialize repo
  • git add - Stage changes
  • git commit - Save snapshots
  • git status - Check state
  • git log - View history

Remote Operations

  • git remote add - Link remote
  • git push - Upload changes
  • git pull - Download updates
  • git clone - Copy repo

💡 Today: Advanced workflows with branching, merging, and more!

Git Branching

Branches allow parallel development without affecting the main codebase

# Create a new branch
git branch feature-login

# List all branches
git branch

# Switch to a branch
git checkout feature-login

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

# Modern way (Git 2.23+)
git switch feature-dashboard
git switch -c new-feature

# Delete a branch
git branch -d old-branch

🌿 Branches are lightweight pointers to commits

Branch Visualization

C1 C2 C5 main C3 C4 feature-branch Feature branch splits from main at C2

Git Merge

Combine changes from different branches

# Switch to the branch you want to merge INTO
git checkout main

# Merge feature branch into main
git merge feature-login

# View merged branches
git branch --merged

# Delete merged branch
git branch -d feature-login

Types of Merge:

Merge Visualization

Before Merge: main feature After Merge: main M Merge commit (M) combines both branch histories

Merge Conflicts & Resolution

Conflicts occur when the same lines are changed in both branches

# Git marks conflicts in files:
<<<<<<< HEAD (current branch)
let name = "Alice";
const age = 25;
=======
let name = "Bob";
const age = 30;
>>>>>>> feature-branch (incoming changes)

Resolution Steps:

  1. Open conflicted files in your editor
  2. Choose which code to keep (or merge both manually)
  3. Remove conflict markers (<<<, ===, >>>)
  4. git add <resolved-file> - Mark as resolved
  5. git commit - Complete the merge

💡 Use git merge --abort to cancel merge

Git Checkout

Switch branches, commits, or restore files

# Switch branches
git checkout main
git checkout feature-branch

# Create and switch to new branch
git checkout -b new-feature

# Checkout specific commit (detached HEAD)
git checkout abc1234

# Restore a file from another branch
git checkout main -- README.md

# Restore file to last commit state
git checkout -- file.txt

# Modern alternatives (Git 2.23+)
git switch main              # for switching branches
git switch -c new-branch     # create and switch
git restore README.md        # for restoring files
git restore --source=main README.md

Git Rebase: Rewriting History

Rebase replays commits from one branch onto another, creating a linear history

What is Rebase?

Why Use Rebase?

When to Use Rebase

✅ Safe to Rebase:

❌ Never Rebase:

Rebase vs Merge

Merge (3-way): C1 C2 C5 C3 M Rebase (linear): C1 C2 C3' main Merge commits show complete branch history Clean linear history (no merge commit) C3' is C3 replayed on top of C2

Git Rebase Commands

Basic Rebase:

# Update feature branch with latest main
git checkout feature-branch
git rebase main

# If conflicts occur:
# 1. Resolve conflicts in files
# 2. git add <resolved-files>
# 3. git rebase --continue

# Skip current commit
git rebase --skip

# Abort rebase and return to original state
git rebase --abort

💡 Always test after rebase before pushing!

Interactive Rebase

Clean up commit history before pushing

# Edit last 3 commits interactively
git rebase -i HEAD~3

# Options in editor:
# pick   = use commit
# squash = combine with previous
# reword = change commit message
# edit   = stop to amend commit
# drop   = remove commit

Common Use Cases:

Git Reflog - Your Safety Net

git reflog records every HEAD movement - even "lost" commits!

# View reflog history
git reflog

# Sample output:
# abc1234 HEAD@{0}: commit: Added login feature
# def5678 HEAD@{1}: rebase finished
# ghi9012 HEAD@{2}: checkout: moving to main
# jkl3456 HEAD@{3}: commit: Fixed bug
# ...

# Recover a "lost" commit
git checkout abc1234

# Or create a branch from it
git branch recovered-branch abc1234

# Reset to a previous state
git reset --hard HEAD@{2}

💡 Reflog is local only - expires after 90 days by default

Git Revert - Safe Undo

git revert creates a new commit that undoes changes from a previous commit

# Revert a specific commit
git revert abc1234

# Revert without auto-commit (review first)
git revert -n abc1234

# Revert multiple commits
git revert abc1234 def5678

# Revert a merge commit (requires -m flag)
git revert -m 1 merge-commit-hash

# Continue after resolving conflicts
git revert --continue

# Abort revert
git revert --abort

Why Use Git Revert?

💡 Revert is the safest way to undo changes on public branches

Revert vs Reset

git revert

  • Creates new commit
  • Preserves history
  • Safe for public branches
  • Can be pushed normally
  • Traceable undo
  • No data loss
# Safe undo
git revert abc1234

git reset

  • Moves HEAD pointer
  • Rewrites history
  • Dangerous for shared branches
  • Requires force push
  • Can lose commits permanently
  • Local changes only
# Destructive undo
git reset --hard HEAD~1

🛡️ Golden Rule: Use revert for public history, reset only for local changes

🛠️ Hands-on Exercise

Practice Git Workflows!

  1. Create a new repository with an initial commit
  2. Create a feature branch and make 3 commits
  3. Switch to main and make 1 commit (create divergence)
  4. Try merging feature branch into main
  5. Resolve any conflicts that arise
  6. Create another feature branch with 2 commits
  7. Practice rebasing onto main
  8. Use git reflog to view your history
  9. Practice git revert on a commit
  10. Try interactive rebase (git rebase -i) to squash commits

Git Workflow Best Practices

💡 Good Git habits save time and prevent disasters!

📚 Homework

Resources

1 / 22