---
title: "Two changes in one file, one commit? Use git add -p"
description: "git add -p shows a file's changes hunk by hunk, and you choose what goes into the next commit and what stays out. If a hunk holds two changes, s splits it and e opens it in your editor."
url: https://ihsanalapsi.dev/articles/git-add-patch-hunks
lang: en
date: 2026-11-25
updated: 2026-11-25
tags: ["Developer tools", "Web development"]
author: Ihsan Alapsi (إحسان العبسي)
---
# Two changes in one file, one commit? Use git add -p

You fixed a bug in a file, and in the same file you left a `console.log` line to test something, and started a feature that is not done yet. You want a commit with the fix only. `git add .` adds the whole file, and the commit ends up with all three. By the end of this article you will know how to pick the hunks you want from a file and leave the rest for later.

## What does git add -p do?

git add -p (or `--patch`) shows you the difference between the index and the file hunk by hunk, and asks you about each hunk whether to add it. A hunk is a group of changed lines next to each other.

According to the [git add documentation](https://git-scm.com/docs/git-add), this option gives you a chance to review the difference before it goes into the index. It is the same as `git add --interactive`, but it skips the first command menu and goes straight to the `patch` subcommand.

## Why does git add . spoil the commit?

Because `git add .` adds every change in the file, and does not know that one line is a fix and another is a test. So the commit carries things that have nothing to do with each other.

The human mistake here is simple: you think of the changes as separate things, and git thinks of the file as one unit. The result is a commit called "fix payment" with a `console.log` inside that prints customer data, or half a feature that breaks the build. And when you want to undo the fix later, the rest comes back out with it, which makes [reverting a pushed commit](/articles/git-revert-pushed-commit) harder.

## What do you answer for each hunk?

You answer with one letter and press Enter. These are the letters you need most, from the same documentation:


| Letter | What it does                                                 |
| ------ | ------------------------------------------------------------ |
| `y`    | stage this hunk                                              |
| `n`    | do not stage this hunk                                       |
| `q`    | quit; do not stage this hunk or any of the remaining ones    |
| `a`    | stage this hunk and all later hunks in the file              |
| `d`    | do not stage this hunk or any of the later hunks in the file |
| `s`    | split the current hunk into smaller hunks                    |
| `e`    | manually edit the current hunk                               |
| `?`    | print help                                                   |


After you decide on all the hunks, git updates the index with only the hunks you chose. The file itself does not change: the test line and the feature stay in it, outside the commit.

## What if a hunk holds two changes?

You press `s` to split it into smaller hunks, and if they are still stuck together, you press `e` and edit it by hand.

The documentation says `e` opens the patch in your editor, and when the editor exits, the result is applied to the index. So you remove from the hunk the lines that do not belong in this commit, and keep the fix only.

For a new file that git does not know yet, there is `git add -N` (or `--intent-to-add`): it only records that the file will be added later, with an entry in the index and no content. The documentation says this is useful for showing the file's content with `git diff` and committing it with `git commit -a`.

## Where do you start now?

The lesson for us: a commit should tell one story, and the unit of that story is the hunk, not the file. Next time, before you type `git add .`, type `git add -p` and answer for each hunk, then look at `git diff --staged` before you commit. If you forgot something and want to fold it into the right commit, see [fixup and autosquash](/articles/git-commit-fixup-autosquash). For more everyday tools, see the [developer tools articles](/articles?tag=developer-tools). What was your last commit that should have been split?

## FAQ

**What is the difference between git add -p and git add --interactive?**

git add -p is the same interactive mode, but it skips the first menu and goes straight to choosing hunks.

**Can I skip pressing Enter after each answer?**

Yes. Set interactive.singleKey to true, and git takes the single key on its own.

**Can I move between hunks instead of answering in order?**

Yes. j and k move to the next or previous undecided hunk, g picks a hunk to go to, and / searches for a hunk matching a regex.
