git add -p

## Metadata
- Author: [[Sal Ferrarello]]
- Full Title: git add -p
- Category: #articles
- Summary: One of the challenges I found in creating better commits is that I often had other changes in my files. Running git add . (or my preferred git add -u) resulted in unrelated changes being introduced in my commit. Then I learned about git add -p.
- URL: https://salferrarello.com/git-add-p/
One of the challenges I found in creating better commits is that I often had other changes in my files. Running `git add .` (or my preferred `git add -u`) resulted in unrelated changes being introduced in my commit.
Then I learned about `git add -p`.
#### Patch
Using [git add --patch](https://git-scm.com/docs/git-add#Documentation/git-add.txt--p) (or the short version `git add -p`) prompts you for each section of changes (a.k.a. hunks), which you can either stage for your commit (`y`) or skip for now (`n` to be included in a later commit).

#### Reviewing Your Changes Before Commits
##### From the Command Line
```
git diff --staged
```
will show you all the changes that are currently staged. I think of this as a preview of the commit I’m about to create.
##### As a Preview in Your Editor
You can modify your Git configuration so that whenever you author a commit in your editor you can also see a preview of the changes that go with that commit. See how to [Preview Your Changes When Writing Your Commit Message](https://salferrarello.com/git-preview-changes-in-commit-message/).
Sal is a PHP developer with a focus on the WordPress platform. He is a [conference speaker](https://salferrarello.com/speaking) with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at [WebDevStudios](https://webdevstudios.com), where he works as a senior backend engineer., git add -p, Sal Ferrarello, 01gnmcxcbh07erfxxyjdfw1skx.