1

My common workflow is to do a bunch of code editing, and then use git add --patch to organize my changes into logically consistent commits.

A scenario that comes up over and over again is this:

  1. I skip a bunch of changes that don't relate to the commit I want to make
  2. I add some changes that make sense
  3. I find a typo or some commented-out code that I forgot to delete
  4. I hit q to exit the interactive add
  5. I go back to my editor and make the small change
  6. I start git add --patch again
  7. I very carefully walk through the entirety of the change set again, making sure to type n over again for all the hunks I don't want to include
  8. I reach my newly updated hunk and type y to add it.
  9. GOTO step 1

This cycle plays out over and over again until finally my commit is ready to go.

What I'd like is for git to remember which hunks I've explicitly declined to add, so that the next time I run git add --patch, I don't have to decline them again.

I don't think git supports this, but is there any tool or workflow I could adapt that would make this easier?

adamesque
  • 1,906
  • 19
  • 24
  • Any reason why you don't espouse the "commit early, commit often" style? – Makoto May 19 '17 at 20:39
  • I do commit fairly often, but especially if I'm in an unfamiliar codebase or spiking something out, I'll have some edits in my working copy that don't make sense to group together into 1 commit. – adamesque May 19 '17 at 20:44
  • For what it's worth, I find myself in similar situations at times when I have done a lot of hacking that needs reconstruction and cleanup. Instead of trying to make a bunch of little commits with `git add -p`, I just commit the big change on a temporary branch, then start over with smaller, better-planned changes on the "real branch". – torek May 19 '17 at 22:31

1 Answers1

0

It's possible that this is best accomplished using the Git integration in my editor (currently VSCode).

In the Git: Open Changes view of a file with changes, you can select a region of the file and then run the Git: Stage Selected Ranges command to stage a hunk. Since you're right in the editor, it's trivial to make a change there before staging.

I may just need to change my workflow and do my staging this way.

(I believe Magit in Emacs also supports this kind of workflow.)

adamesque
  • 1,906
  • 19
  • 24