{"thread":{"id":"45039","subject":"[PATCH v2] reset: add an example of how to split a commit into two","startedAt":"2017-02-03T20:28:41Z","lastAt":"2017-02-04T12:37:26Z","messageCount":5,"participants":["Jacob Keller","Junio C Hamano","Duy Nguyen","Philip Oakley"],"isPatch":true,"patchVersion":2,"patchTotal":null},"messages":[{"id":"310817","messageId":"20170203202833.17666-1-jacob.e.keller@intel.com","threadId":"45039","inReplyTo":null,"subject":"[PATCH v2] reset: add an example of how to split a commit into two","fromName":"Jacob Keller","fromEmail":"jacob.e.keller@intel.com","sentAt":"2017-02-03T20:28:33Z","receivedAt":"2017-02-03T20:28:41Z","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 often useful to break a commit into multiple parts that are more\nlogical separations. This can be tricky to learn how to do without the\nbrute-force method if re-writing code or commit messages from scratch.\n\nAdd a section to the git-reset documentation which shows an example\nprocess for how to use git add -p and git commit -c HEAD@{1} to\ninteractively break a commit apart and re-use the original commit\nmessage as a starting point when making the new commit message.\n\nSigned-off-by: Jacob Keller <jacob.keller@gmail.com>\n---\n Documentation/git-reset.txt | 38 ++++++++++++++++++++++++++++++++++++++\n 1 file changed, 38 insertions(+)\n\ndiff --git a/Documentation/git-reset.txt b/Documentation/git-reset.txt\nindex 25432d9257f9..add6220fce77 100644\n--- a/Documentation/git-reset.txt\n+++ b/Documentation/git-reset.txt\n@@ -292,6 +292,44 @@ $ 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 break\n+apart the changes into two logical chunks and commit each separately. You want\n+to include part of the original commit into the first commit, while including\n+the remainder in a second commit. You can use git reset to rewind the history\n+without changing the index, and then use git add -p to interactively select\n+which hunks to put into the first commit.\n++\n+------------\n+$ git reset HEAD^                           <1>\n+$ git add -p                                <2>\n+$ git diff --cached                         <3>\n+$ git commit -c HEAD@{1}                    <4>\n+...\n+$ git add ...                               <5>\n+$ git diff --cached                         <6>\n+$ git commit ...                            <7>\n+------------\n++\n+<1> First, reset the history back one commit so that we remove the original\n+    commit, but leave the working tree with all the changes.\n+<2> Now, interactively select hunks to add to a new commit using git add -p.\n+    This will ask for each hunk separately and you can use simple commands like\n+    \"yes, include\", \"no don't include\" or even \"edit\".\n+<3> Once satisfied with the hunks, you should verify that it is what you\n+    expected by using git diff --cached to show all changes in the index.\n+<4> Next, commit the changes stored in the index. \"-c\" specifies to load the\n+    editor with a commit message from a previous commit so that you can re-use the\n+    original commit message. HEAD@{1} is special notation to reference what\n+    HEAD used to be prior to the reset command. See linkgit:git-reflog[1] for\n+    more details.\n+<5> Now you've created the first commit, and can repeat steps 2-4 as often as\n+    you like to break the work into any number of commits. Here we show a second\n+    step which simply adds the remaining changes.\n+<6> Then check again that the changes are what you expected to add.\n+<7> And finally commit the remaining changes.\n+\n \n DISCUSSION\n ----------\n-- \n2.11.0.864.ge7592a54611d\n\n"},{"id":"310818","messageId":"xmqqzii3dmub.fsf@gitster.mtv.corp.google.com","threadId":"45039","inReplyTo":"20170203202833.17666-1-jacob.e.keller@intel.com","subject":"Re: [PATCH v2] reset: add an example of how to split a commit into two","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2017-02-03T20:46:20Z","receivedAt":"2017-02-03T20:46:31Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jacob Keller <jacob.e.keller@intel.com> writes:\n\n> +Split a commit into two::\n\nLet's say \"...into two (or more)\" to match what appears in\n\"SPLITTING COMMITS\" section of \"rebase -i\" documentation.  Yours is\nwritten as a sequence of more concrete steps than the existing one\nover there, so it may also make sense to add reference to bring\nreaders of \"git rebase --help\" to this section.\n\n> ++\n> +Suppose that you have created a commit, but later decide that you want to break\n> +apart the changes into two logical chunks and commit each separately. You want\n\n\"two (or more)\" again.  In <5> you already hint that the user can\nrepeat 2-4 number of times.\n\n> +to include part of the original commit into the first commit, while including\n> +the remainder in a second commit. You can use git reset to rewind the history\n> +without changing the index, and then use git add -p to interactively select\n> +which hunks to put into the first commit.\n> ++\n> +------------\n> +$ git reset HEAD^                           <1>\n> +$ git add -p                                <2>\n> +$ git diff --cached                         <3>\n> +$ git commit -c HEAD@{1}                    <4>\n> +...\n> +$ git add ...                               <5>\n> +$ git diff --cached                         <6>\n> +$ git commit ...                            <7>\n> +------------\n> ++\n> +<1> First, reset the history back one commit so that we remove the original\n> +    commit, but leave the working tree with all the changes.\n> +<2> Now, interactively select hunks to add to a new commit using git add -p.\n> +    This will ask for each hunk separately and you can use simple commands like\n> +    \"yes, include\", \"no don't include\" or even \"edit\".\n> +<3> Once satisfied with the hunks, you should verify that it is what you\n> +    expected by using git diff --cached to show all changes in the index.\n> +<4> Next, commit the changes stored in the index. \"-c\" specifies to load the\n> +    editor with a commit message from a previous commit so that you can re-use the\n> +    original commit message. HEAD@{1} is special notation to reference what\n> +    HEAD used to be prior to the reset command. See linkgit:git-reflog[1] for\n> +    more details.\n> +<5> Now you've created the first commit, and can repeat steps 2-4 as often as\n> +    you like to break the work into any number of commits. Here we show a second\n> +    step which simply adds the remaining changes.\n> +<6> Then check again that the changes are what you expected to add.\n> +<7> And finally commit the remaining changes.\n> +\n"},{"id":"310844","messageId":"CACsJy8B2gyw7RQBh6Qfm5HxOyKWted-0ZeDfd2_U3MvWCO1HEA@mail.gmail.com","threadId":"45039","inReplyTo":"20170203202833.17666-1-jacob.e.keller@intel.com","subject":"Re: [PATCH v2] reset: add an example of how to split a commit into two","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2017-02-04T11:06:17Z","receivedAt":"2017-02-04T11:06:52Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Sat, Feb 4, 2017 at 3:28 AM, Jacob Keller <jacob.e.keller@intel.com> wrote:\n> +------------\n> +$ git reset HEAD^                           <1>\n\nIt may be a good idea to add -N here, so that 'add -p' can pick up the\nnew files if they are added in HEAD.\n\n> +$ git add -p                                <2>\n-- \nDuy\n"},{"id":"310850","messageId":"E363F108ABEF45CB99C2C721A90B5EA2@PhilipOakley","threadId":"45039","inReplyTo":"CACsJy8B2gyw7RQBh6Qfm5HxOyKWted-0ZeDfd2_U3MvWCO1HEA@mail.gmail.com","subject":"Re: [PATCH v2] reset: add an example of how to split a commit into two","fromName":"Philip Oakley","fromEmail":"philipoakley@iee.org","sentAt":null,"receivedAt":"2017-02-04T12:16:28Z","isPatch":true,"sender":{"key":"philipoakley@iee.email","avatar":"https://avatars.githubusercontent.com/u/914343?v=4"},"body":"From: \"Duy Nguyen\" <pclouds@gmail.com>\n> On Sat, Feb 4, 2017 at 3:28 AM, Jacob Keller <jacob.e.keller@intel.com> \n> wrote:\n>> +------------\n>> +$ git reset HEAD^                           <1>\n>\n> It may be a good idea to add -N here, so that 'add -p' can pick up the\n> new files if they are added in HEAD.\n\nWhen looking at the man page for `reset` [1] it implies that `-N` requires \n`--mixed` also to be given. Is that correct? Or could the man page be \nclearer?\n\n>\n>> +$ git add -p                                <2>\n> -- \n> Duy\n>\nPhilip\n[1] https://git-scm.com/docs/git-reset \n\n"},{"id":"310852","messageId":"CACsJy8Bk=pnyYNr2OxhQCYgxQQtOU=GqXUfgseL1HatB6SQPnQ@mail.gmail.com","threadId":"45039","inReplyTo":"E363F108ABEF45CB99C2C721A90B5EA2@PhilipOakley","subject":"Re: [PATCH v2] reset: add an example of how to split a commit into two","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2017-02-04T12:36:49Z","receivedAt":"2017-02-04T12:37:26Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Sat, Feb 4, 2017 at 7:16 PM, Philip Oakley <philipoakley@iee.org> wrote:\n> From: \"Duy Nguyen\" <pclouds@gmail.com>\n>>\n>> On Sat, Feb 4, 2017 at 3:28 AM, Jacob Keller <jacob.e.keller@intel.com>\n>> wrote:\n>>>\n>>> +------------\n>>> +$ git reset HEAD^                           <1>\n>>\n>>\n>> It may be a good idea to add -N here, so that 'add -p' can pick up the\n>> new files if they are added in HEAD.\n>\n>\n> When looking at the man page for `reset` [1] it implies that `-N` requires\n> `--mixed` also to be given. Is that correct?\n\nYes. But since --mixed is the default mode, \"reset -N\" equals \"reset --mixed -N\"\n\n> Or could the man page be clearer?\n\nIf someone makes questions, I guess the answer is yes it should be made clearer.\n-- \nDuy\n"}]}