Collaboration
Master team collaboration workflows, pull requests, code review, and distributed development with Git.
Team Collaboration Basics
Setting Up for Collaboration
# Clone team repository
git clone https://github.com/team/project.git
cd project
# Set up remotes
git remote add upstream https://github.com/original/project.git
git remote add colleague https://github.com/colleague/project.git
# View remotes
git remote -v
# Configure user for team project
git config user.name "Your Team Name"
git config user.email "team.email@company.com"
Understanding Collaboration Workflows
Centralized Workflow:
- Everyone pushes to main branch
- Simple but can cause conflicts
- Good for small teams
Feature Branch Workflow:
- Each feature developed in separate branch
- Merge back to main when complete
- Most common approach
Forking Workflow:
- Each developer has own fork
- Contribute via pull requests
- Common for open source
Pull Request Workflow
Creating Pull Requests
# 1. Create feature branch
git checkout -b feature/user-authentication
git push -u origin feature/user-authentication
# 2. Make changes and commit
git add .
git commit -m "Implement user authentication system"
# 3. Push changes
git push origin feature/user-authentication
# 4. Create pull request via web interface
# Include: title, description, reviewers, labels
Pull Request Best Practices
Good Pull Request Structure:
## Summary
Brief description of changes
## Changes
- Added user authentication system
- Implemented login/logout functionality
- Added password validation
- Updated user interface
## Testing
- [ ] Unit tests added
- [ ] Integration tests pass
- [ ] Manual testing completed
## Related Issues
Fixes #123
Closes #456
## Screenshots
[Include relevant screenshots]
Pull Request Commands:
# Checkout someone else's pull request
git fetch origin pull/123/head:pr-123
git checkout pr-123
# Alternative using GitHub CLI
gh pr checkout 123
# Review changes
git diff main..pr-123
git log main..pr-123 --oneline
Code Review Process
Reviewing Code
# Fetch latest changes
git fetch origin
# Checkout PR branch
git checkout pr-branch
# Review changes
git diff main..pr-branch
git log main..pr-branch --oneline
# Test changes locally
npm test
npm run build
# Leave review comments via web interface
Review Checklist
Code Quality:
- Code follows team style guidelines
- Functions are well-named and documented
- No obvious bugs or security issues
- Error handling is appropriate
- Performance considerations addressed
Testing:
- Tests cover new functionality
- Tests are well-written and maintainable
- All tests pass
- Edge cases are considered
Documentation:
- Code is self-documenting
- Complex logic is commented
- API documentation updated
- README updated if needed
Addressing Review Feedback
# Make requested changes
git add .
git commit -m "Address review feedback: improve error handling"
# Force push if you amended commits
git push --force-with-lease origin feature/branch
# Respond to review comments
# Thank reviewers for feedback
# Explain reasoning for changes
Merge Strategies
Fast-Forward Merge
# When feature branch is ahead of main
git checkout main
git merge feature/simple-fix
# Result: linear history
Merge Commit
# Create merge commit to preserve branch history
git checkout main
git merge --no-ff feature/complex-feature
# Result: branch history preserved
Squash Merge
# Combine all commits into one
git checkout main
git merge --squash feature/multiple-commits
git commit -m "Implement feature with multiple improvements"
# Result: clean single commit
Rebase and Merge
# Rebase feature branch onto main
git checkout feature/branch
git rebase main
# Then merge
git checkout main
git merge feature/branch
# Result: linear history without merge commits
Conflict Resolution
Understanding Conflicts
Conflicts occur when:
- Same lines modified in different branches
- File deleted in one branch, modified in another
- Binary files changed differently
Resolving Merge Conflicts
# Start merge that creates conflicts
git merge feature/conflicting-branch
# View conflicted files
git status
# Edit files to resolve conflicts
# Remove conflict markers:
<<<<<<< HEAD
// Your changes
=======
// Their changes
>>>>>>> feature/conflicting-branch
# Stage resolved files
git add resolved-file.js
# Complete merge
git commit
Conflict Resolution Tools
# Use merge tool
git mergetool
# Configure merge tool
git config --global merge.tool vscode
git config --global merge.tool kdiff3
# Manual resolution commands
git checkout --ours conflicted-file.js # Keep your version
git checkout --theirs conflicted-file.js # Keep their version
Preventing Conflicts
# Keep feature branch updated
git checkout feature/branch
git fetch origin
git rebase origin/main
# Communicate with team
# Coordinate on overlapping changes
# Use feature flags for long-running features
Team Workflows
GitHub Flow
# 1. Create branch from main
git checkout main
git pull origin main
git checkout -b feature/improvement
# 2. Make changes
git add .
git commit -m "Implement improvement"
# 3. Push and create PR
git push origin feature/improvement
# Create PR via web interface
# 4. Review and merge
# 5. Delete branch
git branch -d feature/improvement
Git Flow
# Main branches
main # Production releases
develop # Integration branch
# Feature development
git checkout develop
git checkout -b feature/new-feature
# Develop feature
git checkout develop
git merge --no-ff feature/new-feature
git branch -d feature/new-feature
# Release preparation
git checkout develop
git checkout -b release/v1.2.0
# Prepare release
git checkout main
git merge --no-ff release/v1.2.0
git tag -a v1.2.0 -m "Version 1.2.0"
git checkout develop
git merge --no-ff release/v1.2.0
Feature Branch Workflow
# Create feature branch
git checkout -b feature/user-profile
# Work on feature
git add .
git commit -m "Add user profile page"
# Push to remote
git push origin feature/user-profile
# Create pull request
# Review and merge
# Clean up
git checkout main
git pull origin main
git branch -d feature/user-profile
Communication and Documentation
Commit Message Standards
Conventional Commits:
# Format: <type>(<scope>): <description>
feat(auth): add OAuth2 integration
fix(ui): resolve button alignment issue
docs(api): update endpoint documentation
style(css): fix code formatting
refactor(core): simplify data processing
test(unit): add user validation tests
chore(deps): update dependencies
Detailed Commit Messages:
# Use imperative mood
git commit -m "Add user authentication system
- Implement login/logout functionality
- Add password validation
- Update user interface
- Add unit tests for auth module
Fixes #123"
Issue Tracking Integration
# Link commits to issues
git commit -m "Fix login bug
This commit resolves the login issue where users
couldn't authenticate with special characters.
Fixes #456"
# Use keywords for auto-closing
# Closes #123, Fixes #456, Resolves #789
Pull Request Templates
GitHub Pull Request Template:
<!-- .github/pull_request_template.md -->
## Description
Brief description of changes
## Type of Change
- [ ] Bug fix
- [ ] New feature
- [ ] Breaking change
- [ ] Documentation update
## Testing
- [ ] Unit tests added/updated
- [ ] Integration tests pass
- [ ] Manual testing completed
## Checklist
- [ ] Code follows style guidelines
- [ ] Self-review completed
- [ ] Documentation updated
- [ ] Tests added/updated
Branch Management
Branch Naming Conventions
# Feature branches
feature/user-authentication
feature/payment-integration
feat/shopping-cart
# Bug fixes
bugfix/login-error
fix/payment-validation
hotfix/security-issue
# Releases
release/v1.2.0
release/2023-q4
# Experiments
experiment/new-ui
poc/microservices
Branch Protection
GitHub Branch Protection Rules:
# Protect main branch
- Require pull request reviews
- Dismiss stale reviews
- Require status checks
- Require up-to-date branches
- Restrict pushes to specific users
Branch Cleanup
# Delete merged branches
git branch --merged main | grep -v "main\|develop" | xargs -n 1 git branch -d
# Delete remote tracking branches
git remote prune origin
# Clean up local branches
git checkout main
git pull origin main
git branch -d feature/completed-feature
Collaboration Tools
GitHub CLI
# Install GitHub CLI
# macOS: brew install gh
# Linux: apt install gh
# Authenticate
gh auth login
# Create repository
gh repo create my-project
# Clone repository
gh repo clone username/repository
# Create pull request
gh pr create --title "Add feature" --body "Description"
# Review pull request
gh pr checkout 123
gh pr review 123 --approve
gh pr merge 123
Git Hooks for Teams
Pre-commit Hook:
#!/bin/sh
# .git/hooks/pre-commit
# Run tests
npm test
if [ $? -ne 0 ]; then
echo "Tests failed. Fix before committing."
exit 1
fi
# Run linting
npm run lint
if [ $? -ne 0 ]; then
echo "Linting failed. Fix before committing."
exit 1
fi
Commit-msg Hook:
#!/bin/sh
# .git/hooks/commit-msg
# Check commit message format
commit_regex='^(feat|fix|docs|style|refactor|test|chore)(\(.+\))?: .{1,50}'
if ! grep -qE "$commit_regex" "$1"; then
echo "Invalid commit message format."
echo "Use: type(scope): description"
exit 1
fi
Advanced Collaboration
Distributed Workflows
# Multiple remotes workflow
git remote add upstream https://github.com/original/project.git
git remote add colleague https://github.com/colleague/project.git
# Fetch from all remotes
git fetch --all
# Cherry-pick from colleague's branch
git cherry-pick colleague/feature-branch
# Merge from upstream
git merge upstream/main
Pair Programming with Git
# Co-authored commits
git commit -m "Implement feature X
Co-authored-by: Colleague Name <colleague@email.com>"
# Switching between authors
git config user.name "Pair Programming"
git config user.email "pair@company.com"
Code Review Automation
GitHub Actions for PR Review:
# .github/workflows/pr-review.yml
name: PR Review
on: pull_request
jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run tests
run: npm test
- name: Run linting
run: npm run lint
- name: Security scan
run: npm audit
Troubleshooting Team Issues
Common Collaboration Problems
-
Merge conflicts
# Prevention
git pull --rebase origin main
# Resolution
git mergetool
git add resolved-files
git commit -
Accidental commits to main
# Move commits to feature branch
git branch feature/accidental-work
git reset --hard HEAD~3
git checkout feature/accidental-work -
Wrong author in commits
# Fix last commit
git commit --amend --author="Correct Name <correct@email.com>"
# Fix multiple commits
git rebase -i HEAD~3
# Use 'edit' for commits to fix
# Then for each commit:
git commit --amend --author="Correct Name <correct@email.com>"
git rebase --continue
Team Communication
Daily Standup Integration:
# Show recent work
git log --oneline --since="yesterday" --author="Your Name"
# Show current work
git status
git branch --show-current
# Show what you're working on
git log --oneline -5
Sprint Planning:
# Create milestone branch
git checkout -b sprint/week-12
# Track progress
git log --oneline --grep="US-123"
# Merge completed work
git merge feature/user-story-123
Best Practices
For Individuals
- Write clear commit messages - Help reviewers understand changes
- Keep branches small - Easier to review and merge
- Test before pushing - Don't break others' work
- Respond to feedback - Address review comments promptly
- Stay up to date - Sync with main branch regularly
For Teams
- Establish workflows - Document and follow consistently
- Use branch protection - Prevent direct pushes to main
- Require code review - Maintain quality and share knowledge
- Automate testing - Catch issues early
- Communicate effectively - Use issues, PRs, and documentation
For Organizations
- Standardize workflows - Consistency across teams
- Provide training - Ensure Git competency
- Use automation - Reduce manual overhead
- Monitor metrics - Track team performance
- Iterate and improve - Continuously refine processes
Integration with Development Tools
IDE Integration
VS Code Git Features:
- Built-in Git support
- Visual diff and merge
- Branch switching
- Commit history
- Pull request integration
JetBrains IDE Git Features:
- VCS integration
- Local history
- Merge tool
- Annotate (blame)
- Git flow support
Project Management Integration
Jira Integration:
# Link commits to Jira issues
git commit -m "PROJ-123: Fix login bug
This commit resolves the authentication issue
described in PROJ-123."
Trello Integration:
# Link commits to Trello cards
git commit -m "Fix user profile bug
References: https://trello.com/c/abc123"
See Workflows for detailed workflow strategies and Advanced Git for sophisticated collaboration techniques.