Git Workflow for Team Collaboration
Advanced
30 min read
Lesson 4 of 4
Why Use Git?
Git is a distributed version control system that tracks changes in your code. It enables collaboration, maintains history, and provides a safety net for your project.
Key Benefits
- Track every change in your codebase
- Collaborate with multiple developers
- Branch and merge features safely
- Revert to previous versions if needed
- Maintain a complete history of your project
Git Fundamentals
Basic Git Commands
# Initialize a repository
git init
# Clone a repository
git clone https://github.com/username/repo.git
# Check status
git status
# Stage changes
git add . # Add all changes
git add filename.cs # Add specific file
# Commit changes
git commit -m "Your commit message"
# Push to remote
git push origin main
# Pull from remote
git pull origin main
# Create a branch
git branch feature-name
# Switch to branch
git checkout feature-name
# or
git switch feature-name
# Create and switch branch
git checkout -b feature-name
# Merge branch
git merge feature-name
# View branches
git branch
git branch -a # All branches including remote
# Delete branch
git branch -d feature-name
Git Workflow Strategies
1. Git Flow
# Git Flow Branches
main # Production-ready code
develop # Integration branch
feature/* # New features
release/* # Release preparation
hotfix/* # Emergency fixes
# Git Flow Commands
git flow init
git flow feature start feature-name
git flow feature finish feature-name
git flow release start 1.0.0
git flow release finish 1.0.0
git flow hotfix start hotfix-name
git flow hotfix finish hotfix-name
2. GitHub Flow
# GitHub Flow (Simpler)
main # Production-ready code
feature/* # New features (branch from main)
# Workflow Steps
1. Create a branch from main
2. Make changes and commit
3. Push to GitHub
4. Create Pull Request
5. Review and merge
6. Delete the branch
Best Practices
Commit Messages
# Good Commit Messages
feat: Add user authentication
fix: Resolve login bug
docs: Update API documentation
style: Format code according to standards
refactor: Restructure service layer
test: Add unit tests for payment service
chore: Update dependencies
# Bad Commit Messages (Avoid)
Fixed bug
Updated code
Changes
Work in progress
Branch Naming Conventions
# Feature branches
feature/user-authentication
feature/payment-integration
feature/dark-mode
# Bug fix branches
bugfix/login-error
bugfix/display-issue
# Hotfix branches
hotfix/security-patch
hotfix/critical-bug
# Release branches
release/v1.0.0
release/v2.0.0-beta
Pull Request Best Practices
# Pull Request Template
## Description
Brief description of the changes
## Type of Change
- [ ] Bug fix
- [ ] New feature
- [ ] Breaking change
- [ ] Documentation update
## Testing
- [ ] Unit tests added
- [ ] Integration tests added
- [ ] Manual testing performed
## Checklist
- [ ] Code follows style guidelines
- [ ] Self-review completed
- [ ] Comments added for complex code
- [ ] Documentation updated
Common Git Workflows
1. Feature Development
# Start a new feature
git checkout main
git pull origin main
git checkout -b feature/new-feature
# Work on the feature
# ... make changes ...
git add .
git commit -m "feat: Add new feature"
# Push the branch
git push origin feature/new-feature
# Create Pull Request on GitHub
# After review and approval, merge
# Clean up local branch
git checkout main
git pull origin main
git branch -d feature/new-feature
2. Bug Fix
# Start a bug fix
git checkout main
git pull origin main
git checkout -b bugfix/issue-123
# Fix the bug
# ... fix code ...
git add .
git commit -m "fix: Resolve issue #123"
# Push and create PR
git push origin bugfix/issue-123
3. Emergency Hotfix
# Start from main (or production branch)
git checkout main
git pull origin main
git checkout -b hotfix/critical-bug
# Fix urgently
# ... fix code ...
git add .
git commit -m "hotfix: Critical security patch"
# Merge directly to main
git checkout main
git merge hotfix/critical-bug
git push origin main
# Also merge to develop
git checkout develop
git merge hotfix/critical-bug
git push origin develop
Resolving Conflicts
# When conflicts occur during merge
git merge feature/branch
# Resolve conflicts manually in files
# Then mark as resolved
git add resolved-file.cs
# Continue merge
git commit -m "Merge: Resolve conflicts"
# Alternative: Abort merge
git merge --abort
# View conflicts
git diff
git diff --name-only --diff-filter=U
Git Hooks
# .git/hooks/pre-commit (Example)
#!/bin/sh
# Run code formatting before commit
dotnet format --verify-no-changes
if [ $? -ne 0 ]; then
echo "Code formatting issues found. Run 'dotnet format' to fix."
exit 1
fi
# Run tests before commit
dotnet test
if [ $? -ne 0 ]; then
echo "Tests failed. Commit aborted."
exit 1
fi
Useful Git Aliases
# Add to ~/.gitconfig
[alias]
co = checkout
br = branch
ci = commit
st = status
unstage = reset HEAD --
last = log -1 HEAD
tree = log --graph --oneline --decorate --all
uncommit = reset --soft HEAD^
amend = commit --amend --no-edit
pushf = push --force-with-lease
Key Takeaway
- Git is essential for team collaboration
- Choose a workflow that fits your team
- Write clear commit messages
- Use pull requests for code review
- Resolve conflicts carefully
Exercise
Practice Git workflow:
- Initialize a git repository
- Create a feature branch
- Make multiple commits with good messages
- Create a pull request
- Simulate a merge conflict and resolve it
- Use git hooks for quality control