🎯 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
Step 2: Create a New Branch
Step 3: Work on Your Branch
Step 4: Push Branch to GitHub
Step 5: Switch Between Branches
🔀 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
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
Delete Branch After Merge
⚠️ Merge Conflicts
Merge conflicts happen when the same line in a file is changed differently in two branches.
When Conflicts Occur
Conflict Markers in File
Explanation:
<<<<<<< HEAD: Your current branch (main)=======: Divider>>>>>>> feature-branch: Incoming changes
Resolving Conflicts
- Open the conflicted file
- Choose which version to keep (or combine both)
- Remove conflict markers
- Add and commit
Check Conflict Status
🔄 Collaborative Workflow
Feature Branch Workflow
- Pull latest changes:
git pull origin main - Create feature branch:
git checkout -b feature-name - Work and commit: Make multiple commits
- Push branch:
git push -u origin feature-name - Create Pull Request: On GitHub
- Code Review: Team reviews your changes
- Merge: After approval, merge to main
- 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
- Make sure you're on main and up to date
$ git checkout main $ git pull origin main
- Create new branch for form styling
$ git checkout -b feature-form-styling
- Create styled-form.css (from previous lesson)
Add your CSS code to the file
- Commit changes
$ git add styled-form.css $ git commit -m "Add comprehensive form styling"
- Make another change - update HTML
Link CSS in your HTML file
$ git add registration.html $ git commit -m "Link form stylesheet to registration page" - Push branch to GitHub
$ git push -u origin feature-form-styling
Part 2: Create Another Branch for Button Styles
- Go back to main
$ git checkout main
- Create branch for buttons
$ git checkout -b feature-button-library
- Create buttons.html with various button styles
Create the file with different button examples
- Commit
$ git add buttons.html $ git commit -m "Create button library with multiple styles"
- Push to GitHub
$ git push -u origin feature-button-library
Part 3: Merge Branches
- Switch to main
$ git checkout main
- Merge form styling
$ git merge feature-form-styling
Message: "Merge form styling into main"
- Push updated main
$ git push origin main
- Merge button library
$ git merge feature-button-library
Message: "Merge button library into main"
- Push again
$ git push origin main
- Delete merged branches
$ git branch -d feature-form-styling $ git branch -d feature-button-library
- Delete remote branches
$ git push origin --delete feature-form-styling $ git push origin --delete feature-button-library
Part 4: Simulate Merge Conflict
- Create two branches from main
$ git checkout main $ git checkout -b style-update-1
- Edit style.css - change button color to blue
$ git add style.css $ git commit -m "Change button color to blue"
- Go back to main
$ git checkout main
- Create another branch
$ git checkout -b style-update-2
- Edit style.css - change SAME button color to green
$ git add style.css $ git commit -m "Change button color to green"
- Merge first branch - no problem
$ git checkout main $ git merge style-update-1
- Try to merge second branch - CONFLICT!
$ git merge style-update-2
- Open style.css and resolve conflict
Choose one color or combine changes
- Complete merge
$ git add style.css $ git commit -m "Resolve button color conflict"
- 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-layoutbranch - 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
Create branches
Switch branches
Merge branches
Delete branches
Update branch
Undo merge
Compare branches
✅ 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