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
| Path | Purpose |
|---|---|
| README.md | Project overview and setup |
| src/ | Application source code |
| tests/ | Automated test suites |
| docs/ | Technical documentation |
| .github/workflows/ | CI/CD pipeline definitions |
| CONTRIBUTING.md | Contribution rules and branch naming |
Branching Strategy (main + feature)
Hands-on Commands
# 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
- Branch too far behind main: Rebase onto latest main frequently.
- Accidental direct commit to main: Move commit to feature branch and reset main to protected state.
- Stale branches clutter repo: Auto-delete merged branches in GitHub settings.
- Wrong branch naming: Enforce naming policy via repo contribution guide and checks.
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
A storage unit for project code and full change history.
A pointer to a line of development allowing isolated changes.
To prevent unreviewed changes and keep production branch stable.
A temporary branch for developing a specific change.
To reduce clutter and avoid confusion in active branch list.
Intermediate
Main + feature is simpler and faster for continuous delivery; GitFlow is heavier but useful for strict release trains.
Rebase to keep clean linear history on feature branches before PR merge.
Parallel commits on local and remote branch not synchronized via fetch/pull.
Repo policy + CI validation script on branch name pattern.
Less drift from main, fewer conflicts, faster review and merge cycles.
Scenario-based
Pause changes, fetch latest main, rebase incrementally, run tests, then open PR.
Create dedicated refactor branch, merge early, then rebase dependent branches.
Remove secrets, rotate credentials, rewrite branch history before merge, and add secret scanning.
Revert merge commit immediately, fix in follow-up branch, strengthen CI gates.
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.