Even if you've already committed many changes to a single branch, it's possible to break them down into smaller, more manageable PRs.
This article explains the detailed process.
1. Check Current Status
First, review what commits exist in your current branch.
git log --oneline
If all changes are in the latest commit, proceed to the next step.
2. Reset Latest Commit
Reset the latest commit to move changes back to the staging area. Using the --soft
option preserves the working directory changes.
git reset --soft [target branch for eventual merge]
Note
-
--soft
: Undoes the commit and returns changes to the index.
3. Stage Changes in Smaller Units
Use git add -p
to stage changes in smaller chunks.
git add -p
Prompt commands:
Command | Action |
---|---|
y |
Stage this change |
n |
Skip this change |
q |
Skip all remaining changes |
s |
Split current change into smaller chunks |
4. Create Small Commits
Commit the staged changes.
Repeat this process to break down large changes into smaller commits.
git commit -m "feat: add feature xxx"
5. Update Remote Repository
If the branch has already been pushed to the remote repository, you'll need to overwrite the history.
Use the --force-with-lease
option to safely overwrite.
git push --force-with-lease origin feature/large-update
Note
-
--force-with-lease
: Checks remote state and forces push only if no one else has made changes.
6. Create PR
After reflecting the split branch to the remote repository, create a PR.
7. Repeat Process on New Branch
After completing the first PR, create a new branch and repeat the same process.
Here's the workflow:
a. Create New Branch
Switch to a new branch from the current state.
git checkout -b feature/next-small-pr
b. Stage Remaining Changes
Use git add -p
again to stage and split the remaining changes.
git add -p
c. Commit and Push
Commit in appropriate units and push to remote as a new branch.
Since we haven't reset this branch, a normal git push
is fine.
git commit -m "Refactor module Y"
git push origin feature/next-small-pr
d. Create Next PR
Create a PR from the new branch and request review.
Repeat this process as needed to organize the large code change into several smaller PRs.
Summary Table of Overall Flow
Step | Example Command | Description |
---|---|---|
Check Current Status | git log --oneline |
Review current commit history |
Reset | git reset --soft [target branch] |
Undo all commits on branch and return changes to staging area |
Stage Changes in Parts | git add -p |
Interactively select and split changes |
Commit | git commit -m "..." |
Commit selected changes |
Update Remote | git push --force-with-lease origin feature/large-update |
Overwrite history and update remote |
Create New Branch | git checkout -b feature/next-small-pr |
Start work on new branch for next changes |
Important Notes
Be Careful with Force Push
While--force-with-lease
reduces the risk of overwriting others' changes, always ensure you're working with the latest state.Don't Split Too Small
It's important to split into meaningful units. Use "one logical unit" as your guideline.
With these steps, you can safely and efficiently split large changes into smaller PRs, even after committing many changes to a single branch!
Give it a try!
Top comments (0)