Merge git://github.com/doppelganger/gitmagic
[gitmagic.git] / en / multiplayer.txt
blobf24d8face4d2723b572d90856d2fb2fb105558b3
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
33 In the "proj.git" directory, run:
35  $ git --bare update-server-info
36  $ cp hooks/post-update.sample hooks/post-update
38 For older versions of Git, the copy command fails and you should run:
40  $ chmod a+x hooks/post-update
42 Now you can publish your latest edits via SSH from any clone:
44  $ git push web.server:/path/to/proj.git master
46 and anybody can get your project with:
48  $ git clone http://web.server/proj.git
50 === Git Over Anything ===
52 Want to synchronize repositories without servers, or even a network connection?
53 Need to improvise during an emergency? We've seen <<makinghistory, *git
54 fast-export* and *git fast-import* can convert repositories to a single file
55 and back>>. We could shuttle such files back and forth to transport git
56 repositories over any medium, but a more efficient tool is *git bundle*.
58 The sender creates a 'bundle':
60  $ git bundle create somefile HEAD
62 then transports the bundle, +somefile+, to the other party somehow: email,
63 thumb drive, floppy disk, an *xxd* printout and an OCR machine,
64 reading bits over the phone, smoke signals, etc. The receiver retrieves
65 commits from the bundle by typing:
67  $ git pull somefile
69 The receiver can even do this from an empty repository. Despite its
70 size, +somefile+ contains the entire original git repository.
72 In larger projects, eliminate waste by bundling only changes the other
73 repository lacks:
75  $ git bundle create somefile HEAD ^COMMON_SHA1
77 If done frequently, one could easily forget which commit was last sent. The
78 help page suggests using tags to solve this. Namely, after you send a bundle,
79 type:
81  $ git tag -f lastbundle HEAD
83 and create new refresher bundles with:
85  $ git bundle create newbundle HEAD ^lastbundle
87 === Patches: The Global Currency ===
89 Patches are text representations of your changes that can be easily understood
90 by computers and humans alike. This gives them universal appeal. You can email a
91 patch to developers no matter what version control system they're using. As long
92 as your audience can read their email, they can see your edits. Similarly, on
93 your side, all you require is an email account: there's no need to setup an online Git repository.
95 Recall from the first chapter:
97  $ git diff COMMIT
99 outputs a patch which can be pasted into an email for discussion. In a Git
100 repository, type:
102  $ git apply < FILE
104 to apply the patch.
106 In more formal settings, when author names and perhaps signatures should be
107 recorded, generate the corresponding patches past a certain point by typing:
109  $ git format-patch START_COMMIT
111 The resulting files can be given to *git-send-email*, or sent by hand. You can also specify a range of commits:
113  $ git format-patch START_COMMIT..END_COMMIT
115 On the receiving end, save an email to a file, then type:
117  $ git am < FILE
119 This applies the incoming patch and also creates a commit, including information such as the author.
121 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.
123 There are slight differences for mbox-based email clients, but if you use one
124 of these, you're probably the sort of person who can figure them out easily
125 without reading tutorials!
127 === Sorry, We've Moved ===
129 After cloning a repository, running *git push* or *git pull* will automatically
130 push to or pull from the original URL. How does Git do this? The secret lies in
131 config options initialized created with the clone. Let's take a peek:
133  $ git config --list
135 The +remote.origin.url+ option controls the source URL; "origin" is a nickname
136 given to the source repository. As with the "master" branch convention, we may
137 change or delete this nickname but there is usually no reason for doing so.
139 If the original repository moves, we can update the URL via:
141  $ git config remote.origin.url NEW_URL
143 The +branch.master.merge+ option specifies the default remote branch in
144 a *git pull*. During the initial clone, it is set to the current branch of the
145 source repository, so even if the HEAD of the source repository subsequently
146 moves to a different branch, a later pull will faithfully follow the
147 original branch.
149 This option only applies to the repository we first cloned from, which is
150 recorded in the option +branch.master.remote+. If we pull in from other
151 repositories we must explicitly state which branch we want:
153  $ git pull ANOTHER_URL master
155 The above explains why some of our earlier push and pull examples had no
156 arguments.
158 === Remote Branches ===
160 When you clone a repository, you also clone all its branches. You may not have
161 noticed this because Git hides them away: you must ask for them specifically.
162 This prevents branches in the remote repository from interfering with
163 your branches, and also makes Git easier for beginners.
165 List the remote branches with:
167  $ git branch -r
169 You should see something like:
171  origin/HEAD
172  origin/master
173  origin/experimental
175 These represent branches and the HEAD of the remote repository, and can be used
176 in regular Git commands. For example, suppose you have made many commits, and
177 wish to compare against the last fetched version. You could search through the
178 logs for the appropriate SHA1 hash, but it's much easier to type:
180  $ git diff origin/HEAD
182 Or you can see what the "experimental" branch has been up to:
184  $ git log origin/experimental
186 === Multiple Remotes ===
188 Suppose two other developers are working on our project, and we want to
189 keep tabs on both. We can follow more than one repository at a time with:
191  $ git remote add other ANOTHER_URL
192  $ git pull other some_branch
194 Now we have merged in a branch from the second repository, and we have
195 easy access to all branches of all repositories:
197  $ git diff origin/experimental^ other/some_branch~5
199 But what if we just want to compare their changes without affecting our own
200 work? In other words, we want to examine their branches without having
201 their changes invade our working directory. In this case, rather than pull,
202 run:
204  $ git fetch        # Fetch from origin, the default.
205  $ git fetch other  # Fetch from the second programmer.
207 This fetches their histories and nothing more, so although the working
208 directory remains untouched, we can refer to any branch of any repository in
209 a Git command. By the way, behind the scenes, a pull is simply a fetch followed
210 by *git merge*; recall the latter merges a given commit into the working
211 directory. Usually we pull because we want to merge after a fetch; this
212 situation is a notable exception.
214 See *git help remote* for how to remove remote repositories, ignore certain
215 branches, and more.
217 === My Preferences ===
219 For my projects, I like contributors to prepare Git repositories which I can
220 pull. Some Git hosting services let you host your own fork of a project with
221 the click of a button.
223 After I fetch a tree, I run Git commands to navigate and examine the changes,
224 which ideally are well-organized and well-described. I merge unpublished
225 changes of my own, and perhaps make further edits. Once satisfied, I push to
226 the official repository.
228 Though I infrequently receive contributions, I believe this approach scales
229 well. See
230 http://torvalds-family.blogspot.com/2009/06/happiness-is-warm-scm.html[this
231 blog post by Linus Torvalds].
233 Staying in the Git world is slightly more convenient than patch files, as it
234 saves me the step of converting them to Git commits. Furthermore, Git
235 automatically handles details such as recording the author's name and email
236 address, as well as the time and date, and asks the author to describe
237 their own change.