Forrest logo
back to the git tool

git-stage:tldr:49313

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

The command "git stage -p" in Git is used to interactively select and stage changes or updates from the working directory to the index (also known as staging area). It provides a way to review each change or update and allows you to selectively stage parts of a file.

When you run "git stage -p" or "git add -p" (both commands are equivalent), Git presents you with a patch-like view of each modified file, divided into chunks. It offers multiple options for each chunk, such as adding the chunk to the index, skipping the chunk, or splitting the chunk into smaller parts. These options are accessible through commands denoted by single-letter keys.

For example, when viewing a chunk in the patch-like view, you might see options such as:

  • y: Stage this hunk (chunk)
  • n: Do not stage this hunk
  • q: Quit the interactive patch mode
  • s: Split this hunk into smaller ones

By typing the appropriate key, you can choose the action you desire for each chunk. This allows you to stage only specific changes or parts of a file, instead of staging the entire modified file.

Using "git stage -p" can be useful when you have made multiple independent changes within a single file and want to commit them separately. It helps in achieving granular version control and separates unrelated changes into different commits.

Note: The command "git stage -p" is an alias for "git add -p" and can be used interchangeably.

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