{"thread":{"id":"45029","subject":"[PATCH] reset: add an example of how to split a commit into two","startedAt":"2017-02-03T00:31:02Z","lastAt":"2017-02-03T20:16:19Z","messageCount":4,"participants":["Jacob Keller","Duy Nguyen","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"310747","messageId":"20170203003038.22278-1-jacob.e.keller@intel.com","threadId":"45029","inReplyTo":null,"subject":"[PATCH] reset: add an example of how to split a commit into two","fromName":"Jacob Keller","fromEmail":"jacob.e.keller@intel.com","sentAt":"2017-02-03T00:30:38Z","receivedAt":"2017-02-03T00:31:02Z","isPatch":true,"sender":{"key":"jacob.e.keller@intel.com","avatar":"https://avatars.githubusercontent.com/u/874719?v=4"},"body":"From: Jacob Keller <jacob.keller@gmail.com>\n\nIt is sometimes useful to break a commit into parts to more logically\nshow how the code changes. There are many possible ways to achieve this\nresult, but one simple and powerful one is to use git reset -p.\n\nAdd an example to the documentation showing how this can be done so that\nusers are more likely to discover this, instead of inventing more\npainful methods such as re-writing code from scratch, or duplicating git\nadd -p more times than necessary.\n\nSigned-off-by: Jacob Keller <jacob.keller@gmail.com>\n---\n Documentation/git-reset.txt | 23 +++++++++++++++++++++++\n 1 file changed, 23 insertions(+)\n\ndiff --git a/Documentation/git-reset.txt b/Documentation/git-reset.txt\nindex 25432d9257f9..4adac7a25bf9 100644\n--- a/Documentation/git-reset.txt\n+++ b/Documentation/git-reset.txt\n@@ -292,6 +292,29 @@ $ git reset --keep start                    <3>\n <3> But you can use \"reset --keep\" to remove the unwanted commit after\n     you switched to \"branch2\".\n \n+Split a commit into two::\n++\n+Suppose that you have created a commit, but later decide that you want to split\n+the changes into two separate commits, including only part of the old commit\n+into the first commit, and including the rest as a separate commit on top. You\n+can use git reset in patch mode to interactively select which hunks to split\n+out into a separate commit.\n++\n+------------\n+$ git reset -p HEAD^                        <1>\n+$ git commit --amend                        <2>\n+$ git commit ...                            <3>\n+------------\n++\n+<1> This lets you interactively undo changes between HEAD^ and HEAD, so you can\n+    select which parts to remove from the initial commit. The changes are\n+    placed into the index, leaving the working tree untouched.\n+<2> Now, you ammend the initial commit with the modifications that you just\n+    made in the index.\n+<3> Finally, you can add and then commit the final original unmodified files\n+    back as the second commit, enabling you to logically separate a commit\n+    into a sequence of two commits instead.\n+\n \n DISCUSSION\n ----------\n-- \n2.11.0.864.ge7592a54611d\n\n"},{"id":"310791","messageId":"CACsJy8B=OEdUbc3_Svci_whtk=-Bz-4BP4y-Xr_u3CU81dxvCA@mail.gmail.com","threadId":"45029","inReplyTo":"20170203003038.22278-1-jacob.e.keller@intel.com","subject":"Re: [PATCH] reset: add an example of how to split a commit into two","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2017-02-03T09:05:48Z","receivedAt":"2017-02-03T09:06:29Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Fri, Feb 3, 2017 at 7:30 AM, Jacob Keller <jacob.e.keller@intel.com> wrote:\n> From: Jacob Keller <jacob.keller@gmail.com>\n>\n> It is sometimes useful to break a commit into parts to more logically\n> show how the code changes. There are many possible ways to achieve this\n> result, but one simple and powerful one is to use git reset -p.\n>\n> Add an example to the documentation showing how this can be done so that\n> users are more likely to discover this, instead of inventing more\n> painful methods such as re-writing code from scratch, or duplicating git\n> add -p more times than necessary.\n>\n> Signed-off-by: Jacob Keller <jacob.keller@gmail.com>\n> ---\n>  Documentation/git-reset.txt | 23 +++++++++++++++++++++++\n>  1 file changed, 23 insertions(+)\n>\n> diff --git a/Documentation/git-reset.txt b/Documentation/git-reset.txt\n> index 25432d9257f9..4adac7a25bf9 100644\n> --- a/Documentation/git-reset.txt\n> +++ b/Documentation/git-reset.txt\n> @@ -292,6 +292,29 @@ $ git reset --keep start                    <3>\n>  <3> But you can use \"reset --keep\" to remove the unwanted commit after\n>      you switched to \"branch2\".\n>\n> +Split a commit into two::\n> ++\n> +Suppose that you have created a commit, but later decide that you want to split\n> +the changes into two separate commits, including only part of the old commit\n> +into the first commit, and including the rest as a separate commit on top. You\n> +can use git reset in patch mode to interactively select which hunks to split\n> +out into a separate commit.\n> ++\n> +------------\n> +$ git reset -p HEAD^                        <1>\n\nFor good practice, perhaps put \"git diff --cached HEAD^\" before \"git commit\".\n\nI tend to avoid \"reset -p\" and \"checkout -p\" though because sometimes\nit does not work. Not sure if it's just me, I think it may have\nsomething to do with splitting hunks. So I usually go with \"reset\nHEAD^\" then \"add -p\" and \"commit -c HEAD@{1}\" instead.\n\n> +$ git commit --amend                        <2>\n> +$ git commit ...                            <3>\n> +------------\n> ++\n> +<1> This lets you interactively undo changes between HEAD^ and HEAD, so you can\n> +    select which parts to remove from the initial commit. The changes are\n> +    placed into the index, leaving the working tree untouched.\n> +<2> Now, you ammend the initial commit with the modifications that you just\n\ns/ammend/amend/\n\n> +    made in the index.\n> +<3> Finally, you can add and then commit the final original unmodified files\n> +    back as the second commit, enabling you to logically separate a commit\n> +    into a sequence of two commits instead.\n-- \nDuy\n"},{"id":"310815","messageId":"xmqqlgtnf6ce.fsf@gitster.mtv.corp.google.com","threadId":"45029","inReplyTo":"CACsJy8B=OEdUbc3_Svci_whtk=-Bz-4BP4y-Xr_u3CU81dxvCA@mail.gmail.com","subject":"Re: [PATCH] reset: add an example of how to split a commit into two","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2017-02-03T18:59:45Z","receivedAt":"2017-02-03T18:59:51Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Duy Nguyen <pclouds@gmail.com> writes:\n\n>> +$ git reset -p HEAD^                        <1>\n>\n> For good practice, perhaps put \"git diff --cached HEAD^\" before \"git commit\".\n>\n> I tend to avoid \"reset -p\" and \"checkout -p\" though because sometimes\n> it does not work. Not sure if it's just me, I think it may have\n> something to do with splitting hunks. So I usually go with \"reset\n> HEAD^\" then \"add -p\" and \"commit -c HEAD@{1}\" instead.\n\nPerhaps I am superstitious, but I do that, too.  \n\nDoing this that way, the users do not need to learn \"reset -p\" or\n\"checkout -p\" and only need to know \"add -p\", and not having to\nlearn two extra things is a big plus.  On the other hand, it\nrequires the users to learn the reflog, but that knowledge extends\nto the use outside of the -c option of \"commit\" command, so overall\nI think it is a win.\n\n\n>> +$ git commit --amend                        <2>\n>> +$ git commit ...                            <3>\n>> +------------\n>> ++\n>> +<1> This lets you interactively undo changes between HEAD^ and HEAD, so you can\n>> +    select which parts to remove from the initial commit. The changes are\n>> +    placed into the index, leaving the working tree untouched.\n>> +<2> Now, you ammend the initial commit with the modifications that you just\n>\n> s/ammend/amend/\n>\n>> +    made in the index.\n>> +<3> Finally, you can add and then commit the final original unmodified files\n>> +    back as the second commit, enabling you to logically separate a commit\n>> +    into a sequence of two commits instead.\n"},{"id":"310816","messageId":"CA+P7+xqGnS+aYSQGp4kfnhY6eGdV6Y2=RfzFsxCX+cFzHYpvHQ@mail.gmail.com","threadId":"45029","inReplyTo":"xmqqlgtnf6ce.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH] reset: add an example of how to split a commit into two","fromName":"Jacob Keller","fromEmail":"jacob.keller@gmail.com","sentAt":"2017-02-03T20:15:51Z","receivedAt":"2017-02-03T20:16:19Z","isPatch":true,"sender":{"key":"jacob.keller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/874719?v=4"},"body":"On Fri, Feb 3, 2017 at 10:59 AM, Junio C Hamano <gitster@pobox.com> wrote:\n> Duy Nguyen <pclouds@gmail.com> writes:\n>\n>>> +$ git reset -p HEAD^                        <1>\n>>\n>> For good practice, perhaps put \"git diff --cached HEAD^\" before \"git commit\".\n>>\n>> I tend to avoid \"reset -p\" and \"checkout -p\" though because sometimes\n>> it does not work. Not sure if it's just me, I think it may have\n>> something to do with splitting hunks. So I usually go with \"reset\n>> HEAD^\" then \"add -p\" and \"commit -c HEAD@{1}\" instead.\n>\n> Perhaps I am superstitious, but I do that, too.\n>\n> Doing this that way, the users do not need to learn \"reset -p\" or\n> \"checkout -p\" and only need to know \"add -p\", and not having to\n> learn two extra things is a big plus.  On the other hand, it\n> requires the users to learn the reflog, but that knowledge extends\n> to the use outside of the -c option of \"commit\" command, so overall\n> I think it is a win.\n>\n>\n\nI didn't know about the \"-c\" option to git commit, that makes the add\n-p path a bit more straight forward. I'll re-word the example to use\nthis new suggested flow.\n\nThanks,\nJake\n"}]}