@@ -292,6 +292,54 @@ $ git reset --keep start <3>
292
292
<3> But you can use "reset --keep" to remove the unwanted commit after
293
293
you switched to "branch2".
294
294
295
+ Split a commit apart into a sequence of commits::
296
+ +
297
+ Suppose that you have created lots of logically separate changes and commited
298
+ them together. Then, later you decide that it might be better to have each
299
+ logical chunk associated with its own commit. You can use git reset to rewind
300
+ history without changing the contents of your local files, and then successively
301
+ use `git add -p` to interactively select which hunks to include into each commit,
302
+ using `git commit -c` to pre-populate the commit message.
303
+ +
304
+ ------------
305
+ $ git reset -N HEAD^ <1>
306
+ $ git add -p <2>
307
+ $ git diff --cached <3>
308
+ $ git commit -c HEAD@{1} <4>
309
+ ... <5>
310
+ $ git add ... <6>
311
+ $ git diff --cached <7>
312
+ $ git commit ... <8>
313
+ ------------
314
+ +
315
+ <1> First, reset the history back one commit so that we remove the original
316
+ commit, but leave the working tree with all the changes. The -N ensures
317
+ that any new files added with HEAD are still marked so that git add -p
318
+ will find them.
319
+ <2> Next, we interactively select diff hunks to add using the git add -p
320
+ facility. This will ask you about each diff hunk in sequence and you can
321
+ use simple commands such as "yes, include this", "No don't include this"
322
+ or even the very powerful "edit" facility.
323
+ <3> Once satisfied with the hunks you want to include, you should verify what
324
+ has been prepared for the first commit by using git diff --cached. This
325
+ shows all the changes that have been moved into the index and are about
326
+ to be committed.
327
+ <4> Next, commit the changes stored in the index. The -c option specifies to
328
+ pre-populate the commit message from the original message that you started
329
+ with in the first commit. This is helpful to avoid retyping it. The HEAD@{1}
330
+ is a special notation for the commit that HEAD used to be at prior to the
331
+ original reset commit (1 change ago). See linkgit:git-reflog[1] for more
332
+ details. You may also use any other valid commit reference.
333
+ <5> You can repeat steps 2-4 multiple times to break the original code into
334
+ any number of commits.
335
+ <6> Now you've split out many of the changes into their own commits, and might
336
+ no longer use the patch mode of git add, in order to select all remaining
337
+ uncommitted changes.
338
+ <7> Once again, check to verify that you've included what you want to. You may
339
+ also wish to verify that git diff doesn't show any remaining changes to be
340
+ committed later.
341
+ <8> And finally create the final commit.
342
+
295
343
296
344
DISCUSSION
297
345
----------
0 commit comments