DEV Community

OE Haruki
OE Haruki

Posted on

Git Commands for Breaking Down Large Code Changes into Smaller PRs

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
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

b. Stage Remaining Changes

Use git add -p again to stage and split the remaining changes.

git add -p
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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!

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs