You can stage specific hunks interactively in your file(s) using `git add -p`

Instead of staging entire files, git add -p (or --patch) lets you interactively review and stage individual chunks (hunks) of changes. This is perfect for creating focused, logical commits from a file with mixed changes.

Usage:

git add -p [file]

Git will show each hunk and prompt you with options:

Common options:

Example scenario:

You have a file with both bug fixes and new features. Using git add -p, you can:

  1. Stage only the bug fix hunks → commit them separately
  2. Stage the feature hunks → commit them as a different logical change

Why use it:

Split large hunks:

If Git’s hunks are too large, use s to split them into smaller pieces, or e to manually edit which lines to stage.

This approach encourages better commit hygiene and makes it easier to revert specific changes later without affecting unrelated code.

Original source