Don’t Let Your Commits Become a “Messy Stew”
A familiar scenario: You’re fixing a critical logic bug when you notice a few nearby lines of code that look “messy,” so you refactor them on the spot. Then, you add a few comments and delete some redundant console.log statements. Looking back at git status, a single file has changed by 100 lines across three completely different purposes.
If you use git add . and then commit with the message “Fix bug and refactor,” you’re making things difficult for yourself. The code reviewer (or even you, months later) will have to swim through that heap of changes to find the actual bug fix logic. Bundling everything into one commit makes tracking down errors a nightmare.
git add -p (short for –patch) is the lifesaver in this situation. This command allows you to inspect every small change (hunk) and decide whether to move it to the staging area or not. Let’s clean up this code like a pro.
Quick Start: Master git add -p in 5 Minutes
Suppose the file app.py has just been modified for both payment logic and display formatting. Instead of adding the whole file, type the command:
git add -p app.py
Git will split the file into individual blocks of changed code (called a hunk) and ask how you want to handle them:
Stage this hunk [y,n,q,a,d,j,J,g,/,s,e,?]?
Don’t let that string of characters confuse you. In reality, you only need to remember 4 basic keys for 90% of cases:
- y (yes): Agree to include this hunk in the current commit.
- n (no): Skip this hunk for a later commit.
- s (split): If the current hunk still contains both bug fix and refactor code, press
sto split it even further. - q (quit): Exit without adding anything more.
After you’ve made your selections, commit the staged part using git commit -m "Fix: VAT calculation logic". The parts where you pressed n will remain in the unstaged state, ready for you to commit separately with a different message.
Advanced Options to Speed Up Your Workflow
Once you’re comfortable with y/n/s, you can speed up the processing of large files with thousands of lines using navigation keys:
1. Thorough Splitting with the ‘s’ Key
Git automatically groups nearby changed lines into a hunk. If those 10 lines contain 5 lines of bug fixes and 5 lines of comments, the s key will attempt to separate them based on the unchanged lines in between.
2. Smart Navigation (j, J, g)
- j: Leave this hunk undecided and jump to the next hunk.
- k: Go back to the previous hunk.
- g: Quickly jump to a specific hunk in the list.
3. Bulk Decisions (a, d)
- a (all): Stage this hunk and all remaining hunks in the file.
- d (don’t): Skip this hunk and all remaining parts of the file.
“Edit” Mode: Modifying Character by Character with ‘e’
Sometimes the s (split) command cannot divide the code as desired, such as when two changes are right next to each other. In this case, press e (edit) to intervene manually.
Git will open your editor (Vim or Nano). You will see lines starting with + (added) and - (removed).
- Want to remove an added line from this commit? Delete that line.
- Want to keep a line that was supposed to be deleted? Replace the
-sign with a space.
Important Note: Do not change the actual code content while in this mode. You should only delete lines or change the prefix character to avoid “patch does not apply” errors.
Why Is This a Senior Habit?
In a real-world project, a Pull Request (PR) containing 500 lines of messy code will take a colleague an hour to review. If you split it into 5 small commits of 100 lines each, each focusing on one issue, the review time can be reduced by 30%.
Using git add -p brings 3 practical benefits:
- Self-review: You are forced to look back at every line of code. This is when you spot redundant variables or leftover
print(data)statements. - Atomic Commits: Each commit does exactly one thing. If the refactored code causes an error, you can
git revertit without losing the important bug fix. - Superfast Debugging with Git Bisect: Splitting commits helps the
bisectcommand find the exact line causing the error many times faster.
Tips for Combining with Other Git Commands
Final Check with git commit -v
After you finish staging, try using the command:
git commit -v
This command displays the entire code content you are about to commit right below the message window. It ensures 100% that no “garbage” slips into the project history.
Discarding Junk Changes (git checkout -p)
If you want to delete a few experimental code snippets but still want to keep other parts in the same file, use:
git checkout -p filename.py
The mechanism is identical to git add -p, but instead of choosing to stage, you choose y to discard that piece of code. Your file will be clean immediately.
Conclusion
Learning git add -p might make you feel a bit slower in the first few days. However, in the long run, it builds a meticulous and professional mindset. Don’t just be a coder who knows how to write code that runs; be an engineer who knows how to manage source code artistically.

