1

I have made a few changes to a branch on my git repository. However, I would like to add/stage those changes in batches of different commits.

For this, I usually add files with the -e or -p flag to include only the bits I want.

Unfortunately, this usually takes ages since I have to keep counting the lines added and removed so I can update the hunk header i.e @@ -32,7 +32,7 @@.

I have found out that git apply has a flag called --recount which basically ignore hunk headers and do the math based on the diff however I am not sure if git apply will just stage the files or replace the files content with the diff.

Anyway, I would like to know if there is any less painful way to perform git add -e without having to recalculate all hunk headers while staging only smaller bits of the file?

I was looking for a native way to do that within git itself without having to use a GUI application, if that is at all possible?

zanona
  • 12,345
  • 25
  • 86
  • 141

1 Answers1

2

It's been my experience that git add --patch aka git add -p handles this very well. I've never had to do arithmetic on line numbers, just nav through the hunks and apply what I want. Sometimes I have to split some. Anything more than a spelling error I apply just the good ones and bounce out to vim to correct the larger goofs. One stunt that might help is to add a marker to the hunks you fix this way, a pack of @'s or something, then you can / to them, e them, y them and be done with it. Yay for edit macros.

jthill
  • 55,082
  • 5
  • 77
  • 137