Branches and Workflow
Level: Intermediate
ℹ️ What You'll Learn
- Create branches
- Switch between branches
- Merge branches
- Resolve conflicts
- Branching strategies
Create Branch
git branch feature/student-management
git checkout feature/student-management
# Or shorthand:
git checkout -b feature/student-management
List branches:
git branch
# main
# * feature/student-management
Branching Strategy
SMS project structure:
main (production)
├── feature/student-management
│ ├── Add Student model
│ ├── Add StudentService
│ └── Add API controller
├── feature/fee-module
│ ├── Add Fee model
│ └── Add FeeCalculator
└── develop (integration)
├── All features merge here
└── Test before main
Common strategies:
- Git Flow: main, develop, feature/, release/, hotfix/*
- GitHub Flow: main, feature/*, PR required
- Trunk-based: main, very short-lived feature branches
Switch Branch
git checkout main # Switch to main
git checkout feature/fee-module # Switch to fee feature
Working directory updates automatically.
Merge Branch
Feature complete. Merge to main:
git checkout main
git merge feature/student-management
Merge commits appear in history:
* Merge branch 'feature/student-management' into main
|\
| * Add StudentService
| * Add Student model
|/
* Initial commit
Resolve Conflicts
Two branches modify same file:
# merge conflict
git merge feature/fee-module
# Auto-merge failed
# Fix conflicts and commit
File shows conflict markers:
public decimal CalculateFees(int id)
{
<<<<<<< HEAD
return ClassRate - Discount; // main version
=======
return ClassRate - Discount - Scholarship; // feature version
>>>>>>> feature/fee-module
}
Edit to choose correct version, then:
git add StudentFees.cs
git commit -m "Resolve merge conflict in fee calculation"
Rebase (Alternative to Merge)
Keep history linear:
git checkout feature/exam-module
git rebase main
# Apply exam-module commits on top of main
Result: Single line history instead of merge commit.
SMS Workflow Example
# 1. Create feature branch
git checkout -b feature/exam-management
# 2. Work on exam feature
# Add Exam model
# Add ExamService
# Add ExamController
# Write tests
# 3. Commit changes
git add .
git commit -m "Add exam management system"
# 4. Push to server
git push origin feature/exam-management
# 5. Create Pull Request (on GitHub)
# Team review
# 6. Merge to main
git checkout main
git pull
git merge feature/exam-management
# 7. Delete feature branch
git branch -d feature/exam-management
Key Takeaways
- Branch = isolated development
- Merge = combine branches
- Conflicts = resolve manually
- Strategy = organize workflow
💡 Branch Naming
Use prefixes: feature/, bugfix/, hotfix/, release/ Example: feature/student-registration, bugfix/fee-calculation-error
🤖Use AI to Learn Faster
Use ChatGPT, Claude, or Copilot to go deeper on Git Branches. Try these prompts:
"When should I create a new branch?""How do I resolve merge conflicts?""What's the difference between merge and rebase?""Quiz me on branching"
💡 Tip: After reading this article, paste your own code into AI and ask "What could go wrong here and why?" — fastest way to find edge cases and deepen understanding.
nexcoding.in
Have questions on your tech stack, ongoing projects, or need one-to-one training?