Git Best Practices for Developers

Chapter 1 · Practice 3 of 4

Review and stage changes in pieces with git add -p

From Commits and commit messages

Why it matters

git add . stages everything, including debug prints, temporary files and unrelated edits. Staging in pieces keeps commits focused and doubles as a quick self review: you read every line before it is committed.

  • git add -p walks through your changes hunk by hunk so you choose what belongs in this commit.
  • git diff --staged shows exactly what will go in. Run it before every commit.
  • git restore --staged <file> unstages a file you staged by mistake, without losing your edits.

Example

Shell
git add -p                    # pick hunks interactively
git diff --staged             # check what will be committed
git restore --staged app.log  # unstage a file, keep the changes
git commit