{"thread":{"id":"27306","subject":"'git checkout -p' is a bit confusing...","startedAt":"2011-05-09T10:05:49Z","lastAt":"2011-05-09T10:57:00Z","messageCount":2,"participants":["John Szakmeister","Jeff King"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"167480","messageId":"BANLkTik+VbJZKc1Xwb-3p3HPW-zxanc7HQ@mail.gmail.com","threadId":"27306","inReplyTo":null,"subject":"'git checkout -p' is a bit confusing...","fromName":"John Szakmeister","fromEmail":"john@szakmeister.net","sentAt":"2011-05-09T10:05:49Z","receivedAt":"2011-05-09T10:05:49Z","isPatch":false,"sender":{"key":"john@szakmeister.net","avatar":"https://avatars.githubusercontent.com/u/448087?v=4"},"body":"I noticed this a little while back, but thought it may have been my\nown misunderstanding.  I wanted to selectively revert part of a file,\nso I used 'git checkout -p filename'.  Then I needed to edit the hunk,\nand got something like this:\n\n    # Manual hunk edit mode -- see bottom for a quick guide\n    @@ -236,6 +236,12 @@ int run_add_interactive(const char *revision,\nconst char *patch_mode,\n            }\n            args[ac] = NULL;\n\n    +        for (i=0; i < ac; i++)\n    +          {\n    +            fprintf(stderr, \"%s \", args[i]);\n    +          }\n    +        fprintf(stderr, \"\\n\");\n    +\n    # ---\n    # To remove '+' lines, make them ' ' lines (context).\n    # To remove '-' lines, delete them.\n    # Lines starting with # will be removed.\n    #\n    # If the patch applies cleanly, the edited hunk will immediately be\n    # marked for discarding. If it does not apply cleanly, you will be given\n    # an opportunity to edit again. If all lines of the hunk are removed,\n    # then the edit is aborted and the hunk is left unchanged.\n\nSince the diff was showing me the forward direction (from the base to\nmodified working tree), I expected that when I left the +'s in there,\nthat it was going to leave my hunk.  Unfortunately, it discarded my\nhunk.  I can see the text at the bottom, and now it makes sense, but I\nwonder if there's a way to make it so that you can edit the patch to\nlook the way you want, and keep those bits (in much the same way as\n'git add -p' works)?\n\nI hope that makes sense. :-)\n\nThank you!\n\n-John\n"},{"id":"167484","messageId":"20110509105700.GC9060@sigill.intra.peff.net","threadId":"27306","inReplyTo":"BANLkTik+VbJZKc1Xwb-3p3HPW-zxanc7HQ@mail.gmail.com","subject":"Re: 'git checkout -p' is a bit confusing...","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2011-05-09T10:57:00Z","receivedAt":"2011-05-09T10:57:00Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Mon, May 09, 2011 at 06:05:49AM -0400, John Szakmeister wrote:\n\n> I noticed this a little while back, but thought it may have been my\n> own misunderstanding.  I wanted to selectively revert part of a file,\n> so I used 'git checkout -p filename'.  Then I needed to edit the hunk,\n> and got something like this:\n> \n>     # Manual hunk edit mode -- see bottom for a quick guide\n>     @@ -236,6 +236,12 @@ int run_add_interactive(const char *revision,\n> const char *patch_mode,\n>             }\n>             args[ac] = NULL;\n> \n>     +        for (i=0; i < ac; i++)\n>     +          {\n>     +            fprintf(stderr, \"%s \", args[i]);\n>     +          }\n>     +        fprintf(stderr, \"\\n\");\n>     +\n>     # ---\n>     # To remove '+' lines, make them ' ' lines (context).\n>     # To remove '-' lines, delete them.\n>     # Lines starting with # will be removed.\n>     #\n>     # If the patch applies cleanly, the edited hunk will immediately be\n>     # marked for discarding. If it does not apply cleanly, you will be given\n>     # an opportunity to edit again. If all lines of the hunk are removed,\n>     # then the edit is aborted and the hunk is left unchanged.\n> \n> Since the diff was showing me the forward direction (from the base to\n> modified working tree), I expected that when I left the +'s in there,\n> that it was going to leave my hunk.  Unfortunately, it discarded my\n> hunk.  I can see the text at the bottom, and now it makes sense, but I\n> wonder if there's a way to make it so that you can edit the patch to\n> look the way you want, and keep those bits (in much the same way as\n> 'git add -p' works)?\n> \n> I hope that makes sense. :-)\n\nYour mail does make sense. There are basically two ways of looking at a\ndiff like this:\n\n  1. Is it a patch you want to apply to the working tree, and you want\n     to select the parts you like?\n\nor\n\n  2. Is it a change that is in the working tree versus the index, and\n     you want to discard or keep the change?\n\nThe interactive patch viewer is capable of doing both directions. In\nfact, if you do:\n\n  git checkout -p\n\nthen it will show you the diff from the index to the working tree and\nsay \"did you want to discard this hunk?\" It does a similar thing for\n\"git checkout -p HEAD\". But if you do:\n\n  git checkout -p some-other-commit\n\nthen it will say \"did you want to apply this hunk?\".\n\nWhich seems somewhat inconsistent, but mostly does what people mean. But\nas you noticed, when you stop saying \"take this hunk\" versus \"discard\nthis hunk\" and move into \"edit this hunk\", the \"discard\" direction seems\nmore counter-intuitive.\n\nIn theory I guess we could flip between the two directions, which is,\nafter all, more or less what \"apply -R\" does. So we could be in\n\"discard\" mode, but when somebody wants to \"e\"dit the patch, it would\nalways be in \"take the parts you _like_\" mode. I have a nagging feeling\nthat there may be some corner case that doesn't reverse properly, but I\nhaven't thought too long about it.\n\n-Peff\n"}]}