IntermediateLesson 3 of 9

Repositories & Branching

Design clean repository structure and apply practical branching strategy using main plus short-lived feature branches.

Simple Explanation (ELI5)

A repository is your project’s house. Branches are separate rooms where people can work without stepping on each other. Main is the living room everyone shares, feature branches are private rooms for changes.

Technical Explanation

A repository contains source code, commit history, refs (branches/tags), and metadata. Branches are movable pointers to commits. Team safety comes from branch rules: main protected, feature branches short-lived, PR required for merges, and checks enforced before integration.

Repository Structure

PathPurpose
README.mdProject overview and setup
src/Application source code
tests/Automated test suites
docs/Technical documentation
.github/workflows/CI/CD pipeline definitions
CONTRIBUTING.mdContribution rules and branch naming

Branching Strategy (main + feature)

main
feature/login
PR
main
feature/profile
Important: Keep feature branches short-lived (1–3 days) to reduce merge conflict risk.

Hands-on Commands

bash
# Create and switch to feature branch
git checkout -b feature/user-settings

# List branches
git branch -a

# Rename local branch
git branch -m feature/settings-page

# Delete merged local branch
git branch -d feature/settings-page

# Delete remote branch
git push origin --delete feature/settings-page

# Sync branch with main
git fetch origin
git rebase origin/main

Debugging Scenarios

Real-world Use Case

A team shipping 50 PRs/week reduced merge conflicts by 60% after switching to short-lived feature branches and requiring daily rebase from main. Release predictability improved because main stayed always releasable.

Interview Questions

Beginner

What is a repository?

A storage unit for project code and full change history.

What is a branch?

A pointer to a line of development allowing isolated changes.

Why protect main?

To prevent unreviewed changes and keep production branch stable.

What is a feature branch?

A temporary branch for developing a specific change.

Why delete merged branches?

To reduce clutter and avoid confusion in active branch list.

Intermediate

Main + feature vs GitFlow?

Main + feature is simpler and faster for continuous delivery; GitFlow is heavier but useful for strict release trains.

When rebase instead of merge?

Rebase to keep clean linear history on feature branches before PR merge.

What causes branch divergence?

Parallel commits on local and remote branch not synchronized via fetch/pull.

How enforce branch naming strategy?

Repo policy + CI validation script on branch name pattern.

Why short-lived branches matter?

Less drift from main, fewer conflicts, faster review and merge cycles.

Scenario-based

Feature branch is 200 commits behind main. Plan?

Pause changes, fetch latest main, rebase incrementally, run tests, then open PR.

Two teams need same shared refactor.

Create dedicated refactor branch, merge early, then rebase dependent branches.

Developer committed secrets on branch.

Remove secrets, rotate credentials, rewrite branch history before merge, and add secret scanning.

Main broke after merge despite review.

Revert merge commit immediately, fix in follow-up branch, strengthen CI gates.

Repo has monorepo and tiny service teams. Branch model?

Use main + scoped feature branches with path-based CI and code owners per directory.

Summary

A clean repository and disciplined branch strategy are the backbone of scalable team delivery. Protect main, keep branches short, and merge through reviewed PRs.