How to Commit Only Part of a File's Changes in Git
Learn to use git add -p for partial commits in Git. Stage only specific lines like 15 out of 30 changes, verify with git diff --staged, and commit cleanly. Perfect for atomic git commits and better code reviews.
How to commit only part of a file’s changes in Git
When I make changes to a file in Git, how can I commit only some of the changes?
For example, how could I commit only 15 lines out of 30 lines that have been changed in a file?
To commit only part of a file’s changes in Git, fire up git add -p <filename>—it drops you into an interactive mode where you pick hunks (blocks of changes) to stage for your next git commit. Want just 15 out of 30 lines? Split oversized hunks with ‘s’ until you hit the sweet spot, then stage them selectively. It’s a game-changer for keeping commits atomic and reviews painless.
Contents
- Why Partial Commits Matter in Git
- Mastering Git Add -p for Selective Staging
- Interactive Options During Git Add
- Step-by-Step: Commit 15 out of 30 Lines
- Verify Staged Changes Before Git Commit
- Fix Mistakes and Alternatives
- Sources
- Conclusion
Why Partial Commits Matter in Git
Ever hacked away at a file, only to realize half your changes belong in a different feature branch? Git’s partial commit workflow saves the day. Instead of dumping everything with a sloppy git add . and git commit, you stage surgically. This keeps your history clean—think focused PRs that reviewers actually read.
Sources like SourceBae nail it: partial staging makes commits “focused and review-friendly.” Why bother? Bloated commits hide bugs. Small ones? They’re debuggable gold.
But how? It starts with staging, Git’s prep step before committing. git add snapshots changes to the index. The -p flag (patch mode) lets you cherry-pick.
Mastering Git Add -p for Selective Staging
git add -p (or git add --patch) is your go-to for git add commit precision. Run it on a specific file: git add -p src/main.py. Git scans diffs, showing hunks one by one.
Each hunk looks like this in your terminal:
diff --git a/file.txt b/file.txt
index abc123..def456 100644
--- a/file.txt
+++ b/file.txt
@@ -10,6 +10,6 @@ old line
unchanged
-stage this?
+keep this out
unchanged
Prompts hit: y (stage), n (skip), q (quit), a (stage rest). Boom—partial staging without touching the file.
Codingem breaks it down: this splits “large changes into focused commits.” Works on modified, untracked, or even deleted lines. Pro tip: chain it with git add -p . for the whole repo.
What if a hunk’s too chunky?
Interactive Options During Git Add
Git’s prompt isn’t basic—it’s loaded. Here’s the cheat sheet:
- y: Stage this hunk (yes).
- n: Skip it (no).
- s: Split into smaller hunks. Crucial for your 15/30 lines scenario.
- e: Manually edit the hunk in your $EDITOR.
- d: Skip this and all remaining hunks.
- ?: Full help menu.
DEV Community demos entering ‘e’ to tweak lines precisely, like patching countries.txt but only staging “USA” additions.
Hungry for more control? git add -i launches full interactive mode—patch subcommands galore. Or git add --interactive for the same. Feels like a game: stage, unstage, even revert mid-session.
And don’t sleep on editing diffs…
Step-by-Step: Commit 15 out of 30 Lines
Picture report.md with 30 changed lines. You want lines 5-20 only. Here’s the play-by-play:
git status—confirm changes.git add -p report.md- First hunk (lines 1-10): hit ‘s’ to split.
- Smaller hunks appear—‘y’ on lines 5-10.
- Next big hunk (11-30): split again, ‘y’ on 11-20, ‘n’ on 21-30.
qto quit.
Check with git diff --staged—exactly 15 lines glowing green/red.
Now commit: git commit -m "Update report section A: lines 5-20".
SourceBae walks a similar flow, verifying via diff before commit. If the file’s massive? Repeat splits patiently—Git handles it.
Real-world twist: multiple files? git add -p cycles through all.
Verify Staged Changes Before Git Commit
Staged something wrong? Panic not. git diff --staged (or --cached) shows precisely what’s queued for git commit. Empty? Good. Weird lines? Fix it.
git diff compares working dir to staged—unstaged changes stay safe.
Commit confidently: git commit -m "Precise change desc". Log it with git log --oneline -5.
PhoenixNAP stresses --edit for verification: it opens the diff in vim/nano. Delete unwanted lines, save—staged hunk shrinks.
Short and sweet: always diff before commit. Saves embarrassing amends.
Fix Mistakes and Alternatives
Oops, staged too much? git reset -p <file> unstages interactively—same prompts, reverse.
Accidental commit? git reset HEAD~1 or git commit --amend for fixes.
Alternatives when -p feels fiddly:
git add -e <file>: Edit the full diff hunk-by-hunk in editor. Codingem loves this for “coarser hunks.”- Stash extras:
git stash -p, pick hunks to stash away.
Edge cases? Binary files skip -p. Huge repos? Tools like GitKraken visualize hunks.
Practice on a dummy repo—git init test; echo "line1" > file.txt; ...—muscle memory kicks in fast.
Sources
- Commit only part of a file’s changes in Git - SourceBae
- Git How to Commit Parts of a File — The ‘git add -p’ Command - Codingem
- Git: using --interactive mode to commit specific parts of a file - DEV Community
- git add: How to Stage in Git - PhoenixNAP
Conclusion
Partial commits via git add -p transform messy Git workflows into precise git commit machines—perfect for staging just 15 lines from 30 without fallout. Drill the options (split, edit, verify), and you’ll ship cleaner history every time. Next tangled file? You know the drill: patch mode awaits.