-1

What is the use case of git reset -p and then using "e" option to edit an applied hunk before resetting it ?

I tried playing around with the command but all edits I tried were refused by git.

Moha the almighty camel
  • 4,327
  • 4
  • 30
  • 53
  • If you are getting "your-edited-hunk-does-not-apply" error then this post is helpful/related : https://stackoverflow.com/questions/3268596/git-add-interactive-your-edited-hunk-does-not-apply – user Jul 09 '19 at 20:07
  • 1
    You have to be very careful when doing the edit for `e`, so that you only replace `-` by space or delete lines with `+`. And then I found even if I was careful, changing the last line sometimes made Git unhappy anyway. But once successful, Git put the results in the cache, ready for a commit that reverted things to the selected state. What I found confusing is that this only changes the cache, but not the working directory. – joanis Jul 09 '19 at 21:35
  • The use case, as you already have discovered, is to edit the hunk before applying it. – mkrieger1 Jul 09 '19 at 22:31

2 Answers2

1

I think the man page explains the use case, and also the common pitfalls you have encountered (edited for formatting):

-e, --edit
       Open the diff vs. the index in an editor and let the user edit it.
       After the editor was closed, adjust the hunk headers and apply the patch
       to the index.

       The intent of this option is to pick and choose lines of the patch to
       apply, or even to modify the contents of lines to be staged. This can be
       quicker and more flexible than using the interactive hunk selector.
       However, it is easy to confuse oneself and create a patch that does not
       apply to the index. See EDITING PATCHES below.

I've seen people use this command effectively; it's great for deleting extra lines and whitespace, logger and debugger statements, or fixing last-minute typos. But it should be used sparingly because it's easy to create a patch that does not apply.

When that happens, you can discard the change with n and make the correct change in your text editor.

See man git-add for more info.

Jake Worth
  • 5,490
  • 1
  • 25
  • 35
1

The other answer and comments discuss git add, but the question is about git reset. When using reset, you are editing the patch that will be "backed out" of the current index (i.e. what is already staged), so you can't just edit the patch to be what you wish you'd originally staged.

- lines will be added relative to the current index, and + lines will be deleted from the current index.

vertere
  • 11
  • 1