Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing Hunk Interactively with git add --patch

Tags:

git

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 ;)

like image 530
Koobz Avatar asked Feb 01 '10 09:02

Koobz


2 Answers

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.

like image 74
Xentac Avatar answered Sep 22 '22 01:09

Xentac


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
like image 26
Herman van Rink Avatar answered Sep 26 '22 01:09

Herman van Rink