I like to use git add --patch (and more recently, git add -i) to untangle my commits as well as verify that what I'm committing is good to go.
Once in a while I'll come across a hunk that might be an error logging statement, an extra newline (usually from erasing the aforementioned logging statement) - something that I'd actually rather remove entirely.
I don't want to stage and I'd also like to simply delete the hunk in question while it's right there in front me of (rather than jumping back to my editor and trying again). I want to apply the change against my working file as well.
Is there a way to do this?
What I've considered is using the edit hunk functionality.
This, in conjunction with the suggestion hash
made below gets me a slightly nicer workflow than I have now.
I agree that it's a violation of the git add
's separation of concerns. OTOH it would just be so convenient ;P I sound like my boss ;)
Better than using reset hard HEAD, after committing all your valid hunks, you can just git checkout the file to reset it to what's recorded in the current branch. That way it doesn't affect any other files.
There's a thorough explanation of why git add does not do this from Git maintainer Junio on the Git mailinglist in response to a feature request.
Short version of his alternative process:
# start from N-commit worth of change, debug and WIP
git stash save -p debug ;# stash away only the debugging aid
# now we have only N-commit worth of change and WIP
git stash save -p wip ;# stash away WIP
git add -p ;# prepare the index for the next commit
git stash save -k ;# save away the changes for later commits
git commit
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With