Real-world Scenarios

Apply Git knowledge to realistic development situations

Learning Objectives

  • Handle complex merge and rebase scenarios
  • Manage feature releases and hotfixes
  • Coordinate with multiple team members
  • Recover from common real-world issues

Scenario Overview

These scenarios simulate real-world development situations you're likely to encounter. Each scenario combines multiple Git concepts and requires careful planning and execution.

Scenario 1: Feature Release Coordination

Situation

Your team is preparing a major release that includes three features. Two are complete, but one needs more testing. Meanwhile, a critical bug is reported in production that needs immediate attention.

Initial State:

Repository Setup
# Current branches:
# - main (production)
# - develop
# - feature/user-auth (complete)
# - feature/payment-api (complete)
# - feature/reporting (needs testing)
# Create test repository
$ git init release-coordination
$ cd release-coordination
$ touch app.js && git add app.js && git commit -m "Initial commit"
# Setup branches
$ git checkout -b develop
$ git checkout -b feature/user-auth
$ echo "Auth implementation" > auth.js
$ git add auth.js && git commit -m "Implement user authentication"
$ git checkout develop
$ git checkout -b feature/payment-api
$ echo "Payment API" > payment.js
$ git add payment.js && git commit -m "Implement payment API"
$ git checkout develop
$ git checkout -b feature/reporting
$ echo "Reports" > reports.js
$ git add reports.js && git commit -m "Add reporting feature"

Tasks:

  1. Create a release branch for the complete features
  2. Handle the critical bug fix
  3. Merge completed features while keeping reporting separate
  4. Update all branches with the bug fix

Solution:

Feature Release Commands
# Create release branch
$ git checkout develop
$ git checkout -b release/v2.0
$ git merge feature/user-auth
$ git merge feature/payment-api
# Handle critical bug
$ git checkout main
$ git checkout -b hotfix/critical-bug
$ echo "Bug fix" > fix.js
$ git add fix.js
$ git commit -m "Fix critical production issue"
# Merge hotfix to main
$ git checkout main
$ git merge hotfix/critical-bug
$ git tag -a v1.0.1 -m "Hotfix release"
# Update develop and release branches
$ git checkout develop
$ git merge hotfix/critical-bug
$ git checkout release/v2.0
$ git merge hotfix/critical-bug
# Continue with release
$ git checkout main
$ git merge release/v2.0
$ git tag -a v2.0 -m "Major release with auth and payments"

Scenario 2: Conflict Resolution Sprint

Situation

Multiple team members have been working on related features that affect the same configuration files. You need to merge all changes while ensuring the configuration remains valid.

Setup:

Conflict Scenario Setup
# Setup test repository
$ git init config-conflicts
$ cd config-conflicts
# Create base config
$ cat > config.json << EOL
{
"api": {
"endpoint": "https://api.example.com",
"timeout": 30,
"retries": 3
}
}
EOL
$ git add config.json && git commit -m "Initial config"
# Team member 1 changes
$ git checkout -b feature/timeout-updates
$ sed -i 's/"timeout": 30/"timeout": 60/g' config.json
$ git commit -am "Increase API timeout"
# Team member 2 changes
$ git checkout main
$ git checkout -b feature/retry-policy
$ sed -i 's/"retries": 3/"retries": 5,
"backoff": "exponential"/g' config.json
$ git commit -am "Update retry policy"
# Team member 3 changes
$ git checkout main
$ git checkout -b feature/endpoint-update
$ sed -i 's/api.example.com/api.newdomain.com/g' config.json
$ git commit -am "Update API endpoint"

Resolution Process:

Conflict Resolution
# Create integration branch
$ git checkout -b integration/config-updates main
# Merge each feature with manual resolution
$ git merge feature/timeout-updates
# Resolve conflicts in config.json
$ git add config.json
$ git commit -m "Merge timeout updates"
$ git merge feature/retry-policy
# Resolve conflicts in config.json
$ git add config.json
$ git commit -m "Merge retry policy"
$ git merge feature/endpoint-update
# Resolve conflicts in config.json
$ git add config.json
$ git commit -m "Merge endpoint update"
# Validate final config
$ node -e "JSON.parse(require('fs').readFileSync('config.json'))"
# Update main
$ git checkout main
$ git merge integration/config-updates

Final Config:

Resolved config.json
{
"api": {
"endpoint": "https://api.newdomain.com",
"timeout": 60,
"retries": 5,
"backoff": "exponential"
}
}

Scenario 3: Repository Recovery

Situation

A team member accidentally force-pushed an old version of the develop branch, losing several important commits. You need to recover the lost work and establish safeguards.

Recovery Steps:

Repository Recovery
# Check reflog for lost commits
$ git reflog show develop
# Create recovery branch
$ git checkout -b recovery/develop develop@{1}
# Verify lost commits
$ git log --oneline recovery/develop
# Reset develop to correct state
$ git checkout develop
$ git reset --hard recovery/develop
# Setup branch protection
$ git config branch.develop.allowforce false
$ git config receive.denyNonFastForwards true
# Create backup refs
$ git tag backup/develop-$(date +%Y%m%d) develop
# Document incident
$ git notes add -m "Recovered from accidental force-push on $(date)"

Prevention Measures:

  • Configure branch protection rules
  • Set up automated backups
  • Document recovery procedures

Real-world Best Practices

  • Always create backup branches before complex operations
  • Communicate with team members before major branch operations
  • Maintain a clear branching strategy and document exceptions
  • Set up automated testing for critical configuration files