aafd2ec3b22ca3d58b163c6e4efd994e11ed015d
[gitmagic.git] / pl / multiplayer.txt
blobaafd2ec3b22ca3d58b163c6e4efd994e11ed015d
1 == Multiplayer Git ==
3 Initially I used Git on a private project where I was the sole developer.
4 Amongst the commands related to Git's distributed nature, I needed only *pull*
5 and *clone* so could I keep the same project in different places.
7 Later I wanted to publish my code with Git, and include changes from
8 contributors. I had to learn how to manage projects with multiple developers
9 from all over the world. Fortunately, this is Git's forte, and arguably its
10 raison d'ĂȘtre.
12 === Who Am I? ===
14 Every commit has an author name and email, which is shown by *git log*.
15 By default, Git uses system settings to populate these fields.
16 To set them explicitly, type:
18   $ git config --global user.name "John Doe"
19   $ git config --global user.email johndoe@example.com
21 Omit the global flag to set these options only for the current repository.
23 === Git Over SSH, HTTP ===
25 Suppose you have SSH access to a web server, but Git is not installed. Though
26 less efficient than its native protocol, Git can communicate over HTTP.
28 Download, compile and install Git in your account, and create a repository in
29 your web directory:
31  $ GIT_DIR=proj.git git init
32  $ cd proj.git
33  $ git --bare update-server-info
34  $ cp hooks/post-update.sample hooks/post-update
36 For older versions of Git, the copy command fails and you should run:
38  $ chmod a+x hooks/post-update
40 Now you can publish your latest edits via SSH from any clone:
42  $ git push web.server:/path/to/proj.git master
44 and anybody can get your project with:
46  $ git clone http://web.server/proj.git
48 === Git Over Anything ===
50 Want to synchronize repositories without servers, or even a network connection?
51 Need to improvise during an emergency? We've seen <<makinghistory, *git
52 fast-export* and *git fast-import* can convert repositories to a single file
53 and back>>. We could shuttle such files back and forth to transport git
54 repositories over any medium, but a more efficient tool is *git bundle*.
56 The sender creates a 'bundle':
58  $ git bundle create somefile HEAD
60 then transports the bundle, +somefile+, to the other party somehow: email,
61 thumb drive, an *xxd* printout and an OCR scanner, reading bits over the phone,
62 smoke signals, etc. The receiver retrieves commits from the bundle by typing:
64  $ git pull somefile
66 The receiver can even do this from an empty repository. Despite its
67 size, +somefile+ contains the entire original git repository.
69 In larger projects, eliminate waste by bundling only changes the other
70 repository lacks. For example, suppose the commit ``1b6d...'' is the most
71 recent commit shared by both parties:
73  $ git bundle create somefile HEAD ^1b6d
75 If done frequently, one could easily forget which commit was last sent. The
76 help page suggests using tags to solve this. Namely, after you send a bundle,
77 type:
79  $ git tag -f lastbundle HEAD
81 and create new refresher bundles with:
83  $ git bundle create newbundle HEAD ^lastbundle
85 === Patches: The Global Currency ===
87 Patches are text representations of your changes that can be easily understood
88 by computers and humans alike. This gives them universal appeal. You can email a
89 patch to developers no matter what version control system they're using. As long
90 as your audience can read their email, they can see your edits. Similarly, on
91 your side, all you require is an email account: there's no need to setup an online Git repository.
93 Recall from the first chapter:
95  $ git diff 1b6d > my.patch
97 outputs a patch which can be pasted into an email for discussion. In a Git
98 repository, type:
100  $ git apply < my.patch
102 to apply the patch.
104 In more formal settings, when author names and perhaps signatures should be
105 recorded, generate the corresponding patches past a certain point by typing:
107  $ git format-patch 1b6d
109 The resulting files can be given to *git-send-email*, or sent by hand. You can also specify a range of commits:
111  $ git format-patch 1b6d..HEAD^^
113 On the receiving end, save an email to a file, then type:
115  $ git am < email.txt
117 This applies the incoming patch and also creates a commit, including information such as the author.
119 With a browser email client, you may need to click a button to see the email in its raw original form before saving the patch to a file.
121 There are slight differences for mbox-based email clients, but if you use one
122 of these, you're probably the sort of person who can figure them out easily
123 without reading tutorials!
125 === Sorry, We've Moved ===
127 After cloning a repository, running *git push* or *git pull* will automatically
128 push to or pull from the original URL. How does Git do this? The secret lies in
129 config options created with the clone. Let's take a peek:
131  $ git config --list
133 The +remote.origin.url+ option controls the source URL; ``origin'' is a nickname
134 given to the source repository. As with the ``master'' branch convention, we may
135 change or delete this nickname but there is usually no reason for doing so.
137 If the original repository moves, we can update the URL via:
139  $ git config remote.origin.url git://new.url/proj.git
141 The +branch.master.merge+ option specifies the default remote branch in
142 a *git pull*. During the initial clone, it is set to the current branch of the
143 source repository, so even if the HEAD of the source repository subsequently
144 moves to a different branch, a later pull will faithfully follow the
145 original branch.
147 This option only applies to the repository we first cloned from, which is
148 recorded in the option +branch.master.remote+. If we pull in from other
149 repositories we must explicitly state which branch we want:
151  $ git pull git://example.com/other.git master
153 The above explains why some of our earlier push and pull examples had no
154 arguments.
156 === Remote Branches ===
158 When you clone a repository, you also clone all its branches. You may not have
159 noticed this because Git hides them away: you must ask for them specifically.
160 This prevents branches in the remote repository from interfering with
161 your branches, and also makes Git easier for beginners.
163 List the remote branches with:
165  $ git branch -r
167 You should see something like:
169  origin/HEAD
170  origin/master
171  origin/experimental
173 These represent branches and the HEAD of the remote repository, and can be used
174 in regular Git commands. For example, suppose you have made many commits, and
175 wish to compare against the last fetched version. You could search through the
176 logs for the appropriate SHA1 hash, but it's much easier to type:
178  $ git diff origin/HEAD
180 Or you can see what the ``experimental'' branch has been up to:
182  $ git log origin/experimental
184 === Multiple Remotes ===
186 Suppose two other developers are working on our project, and we want to
187 keep tabs on both. We can follow more than one repository at a time with:
189  $ git remote add other git://example.com/some_repo.git
190  $ git pull other some_branch
192 Now we have merged in a branch from the second repository, and we have
193 easy access to all branches of all repositories:
195  $ git diff origin/experimental^ other/some_branch~5
197 But what if we just want to compare their changes without affecting our own
198 work? In other words, we want to examine their branches without having
199 their changes invade our working directory. Then rather than pull, run:
201  $ git fetch        # Fetch from origin, the default.
202  $ git fetch other  # Fetch from the second programmer.
204 This just fetches histories. Although the working directory remains untouched,
205 we can refer to any branch of any repository in a Git command because we now
206 possess a local copy.
208 Recall that behind the scenes, a pull is simply a *fetch* then *merge*.
209 Usually we *pull* because we want to merge the latest commit after a fetch;
210 this situation is a notable exception.
212 See *git help remote* for how to remove remote repositories, ignore certain
213 branches, and more.
215 === My Preferences ===
217 For my projects, I like contributors to prepare repositories from which I can
218 pull. Some Git hosting services let you host your own fork of a project with
219 the click of a button.
221 After I fetch a tree, I run Git commands to navigate and examine the changes,
222 which ideally are well-organized and well-described. I merge my own changes,
223 and perhaps make further edits. Once satisfied, I push to the main repository.
225 Though I infrequently receive contributions, I believe this approach scales
226 well. See
227 http://torvalds-family.blogspot.com/2009/06/happiness-is-warm-scm.html[this
228 blog post by Linus Torvalds].
230 Staying in the Git world is slightly more convenient than patch files, as it
231 saves me from converting them to Git commits. Furthermore, Git handles details
232 such as recording the author's name and email address, as well as the time and
233 date, and asks the author to describe their own change.