Wrote about branches
[gitmagic/gitmagic.git] / branch.txt
blob04ccc76d437f68e3a2ce6b1afd102456dff50953
1 = Branch Magic =
3 Now that you've mastered some simple Git sleights, it's time to learn some showstoppers.
5 External factors inevitably necessitate context switching. A severe bug manifests in the released version suddenly without warning, and must be fixed as soon as possible at all costs. The guy who wrote a certain function is about to leave, so you should drop what you are doing and get him to help you with your changes that affect his code.
7 Interrupting your train of thought can be detrimental to your productivity, and the slower and less convenient it is to switch contexts, the greater the loss. With centralized version control we must download a fresh working copy from the central server. Distributed systems fare much better, as we can clone the repository up to the desired version locally.
9 But cloning still entails copying the whole working directory as well as the entire history up to the given point. Even though Git reduces the cost of this with file sharing and hard links, there are some files we cannot link nor share. The project files must be recreated in their entirety in the new working directory, so we may edit them.
11 Happily, Git has a better tool for these situations that is much faster and more space-efficient than cloning: <tt>git-branch</tt>.
13 == The Boss Key ==
15 Ever play one of those games where you could hit a special key combination at
16 any time, and the screen would instantly display a spreadsheet or something? So if the boss walked in the office while you were playing the game you could quickly hide this fact?
18 In some directory:
20  $ echo "I'm smarter than my boss" > myfile.txt
21  $ git-init-db
22  $ git-add .
23  $ git-commit -m "Initial commit"
25 So we have created a Git repository that tracks one text file containing a certain message. Now type:
27  $ git checkout -b boss  # nothing seems to change after this
28  $ echo "My boss is smarter than me" > myfile.txt
29  $ git-commit -a -m "Another commit"
31 It looks like we've just overwritten our file and committed it. But it's an illusion. Type:
33  $ git checkout master  # switch to original version of the file
35 and hey presto! The text file is restored. And if the boss decides to snoop around this directory, type:
37  $ git checkout boss  # switch to version suitable for boss' eyes
39 You can switch between the two versions of the file as much as you like, and commit to each independently.
41 One can imagine reasons to use this that have nothing to do with source code management. Perhaps you have a program that reads data from a certain directory, and every now and then you'd like to switch the data back and forth without reconfiguring the program.
43 == Dirty Work ==
45 Say you're working on some feature, and for some reason, you need to go back to an old version and temporarily need to put in a few prints statements to see how something works. Then:
47  $ git commit -a
48  $ git checkout -b dirty SHA1_HASH
50 Now you can add ugly temporary code all over the place. When you're done,
52  $ git checkout master
54 to return to your original work, and:
56  $ git branch -d dirty
58 to remove the "dirty" branch, and all traces of the temporary code.
60 == Quick Fixes ==
62 This time, say you're in the middle of something and you are required to drop everything and work on fixing a bug that's just been reported:
64  $ git commit -a
65  $ git checkout -b fixes SHA1_HASH
67 Then once you've fixed the bug:
69  $ git commit -a -m "bug fixed"
70  $ git push  # to the central repository
71  $ git checkout master
73 ...and resume work on your original task.
75 == Working While Being Reviewed ==
77 Some projects require your code to be reviewed before you may submit it. To make life easier for those reviewing your code, if you have a big change to make you might break it into two or more parts, and get each parts separately reviewed.
79 What if the second part cannot be written until the first part is approved and checked in? In many version control systems, you'd have to send the first part to the reviewers, and then wait until it has been approved before starting on the second part.
81 Actually that's not quite true, but in these systems editing part two before part one has been submitted involves a lot of suffering and hardship. In Git, branching and merging are painless. So after you've committed the first part and sent it for review:
83  $ git checkout -b part2
85 Next, code the second part of the big change while you're waiting for the first part to be accepted. When the first part is approved and submitted,
87  $ git checkout master
88  $ git merge part2
89  $ git branch -d part2
91 and the second part of the change is ready to review.
93 But wait! What if it wasn't that simple? Say you made a mistake in the first part, which you have to correct before submitting. No problem! First, switch back to the master branch with
94  $ git checkout master
96 Fix the issue with the first part of the change and hope it gets approved. If not we simply repeat this step.
98 Eventually, once the first part has been approved and submitted:
100  $ git merge part2
102 and again, the second part is ready to be reviewed.
104 == Managing Branches ==
106 Type:
108  $ git branch
110 to list all the branches. There is always a branch named "master" which is where you start by default. This command can also do other operations with branches. See <tt>git help branch</tt> for details.
112 == An Analogy ==
114 Several Linux window managers allow you to have multiple desktops, which means you can instantly view a different state of the desktop.
116 Git branching is like multiple desktops for your working directory. With this analogy Git cloning is like attaching another monitor on which you can open more windows.
118 Perhaps a better example is the famous [[http://TODO][<tt>screen</tt>]] utitlity. This gem allows you to create multiple terminal sessions in the same terminal. Instead of opening new terminals, you can use the same one if you run <tt>screen</tt>. In fact, you can do a lot more with <tt>screen</tt> but that's a topic for another day.