Forrest logo
back to the git tool

git-add:tldr:27488

git-add: Interactively stage parts of files.
$ git add -p
try on your machine

The command "git add -p" is used in Git to review and stage changes selectively. The "-p" flag stands for "patch mode".

When you run "git add -p", Git starts the patch mode, which allows you to review each change made to your files since the last commit and decide whether to stage them or not. Git presents each change as a patch and prompts you with choices for each hunk (patch).

You have several options (choices) for each hunk:

  1. y - stage this hunk: Selecting "y" stages the current hunk and adds it to the index or staging area for the next commit.
  2. n - do not stage this hunk: Selecting "n" skips the current hunk and leaves it unmodified in the working directory.
  3. s - split this hunk: Selecting "s" allows you to split the current hunk into smaller, more manageable hunks.
  4. q - quit: Selecting "q" quits the patch mode without making any changes, but keeps any changes that were already staged.
  5. a - stage this hunk and all remaining hunks in the file: Selecting "a" stages the current hunk plus all the remaining hunks in the file.
  6. d - do not stage this hunk or any remaining hunks in the file: Selecting "d" skips the current hunk plus all the remaining hunks in the file.
  7. ? - help: Displays a list of available options or commands.

After making your choices for each hunk, Git will either stage the approved changes or keep them unchanged in the working directory, based on your selections.

Using "git add -p" is helpful when you want to carefully review and control which changes to include in the next commit.

This explanation was created by an AI. In most cases those are correct. But please always be careful and never run a command you are not sure if it is safe.
back to the git tool