• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Dev Tips / git add -p

git add -p

Last updated on February 13, 2021 by Sal Ferrarello

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 (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).

git add -p example.

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.

Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit
Warning! This is a draft, not a finalized post. See full draft disclosure.

Filed Under: Dev Tips, Draft Tagged With: Git

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in