开发者

Removing Hunk Interactively with git add --patch

开发者 https://www.devze.com 2022-12-18 11:58 出处:网络
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.

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


You can add to index whatever you want, commit, then reset hard HEAD, then what was not in the index is lost.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号