You've been there. You're deep in a coding session, and the "flow" takes over. You start fixing a bug in auth.py, but then you notice a typo in a comment. While you're at it, you refactor that messy nested loop. Suddenly, you run git status and realize you've modified fifty lines across three different logical changes.
Most people just run git add . and call it a day. Some, who are a bit more disciplined, use git add -p to stage hunks interactively. But what happens when Git's "hunk" contains two different changes you want to separate? Git asks if you want to split it (s), but sometimes the lines are too close together for the automatic splitter to work.
That is exactly where git add -p e comes in.
The e stands for edit. It is the nuclear option of staging. It opens a temporary file in your text editor and lets you manually craft the patch you want to stage. It’s powerful. It’s a bit scary the first time. But honestly, it is the only way to maintain a truly professional, readable commit history when things get messy.
The Problem with Standard Patch Staging
Git is smart, but it isn't a mind reader. When you use the standard patch mode, Git breaks your changes into "hunks." It then asks you a series of questions: "Stage this hunk? [y,n,q,a,d,s,e,?]".
If you have a change on line 10 and another change on line 11, Git sees that as one block. You can't "split" it with s because there is no unchanged line between them to act as a divider. You're stuck. Either you stage both changes together—mixing your refactor with your bug fix—or you stage neither.
This creates technical debt in your Git history.
When a teammate (or you, six months from now) looks at the logs to find why a feature broke, they’ll see a commit titled "Fix login bug" that also contains random CSS tweaks and a renamed variable. It makes git bisect harder. It makes code reviews annoying. This is why git add -p e matters. It lets you delete the lines you don't want from the staging area without actually deleting them from your working file.
How git add -p e Actually Works Under the Hood
When you hit e, Git launches your default terminal editor—usually Vim or Nano, unless you’ve configured it otherwise. You aren't editing your actual file. You are editing a patch.
👉 See also: Why Earth: Planets in the Solar System Earth and the Secrets of Our Neighborhood
The file you see has a specific format. Lines starting with a space are "context" lines. Lines starting with a minus (-) are lines you are removing. Lines starting with a plus (+) are lines you are adding.
Here is the secret: you can modify these signs to tell Git exactly what to do.
If you want to keep a change in your file but not stage it for this commit, you simply revert the line in the editor to its original state. Change a + line back to a context line (a space). If you added a line but don't want to stage it yet, delete the line starting with + entirely. If you deleted a line but want to keep it deleted in your file but not in this commit, change the - to a space.
It feels like magic. Or surgery.
A Real-World Example
Imagine you're working on a JavaScript file. You've added a console.log for debugging and you also fixed the actual logic error.
- const total = price + tax
+ console.log("Debugging total:", price, tax)
+ const total = (price + tax) * discount
You want to stage the fix, but you definitely don't want that console.log in your pull request. When you run git add -p e, you just delete the line with the console.log in the editor. Save and exit.
Git stages the math fix. The console.log stays in your actual file on your disk. You haven't lost your debug info, but your commit stays clean.
The Golden Rules of Manual Hunk Editing
Don't mess with the lines that start with @.
Those are the "hunk headers." They tell Git the line numbers and the size of the change. If you start deleting them or changing the numbers manually, the patch will fail to apply. Git will give you a "patch does not apply" error and nothing will be staged.
Keep it simple.
- To skip adding a new line: Delete the line starting with
+. - To skip a deletion: Change the
-at the start of the line to a space. - To modify a change: You can actually edit the text of a
+line directly.
It’s worth noting that if you break the patch, Git won't hurt your file. It just fails the staging process for that hunk and moves on. You can always try again.
Why Does This Matter for E-E-A-T?
If you're a senior developer, your Git history is your resume.
🔗 Read more: Why Every Cybertruck on Fire Becomes a Viral Obsession
Junior developers often think Git is just a backup system. It’s not. It’s a communication tool. Using git add -p e shows a level of "Excellence" and "Trustworthiness" in your workflow. It indicates that you care about the "Atomic Commit" principle—the idea that every commit should do exactly one thing and leave the codebase in a working state.
Linus Torvalds, the creator of Git, has famously high standards for commit messages and structure. While most of us aren't submitting patches to the Linux kernel, the philosophy remains. High-quality software requires high-quality version control.
Common Pitfalls and How to Avoid Them
The biggest headache is the "Trailing Space."
Some editors are configured to automatically strip trailing whitespace when you save. If you're editing a Git patch and your editor removes a space from a context line (a line that starts with a space), the patch will fail. Git expects that space to be there.
If you’re using Vim, you might see patch fragment has no header or corrupt patch. This usually means you accidentally deleted one of those @ lines or messed up the leading character of a line.
Another tip: don't try to rewrite history inside the e prompt. If you find yourself spending more than two minutes editing a hunk, it’s probably better to just git stash, fix the file properly, or use a GUI tool like GitKraken or Fork that lets you stage individual lines with a mouse click. But for those of us who live in the CLI, e is the quickest path to perfection.
Configuring Your Editor
If your default editor is something heavy like VS Code, the e command might feel clunky because it has to open a whole new window. You can set a specific editor just for Git by running:
git config --global core.editor "nano"
Nano is actually great for hunk editing because it doesn't do any "smart" formatting that might break the patch syntax.
Actionable Steps for Your Next Commit
Ready to try it? Don't wait until you have a critical production bug. Practice in a safe environment.
- Open a dummy project.
- Make three different changes to the same paragraph of text in a file.
- Run
git add -p. - When Git shows the hunk, press
e. - Try to stage only the middle change.
- Save and exit the editor.
- Run
git diff --cachedto see what you actually staged. - Run
git diffto see what is still left in your working directory.
Once you get the hang of modifying those + and - prefixes, you'll never go back to "all or nothing" staging. You’ll find yourself more confident during refactors because you know you can always untangle the mess later. It makes you a better coder because it forces you to look at every single line of code one last time before it gets locked into the history forever.
Clean commits lead to easier reverts. Easier reverts lead to less stress. Less stress leads to better code. It all starts with that tiny e at the command prompt.