Home GitHub

📅 Week 3 - Day 3: Git Advanced - Branching & Merging

Friday | Duration: 3 hours

🎯 Learning Objectives

  • Understand Git branching workflow
  • Create and switch between branches
  • Merge branches successfully
  • Resolve merge conflicts
  • Practice collaborative Git workflow

🌳 Understanding Branches

What is a Branch?

A branch is an independent line of development. It allows you to work on features without affecting the main codebase.

Why Use Branches?

  • Isolation: Develop features independently
  • Experimentation: Try new ideas without risk
  • Collaboration: Multiple team members work simultaneously
  • Organization: Keep different versions separate
  • Code Review: Review changes before merging to main

Branch Visualization

main:     A---B---C---F---G
               \       /
feature:        D---E

Commits D and E happen on feature branch, then merged back to main

🔧 Essential Git Branch Commands

Command Description
git branch List all local branches
git branch branch-name Create a new branch
git checkout branch-name Switch to a branch
git checkout -b branch-name Create and switch to a new branch
git merge branch-name Merge branch into current branch
git branch -d branch-name Delete a branch (safe)
git branch -D branch-name Force delete a branch
git branch -a List all branches (local and remote)

📝 Creating and Using Branches

Step 1: Check Current Branch

# See which branch you're on $ git branch # Output shows all branches, current branch has * * main

Step 2: Create a New Branch

# Method 1: Create branch $ git branch feature-form-styling # Then switch to it $ git checkout feature-form-styling # Method 2: Create and switch in one command (recommended) $ git checkout -b feature-form-styling

Step 3: Work on Your Branch

# Make changes to files # Then add and commit as usual $ git add . $ git commit -m "Add beautiful form styling with transitions" # Make more commits $ git add . $ git commit -m "Add hover effects to buttons"

Step 4: Push Branch to GitHub

# First time pushing a new branch $ git push -u origin feature-form-styling # After that, just use $ git push

Step 5: Switch Between Branches

# Go back to main $ git checkout main # Go to feature branch $ git checkout feature-form-styling # Or use newer syntax (Git 2.23+) $ git switch main $ git switch feature-form-styling

🔀 Merging Branches

Merging combines changes from one branch into another.

Fast-Forward Merge

Occurs when there are no new commits on the target branch.

Before merge:
main:     A---B
               \
feature:        C---D

After merge:
main:     A---B---C---D
# Switch to main branch $ git checkout main # Merge feature branch $ git merge feature-form-styling # Output: Fast-forward styled-form.css | 150 +++++++++++++++++ 1 file changed, 150 insertions(+)

Three-Way Merge

Occurs when both branches have new commits.

Before merge:
main:     A---B---E
               \
feature:        C---D

After merge:
main:     A---B---E---M
               \   /
feature:        C-D
# Switch to main $ git checkout main # Merge creates a merge commit $ git merge feature-form-styling # Git opens editor for merge commit message # Save and close to complete merge

Delete Branch After Merge

# Delete local branch $ git branch -d feature-form-styling # Delete remote branch $ git push origin --delete feature-form-styling

⚠️ Merge Conflicts

Merge conflicts happen when the same line in a file is changed differently in two branches.

When Conflicts Occur

$ git checkout main $ git merge feature-branch # Output: Auto-merging style.css CONFLICT (content): Merge conflict in style.css Automatic merge failed; fix conflicts and then commit the result.

Conflict Markers in File

/* style.css */ button { padding: 10px 20px; <<<<<<< HEAD background: blue; color: white; ======= background: green; color: #fff; >>>>>>> feature-branch border: none; }

Explanation:

  • <<<<<<< HEAD: Your current branch (main)
  • =======: Divider
  • >>>>>>> feature-branch: Incoming changes

Resolving Conflicts

  1. Open the conflicted file
  2. Choose which version to keep (or combine both)
  3. Remove conflict markers
  4. Add and commit
/* After resolution - choose what you want */ button { padding: 10px 20px; background: green; /* Kept feature-branch version */ color: white; border: none; }
# After fixing conflicts $ git add style.css $ git commit -m "Resolve merge conflict in button styling"

Check Conflict Status

# See conflicted files $ git status Unmerged paths: (use "git add ..." to mark resolution) both modified: style.css # Abort merge if needed $ git merge --abort

🔄 Collaborative Workflow

Feature Branch Workflow

  1. Pull latest changes: git pull origin main
  2. Create feature branch: git checkout -b feature-name
  3. Work and commit: Make multiple commits
  4. Push branch: git push -u origin feature-name
  5. Create Pull Request: On GitHub
  6. Code Review: Team reviews your changes
  7. Merge: After approval, merge to main
  8. Delete branch: Clean up after merge

Best Practices

  • ✅ Use descriptive branch names: feature-user-auth, fix-login-bug
  • ✅ Keep branches short-lived
  • ✅ Commit often with clear messages
  • ✅ Pull from main regularly to avoid conflicts
  • ✅ Test before merging
  • ❌ Don't commit directly to main in team projects
  • ❌ Don't have multiple features in one branch

💻 Main Activity: Branch & Merge Workflow

Task: Practice Git Branching

Create multiple branches for different styling updates

Activity Steps

Part 1: Create Feature Branch for Form Styling

  1. Make sure you're on main and up to date
    $ git checkout main $ git pull origin main
  2. Create new branch for form styling
    $ git checkout -b feature-form-styling
  3. Create styled-form.css (from previous lesson)

    Add your CSS code to the file

  4. Commit changes
    $ git add styled-form.css $ git commit -m "Add comprehensive form styling"
  5. Make another change - update HTML

    Link CSS in your HTML file

    $ git add registration.html $ git commit -m "Link form stylesheet to registration page"
  6. Push branch to GitHub
    $ git push -u origin feature-form-styling

Part 2: Create Another Branch for Button Styles

  1. Go back to main
    $ git checkout main
  2. Create branch for buttons
    $ git checkout -b feature-button-library
  3. Create buttons.html with various button styles

    Create the file with different button examples

  4. Commit
    $ git add buttons.html $ git commit -m "Create button library with multiple styles"
  5. Push to GitHub
    $ git push -u origin feature-button-library

Part 3: Merge Branches

  1. Switch to main
    $ git checkout main
  2. Merge form styling
    $ git merge feature-form-styling

    Message: "Merge form styling into main"

  3. Push updated main
    $ git push origin main
  4. Merge button library
    $ git merge feature-button-library

    Message: "Merge button library into main"

  5. Push again
    $ git push origin main
  6. Delete merged branches
    $ git branch -d feature-form-styling $ git branch -d feature-button-library
  7. Delete remote branches
    $ git push origin --delete feature-form-styling $ git push origin --delete feature-button-library

Part 4: Simulate Merge Conflict

  1. Create two branches from main
    $ git checkout main $ git checkout -b style-update-1
  2. Edit style.css - change button color to blue
    $ git add style.css $ git commit -m "Change button color to blue"
  3. Go back to main
    $ git checkout main
  4. Create another branch
    $ git checkout -b style-update-2
  5. Edit style.css - change SAME button color to green
    $ git add style.css $ git commit -m "Change button color to green"
  6. Merge first branch - no problem
    $ git checkout main $ git merge style-update-1
  7. Try to merge second branch - CONFLICT!
    $ git merge style-update-2
  8. Open style.css and resolve conflict

    Choose one color or combine changes

  9. Complete merge
    $ git add style.css $ git commit -m "Resolve button color conflict"
  10. Clean up
    $ git branch -d style-update-1 $ git branch -d style-update-2

✏️ Practice Exercises

Exercise 1: Feature Development

Create a branch for each feature:

  • Branch 1: Add navigation bar
  • Branch 2: Add footer
  • Branch 3: Add contact form
  • Merge all three into main

Exercise 2: Bug Fix Branch

Practice creating bug fix branches:

  • Create fix-responsive-layout branch
  • Fix any responsive issues
  • Commit with message: "Fix: responsive layout on mobile"
  • Merge to main

Exercise 3: Conflict Resolution

Practice resolving conflicts:

  • Create two branches modifying the same CSS
  • Merge first branch
  • Attempt to merge second branch
  • Resolve the conflict
  • Complete the merge

🎓 Git Branching Cheat Sheet

View branches

$ git branch # Local branches $ git branch -a # All branches $ git branch -r # Remote branches

Create branches

$ git branch branch-name # Create $ git checkout -b branch-name # Create and switch

Switch branches

$ git checkout branch-name # Old way $ git switch branch-name # New way (Git 2.23+)

Merge branches

$ git merge branch-name # Merge into current branch $ git merge --no-ff branch-name # Force merge commit

Delete branches

$ git branch -d branch-name # Safe delete (merged only) $ git branch -D branch-name # Force delete $ git push origin --delete branch-name # Delete remote

Update branch

$ git pull origin main # Get latest from remote $ git fetch # Fetch without merging

Undo merge

$ git merge --abort # Cancel conflicted merge $ git reset --hard HEAD~1 # Undo last merge (careful!)

Compare branches

$ git diff main..feature # See differences $ git log main..feature # See commits

✅ Today's Deliverable

  • Feature branch created for form styling
  • Multiple commits on feature branch
  • Successfully merged branch to main
  • Resolved at least one merge conflict
  • Deleted branches after merging
  • GitHub repository showing branch history
  • Screenshot of git log --graph --oneline --all

Visualize Your Branches

# See branch graph in terminal $ git log --graph --oneline --all --decorate # Example output: * a4b5c6d (HEAD -> main) Merge branch 'feature-form-styling' |\ | * d3e4f5g Add button hover effects | * c2d3e4f Add form styling |/ * b1c2d3e Initial commit