Handling PR feedback and updates in Git - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When working with pull requests (PRs), we often update code based on feedback. Understanding how the time to apply these updates grows helps us manage our work efficiently.
We want to know: how does the effort to update a PR change as the number of feedback points or changes grows?
Analyze the time complexity of the following git commands used to update a PR after feedback.
git checkout feature-branch
# Make code changes based on feedback
git add .
git commit -m "Address PR feedback"
git push origin feature-branch
This snippet shows the typical steps to update a feature branch with new changes after receiving PR feedback.
Look for repeated actions that affect time.
- Primary operation: Adding and committing changed files.
- How many times: Once per update cycle, but the number of files changed can vary.
As the number of files or lines changed grows, the time to stage and commit grows roughly in proportion.
| Input Size (changed files) | Approx. Operations |
|---|---|
| 10 | 10 file adds + 1 commit |
| 100 | 100 file adds + 1 commit |
| 1000 | 1000 file adds + 1 commit |
Pattern observation: The time grows linearly with the number of changed files because each file must be added before committing.
Time Complexity: O(n)
This means the time to update a PR grows in direct proportion to the number of files or changes you need to stage and commit.
[X] Wrong: "Updating a PR takes the same time no matter how many files I change."
[OK] Correct: Each changed file must be added and committed, so more changes mean more work and more time.
Understanding how your work scales with the size of changes shows you can manage code updates efficiently. This skill helps you plan and communicate better in real projects.
"What if instead of adding all files at once, you add files one by one in a loop? How would the time complexity change?"
Practice
Solution
Step 1: Switch to the feature branch
You need to work on the branch where the PR was created to apply feedback changes.Step 2: Make the necessary code updates
After switching, edit the code to address the feedback.Final Answer:
Check out the feature branch locally to make changes -> Option AQuick Check:
Update code on feature branch = A [OK]
- Trying to update main branch instead of feature branch
- Closing PR unnecessarily
- Starting a new branch without reason
Solution
Step 1: Stage all changes
The commandgit add .stages all modified and new files in the current directory.Step 2: Commit the staged changes
After staging, you usegit commit -m "message"to save changes locally.Final Answer:
git add . -> Option DQuick Check:
Stage all changes = git add . [OK]
- Using git commit -a without staging new files
- Pushing before committing
- Creating new branches unnecessarily
git add file.txt git commit -m "Fix typo" git push origin feature-branch
What happens to the existing pull request linked to
feature-branch?Solution
Step 1: Push updates to the feature branch
Pushing commits to the branch linked to the PR updates the PR automatically.Step 2: PR reflects new commits
The PR shows the new changes for reviewers to see and re-review.Final Answer:
The pull request updates automatically with the new commit -> Option BQuick Check:
Push to feature branch updates PR = A [OK]
- Thinking a new PR is needed for each update
- Believing PR closes on push
- Assuming manual merge needed to update PR
! [rejected] feature-branch -> feature-branch (non-fast-forward)What is the best way to fix this?
Solution
Step 1: Fetch and rebase remote changes
Rungit pull --rebase origin feature-branchto update your local branch with remote changes without merge commits.Step 2: Push the rebased branch
After rebasing, push your changes normally withgit push origin feature-branch.Final Answer:
Pull latest changes from remote and rebase before pushing -> Option AQuick Check:
Fix non-fast-forward by rebasing and pushing = C [OK]
- Using force push without caution
- Deleting remote branch unnecessarily
- Creating new branches instead of syncing
Solution
Step 1: Soft reset last 3 commits
git reset --soft HEAD~3moves HEAD back but keeps changes staged, allowing to combine commits.Step 2: Create a single new commit and force push
Commit staged changes with a new message, then force push to update PR with one clean commit.Final Answer:
git reset --soft HEAD~3; git commit -m "Clean update"; git push --force -> Option CQuick Check:
Combine commits by soft reset and force push = B [OK]
- Using merge instead of squash
- Forgetting to force push after rewriting history
- Incorrectly rebasing without editing commits
