Trunk-based development in Git - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to integrate code changes grows when using trunk-based development in git.
Specifically, how does the process scale as more developers add changes to the main branch?
Analyze the time complexity of the following git commands in trunk-based development.
# Developer updates local main branch
git checkout main
# Pull latest changes from remote
git pull origin main
# Make small change and commit
git add .
git commit -m "small change"
# Push change to remote main branch
git push origin main
This snippet shows the typical steps a developer takes to update and push small changes directly to the main branch.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Pulling and pushing changes to the main branch.
- How many times: Once per developer per change, repeated continuously as new changes are made.
As more developers push changes, the time to pull and merge updates grows roughly in proportion to the number of changes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 developers | About 10 pull and push operations per change cycle |
| 100 developers | About 100 pull and push operations per change cycle |
| 1000 developers | About 1000 pull and push operations per change cycle |
Pattern observation: The number of operations grows linearly with the number of developers making changes.
Time Complexity: O(n)
This means the time to integrate changes grows linearly as more developers push to the main branch.
[X] Wrong: "Pushing directly to main means integration time stays the same no matter how many developers work."
[OK] Correct: Each new change requires pulling and merging others' changes, so integration time grows with more developers.
Understanding how integration time scales in trunk-based development shows you grasp teamwork impact on code flow, a key skill in real projects.
"What if developers used feature branches instead of pushing directly to main? How would the time complexity change?"
Practice
trunk-based development?Solution
Step 1: Understand trunk-based development concept
It focuses on working mainly on one main branch, often called trunk or main.Step 2: Compare options with this concept
Options A, C, and D describe practices that do not align with trunk-based development principles.Final Answer:
Developing mostly on one main branch to avoid big merge conflicts -> Option AQuick Check:
Trunk-based development = one main branch [OK]
- Thinking trunk means many long-lived branches
- Believing merges happen rarely in trunk-based
- Confusing local-only work with trunk-based
git commands is best to quickly merge a short-lived feature branch back to main in trunk-based development?Solution
Step 1: Identify the command to merge branches
git merge feature-branchmerges the feature branch into the current branch, usually main.Step 2: Check other commands
git rebaserewrites history,git checkoutswitches branches, andgit branch -ddeletes a branch but does not merge.Final Answer:
git merge feature-branch -> Option AQuick Check:
Merge short-lived branch = git merge [OK]
- Using git checkout instead of merging
- Deleting branch before merging
- Confusing rebase with merge
git checkout main git pull origin main git checkout -b feature # make changes git commit -am 'Add feature' git checkout main git merge feature
What is the state of the
main branch after these commands?Solution
Step 1: Follow the commands step-by-step
Start on main, update it from origin, create feature branch, commit changes, switch back to main, then merge feature into main.Step 2: Understand merge effect
After merge, main branch includes the feature changes locally.Final Answer:
Main branch has the new feature changes merged in -> Option BQuick Check:
Merge feature into main = main updated [OK]
- Assuming merge deletes feature branch automatically
- Thinking main is unchanged after merge
- Forgetting to pull origin before branching
Solution
Step 1: Understand merge conflicts
Conflicts happen when changes overlap. They must be fixed manually to keep code correct.Step 2: Choose correct resolution
Resolving conflicts manually and committing is the proper way. Deleting branch or ignoring conflicts causes problems.Final Answer:
Resolve conflicts manually, then commit the merge -> Option CQuick Check:
Fix conflicts manually = merge success [OK]
- Ignoring conflicts thinking git fixes automatically
- Deleting branch instead of resolving
- Force pushing without resolving conflicts
Solution
Step 1: Understand trunk-based development goals
It encourages short-lived branches merged quickly to main to reduce conflicts and speed releases.Step 2: Evaluate options
Create short-lived feature branches, merge them quickly to main, and deploy often matches this approach. Keep all features in one big branch for weeks before merging causes long-lived branches. Work only on main branch without any feature branches limits parallel work. Use separate repositories for each feature complicates repo management.Final Answer:
Create short-lived feature branches, merge them quickly to main, and deploy often -> Option DQuick Check:
Short-lived branches + quick merge = trunk-based best practice [OK]
- Thinking all work must be on main only
- Using long-lived branches defeats trunk-based purpose
- Splitting features into separate repos unnecessarily
