1 From: Junio C Hamano <junkio@cox.net>
2 Subject: Separating topic branches
3 Abstract: In this article, JC describes how to separate topic branches.
5 This text was originally a footnote to a discussion about the
6 behaviour of the git diff commands.
8 Often I find myself doing that [running diff against something other
9 than HEAD] while rewriting messy development history. For example, I
10 start doing some work without knowing exactly where it leads, and end
11 up with a history like this:
18 At this point, "topic" contains something I know I want, but it
19 contains two concepts that turned out to be completely independent.
20 And often, one topic component is larger than the other. It may
21 contain more than two topics.
23 In order to rewrite this mess to be more manageable, I would first do
24 "diff master..topic", to extract the changes into a single patch, start
25 picking pieces from it to get logically self-contained units, and
26 start building on top of "master":
28 $ git diff master..topic >P.diff
29 $ git checkout -b topicA master
30 ... pick and apply pieces from P.diff to build
31 ... commits on topicA branch.
39 Before doing each commit on "topicA" HEAD, I run "diff HEAD"
40 before update-index the affected paths, or "diff --cached HEAD"
41 after. Also I would run "diff --cached master" to make sure
42 that the changes are only the ones related to "topicA". Usually
43 I do this for smaller topics first.
45 After that, I'd do the remainder of the original "topic", but
46 for that, I do not start from the patchfile I extracted by
47 comparing "master" and "topic" I used initially. Still on
48 "topicA", I extract "diff topic", and use it to rebuild the
51 $ git diff -R topic >P.diff ;# --cached also would work fine
52 $ git checkout -b topicB master
53 ... pick and apply pieces from P.diff to build
54 ... commits on topicB branch.
65 After I am done, I'd try a pretend-merge between "topicA" and
66 "topicB" in order to make sure I have not missed anything:
68 $ git pull . topicA ;# merge it into current "topicB"
71 o---o---o---o---o---* (pretend merge)
79 The last diff better not to show anything other than cleanups
80 for crufts. Then I can finally clean things up:
83 $ git reset --hard HEAD^ ;# nuke pretend merge