Update EGit eclipse help from wiki
[egit.git] / org.eclipse.egit.doc / help / EGit / Git_For_Eclipse_Users / Git-For-Eclipse-Users.html
blob94adb1785b4105481a1fba32dba656c3963abca0
1 <?xml version='1.0' encoding='utf-8' ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
5 <title>Git for Eclipse Users</title>
6 <link type="text/css" rel="stylesheet" href="../../book.css"/>
7 </head>
8 <body>
9 <table class="navigation" style="width: 100%;" border="0" summary="navigation">
10 <tr>
11 <th style="width: 100%" align="center" colspan="3">Git for Eclipse Users</th>
12 </tr>
13 <tr>
14 <td style="width: 20%" align="left"></td>
15 <td style="width: 60%" align="center"></td>
16 <td style="width: 20%" align="right">
17 <a href="Updating-This-Document.html" title="Updating This Document">
18 <img alt="Next" border="0" src="../../images/next.gif"/>
19 </a>
20 </td>
21 </tr>
22 <tr>
23 <td style="width: 20%" align="left" valign="top"></td>
24 <td style="width: 60%" align="center"></td>
25 <td style="width: 20%" align="right" valign="top">Updating This Document</td>
26 </tr>
27 </table><hr/>
28 <p>This post is aimed at those who have been using Eclipse for a while, and probably have been either using the baked-in CVS or external SVN providers to store their source code. The content of the post is about Git; what it means to you, as an Eclipse user – and specifically, how it affects how you obtain or work with projects from Eclipse.org.</p>
29 <p>This post is not about the relative merits of Git over CVS/SVN, or of Git versus other distributed version control systems (DVCS) like Mercurial (Hg). There's other sites which can give those flavours if needed.</p>
30 <p>Once you understand the conceptual differences between CVS/SVN and Git, and then subsequently start to use Git, you may find it very difficult to go back. You should only really start to experiment if you think you're going to migrate in the near future, because using Git is like watching TV in colour; once you've discovered it, it's really difficult to go back to black &amp; white.</p>
31 <dl>
32 <dd>&#9758;
33 <b>Once you start to use a DVCS, it's very difficult to want to go back</b>
34 </dd>
35 </dl>
36 <h2 id="Centralised_version_control_systems">Centralised version control systems</h2>
37 <p>So, what do you need to know about Git? Well, both CVS and SVN are known as
38 <i>centralised</i> version control systems (CVCS). That is, there is one Master repository which people share code at; everyone checks out their code (or branch) from that repository, and checks changes back in. For code that needs to be sent person-to-person (for example, for review, or as a way of contributing fixes), it is possible to create a
39 <i>patch</i>, which is a diff of your code against the given Master repository version (often HEAD, but sometimes a branch like Eclipse_35).
40 </p>
41 <p>There are two problems that surface with a centralised version control system; though those problems aren't immediately apparent or obvious.</p>
42 <ul>
43 <li>You need to be 'on-line' to perform actions, like diff or patch<sup>*</sup></li>
44 <li>Patches generated against a particular branch can become outdated fairly quickly as development of the snapshot-in-time branch moves on (e.g. when it comes to apply, HEAD is different to before)</li>
45 </ul>
46 <p>The first one is rarely apparent for those working with Eclipse in a location at (or near to) the repository itself. Those in the same continent will rarely experience delays due to global networking variance; in addition, they tend to be employed in an organisation and sat at a desktop connected to wired networking for most of the day. Road warriors (those with laptops and who code from the local coffee shop) tend to operate in a more frequently disconnected mode, which limits repository functionality to when they are connected.
47 <i>(*A quick note here on SVN; since SVN keeps the last-known-checkout, it's possible to do a limited set of operations whilst disconnected from SVN, like diff from the last-known-checkout. However, in general, you are prevented from doing many of the operations that are possible whilst connected.)</i>
48 </p>
49 <p>The second one is simply an artefact of the way in which patches work. These are generally performed against HEAD (a snapshot in time) and then applied later (sometimes, months or even
50 <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=4922" target="egit_external">eight years later</a>). Although they record the version of the file they were patched against, the patch itself is sensitive to big changes in the file, sometimes leading to the patch being inapplicable. Even relatively simple operations, like a file-rename, can throw a well-formed CVCS patch out of the window.
51 </p>
52 <h2 id="Distributed_Version_Control_Systems">Distributed Version Control Systems</h2>
53 <p>Distributed Version Control Systems (DVCS) are a different family of version control systems to those that most are familiar with. The two most popular are arguably
54 <a href="http://www.git-scm.org" target="egit_external">Git</a> and
55 <a href="http://mercurial.selenic.com" target="egit_external">Hg</a>, although many others (
56 <a href="http://darcs.net/" target="egit_external">Darcs</a>,
57 <a href="http://bazaar.canonical.com/en/" target="egit_external">Bazaar</a>,
58 <a href="http://www.bitkeeper.com/" target="egit_external">Bitkeeper</a> etc.) exist. Unlike centralised version control systems (where every individual checks into/out of a shared system), a distributed version control system shares out the data across each participant. Unlike
59 <a href="http://www.bittorrent.com/" target="egit_external">Bittorrent</a>, where the contents are scattered across various machines, in a DVCS each user has a full copy of the repository.
60 </p>
61 <dl>
62 <dd>&#9758;
63 <b>Each user has a full copy of the repository</b>
64 </dd>
65 </dl>
66 <p>This initially sounds impossible, especially if you're used to centralised version control systems, and even more so if they involve pessimistic file-based locking. (If you do firmly want pessimistic locking, please stop reading here. Thanks.) Questions arise, like:</p>
67 <ol>
68 <li>If everyone has a copy of the repository, don't all the forks diverge?</li>
69 <li>Where is the master repository kept?</li>
70 <li>Isn't the repository, like, really big?</li>
71 <li>No really, I like pessimistic locking.</li>
72 </ol>
73 <p>Let's answer each one of these questions in turn. (If I missed your favourite question, then please feel free to add one in the comments.)</p>
74 <ol>
75 <li>Yes, the forks
76 <i>can</i> diverge. But after all, open-source can diverge anyway. There's nothing stopping me from forking the <code>dev.eclipse.org</code> codebase, and publishing my own version of it called
77 <a href="http://sourceforge.net/projects/rcpapps/files/maclipse/" target="egit_external">Maclipse</a>. The key thing here is that whilst forks are possible,
78 <i>forking is not a bad thing in itself</i>. After all, look at Linux and Android; originally, they shared a history, but are now different. XFree86 and X.Org
79 <a href="http://www.x.org/wiki/XorgFoundation" target="egit_external">split</a> over licensing issues. MySQL was forked to create
80 <a href="http://askmonty.org/wiki/index.php/MariaDB" target="egit_external">MariaDB</a>, and so on.The key thing about forks is that the best survive. X.Org is now the default X client, whereas XFree86 was the default beforehand. The jury is still out on MySQL versus MariaDB. And although Maclipse has been downloaded literally <span title="Actually, about one and a half thousand. That&#39;s more than I expected.">tens of times</span>, it hasn't caused a dent in Eclipse's growth.
81 <dl>
82 <dd>&#9758;
83 <b>Forks happen</b>
84 </dd>
85 </dl>
86 </li>
87 <li>Do not try to bend the <span title="spoon">master repository</span> – that's impossible. Instead, only try to realise the truth; there is no <span title="spoon">master repository</span>.If fact, there's a veritable matrix of master repositories possible. Each repository can be considered a node in a graph; nodes in the graph can be connected to each other in any way. However, rather than an n-n set of links, the graph usually self-organises into a tree-like structure, logically associating with one point that acts as a funnel for everything else. In a sense, that's a master repository – everyone has already made the choice; now you have to understand it. Should an oracle intervene, a neo-master can be chosen.
88 <dl>
89 <dd>&#9758;
90 <b>There is no master repository</b>
91 </dd>
92 </dl>
93 </li>
94 <li>Having accepted that there is no master repository, it becomes clear that the repository must live in its entirety on each of the nodes in the DVCS. This usually leads to fears about the size of the repository, even taking into account the fact that storage is cheap.A key point here is that DVCS repositories are usually far
95 <i>smaller</i> than their counterpart CVCS repositories, not least of which is because everyone has to have a full repository in order to do any work. It's a natural consequence that they're smaller.However, they're also smaller because each repository contains far less scope than a CVCS repository. For example, most organisations will have one mammoth CVCS repository with several thousand top-level 'modules' (or 'projects') underneath. Because of the administrative overhead with 'creating a new repository', it is often easier to reuse the same one for everything. (SVN put some limits on how wide it could grow, which CVS tended not to have; but even so, the main
96 <a href="http://svn.apache.org/viewvc?view=revision&amp;revision=908283" target="egit_external">Apache SVN</a> is over 900k revisions.)By contrast, setting up a DVCS is usually nothing more than a directory with a few administrative files inside. It doesn't require administrator privileges or specific ports; in fact, since there's no central server to speak of, it doesn't even need to be shared by network protocols.As a result, a DVCS repository is much more granular – and easy to create – than a traditional CVCS repository. Firstly, it's always on your machine (there's no centralised server to configure) and secondly, all you need access to is a file system. So typically, a DVCS “repository” will often be at the level of an Eclipse project or project working set. For example, although the [
97 <a href="http://dev.eclipse.org/viewcvs/index.cgi/?root=RT_Project" target="egit_external">http://dev.eclipse.org/viewcvs/index.cgi/?root=RT_Project</a> CVS <span title="RunTime">RT</span> repository] is shared by
98 <a href="http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.equinox/?root=RT_Project" target="egit_external">Equinox</a> and
99 <a href="http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.ecf/?root=RT_Project&amp;view=log" target="egit_external">ECF</a>, a DVCS-based solution would almost certainly see the Equinox and ECF projects in their own repositories; perhaps, even breaking down further into (say) ECF-Doc and ECF-Bundles. Think of a DVCS repository as one or a few Eclipse projects instead of hundreds of projects together.
100 <dl>
101 <dd>&#9758;
102 <b>DVCS repositories are much smaller, typically because they only contain a small number of highly-related projects</b>
103 </dd>
104 </dl>
105 </li>
106 <li>That's not a question. Look, if you want the benefits of a centralised DVCS with pessimistic locking and pessimistic users, then go look at
107 <a href="http://www-01.ibm.com/software/awdtools/clearcase/" target="egit_external">ClearCase</a>.
108 <dl>
109 <dd>&#9758;
110 <b>Friends don't let friends use ClearCase</b>
111 </dd>
112 </dl>
113 </li>
114 </ol>
115 <h2 id="How_does_it_work.3F">How does it work?</h2>
116 <p>There are two pieces of information that identify elements in a CVCS; a file's
117 <i>name</i>, and its
118 <i>version</i> (sometimes called
119 <i>revision</i>). In the case of CVS, each file has its own version stream (1.1, 1.2, 1.3), whilst in SVN, each changeset has a 'repository revision' number. Tags (or branches) are symbolic identifiers which may be attached to any specific set of files or repository revision, and are mostly for human consumption (e.g. HEAD, trunk, ECLIPSE_35).
120 </p>
121 <p>This doesn't work in a DVCS. Because there is no central repository, there is no central repository version number (either for the repository as a whole, or for individual files).</p>
122 <p>Instead, a DVCS operates at the level of a
123 <i>changeset</i>. Logically, a repository is made up from an initial (empty) state, followed by many changesets. (A changeset is merely a change to a set of files; if you think 'patch' from CVS or SVN, then you're not far off.)
124 </p>
125 <p>Identifying a changeset is much harder. We can't use a (global) revision number, because that concept isn't used. Instead, a changeset is represented as a hash of its contents. For example, given the changeset:</p>
126 <pre>--- a/README.txt
127 +++ b/README.txt
128 @@ -1 +1 @@
129 -SVN is great
130 +Git is great
131 </pre>
132 <p>then we can create a 'hash' using (for example) <code>md5</code>, to generate the string <code>0878a8189e6a3ae1ded86d9e9c7cbe3f</code>. When referring to our change with others, we can use this hash to identify the change in question.</p>
133 <dl>
134 <dd>&#9758;
135 <b>Changesets are identified by a hash of their contents</b>
136 </dd>
137 </dl>
138 <p>Clearly, though, this doesn't work on its own. What happens if we do the same change later on? It would have the same change, and so we don't want the same hash value.</p>
139 <p>What happens is that a changeset contains two things; the change itself, and a back-pointer to the previous changeset. In other words, we end up with something like:</p>
140 <pre>previous: 48b2179994d494485b79504e8b5a6b23ce24a026
141 --- a/README.txt
142 +++ b/README.txt
143 @@ -1 +1 @@
144 -SVN is great
145 +Git is great
146 </pre>
147 <dl>
148 <dd>&#9758;
149 <b>Changesets (recursively) contain pointers to the previous changeset</b>
150 </dd>
151 </dl>
152 <p>Now, if we were to have the same change again, the
153 <i>previous</i> value would be different, so we'd get a different hash value. We could set up an argument:
154 </p>
155 <pre>previous: 48b2179994d494485b79504e8b5a6b23ce24a026
156 --- a/README.txt
157 +++ b/README.txt
158 @@ -1 +1 @@
159 -SVN is great
160 +Git is great
161 </pre>
162 <pre>previous: 8cafc7ecd01d86977d2af254fc400cee
163 --- a/README.txt
164 +++ b/README.txt
165 @@ -1 +1 @@
166 -Git is great
167 +SVN is great
168 </pre>
169 <pre>previous: cba3ef5b2d1101c2ac44846dc4cdc6f4
170 --- a/README.txt
171 +++ b/README.txt
172 @@ -1 +1 @@
173 -Git is great
174 +SVN is great
175 </pre>
176 <p>Each time, the value of the changeset includes a pointer to what comes before, so the hash is continually changing.</p>
178 <b>''Note</b>&lt;nowiki&gt;: Rather than using &lt;/nowiki&gt;<code>md5</code>, as shown here, most DVCS (including Git) use an <code>sha1</code> hash instead. Also, the exact way that the prior elements in the tree are stored, and their relationships, isn't accurately portrayed above; however, it gives the idea of how they are organised sufficiently well.''
179 </p>
180 <dl>
181 <dd>&#9758;
182 <b>Git changesets are identified by an SHA-1 hash</b>
183 </dd>
184 </dl>
185 <h2 id="Changesets_and_branches">Changesets and branches</h2>
186 <p>Given that a changeset is a long value like <code>48b2179994d494485b79504e8b5a6b23ce24a026</code>, it can be unfriendly to use. Fortunately, there are a couple of ways around this. Git, like other DVCSs, allow you to use an abbreviated form of the changeset, providing that it's unique in the repository. For small repositories, this means that you can refer to changesets by really short values, like <code>48b21</code> or even <code>48</code>. Conventionally, developers often use 6 digits of the hash – but large projects (like the Linux kernel) tend to have to use slightly larger references in order to have uniqueness.</p>
187 <dl>
188 <dd>&#9758;
189 <b>Git hashes can be shortened to any unique prefix</b>
190 </dd>
191 </dl>
192 <p>The current version of your repository is simply a pointer to the end of the tree. For this reason, it's often referred to as a
193 <i>tip</i>, but <code>HEAD</code> is used the symbolic identifier for what the current repository is pointing to. Similarly, any branch can be referred to by its changeset id, which includes that and all prior changes. The default branch is usually called
194 <i>master</i>.
195 </p>
196 <dl>
197 <dd>&#9758;
198 <b>The default 'trunk' is called 'master' in Git</b>
199 </dd>
200 <dd>&#9758;
201 <b>The tip of the current branch is referred to as 'HEAD</b>'
202 </dd>
203 </dl>
204 <p>As a direct corollary from this, creating branches in a DVCS is fast. All that happens is the repository on disk is updated to point to a different element in the (already physically present) tree, and you're good to go. Furthermore, it's trivial to ping-pong between different branches on the same repository, which can contain different states and evolve independently.</p>
205 <dl>
206 <dd>&#9758;
207 <b>Creating, and switching between, branches is fast</b>
208 </dd>
209 </dl>
210 <p>Because branching is so fast, branches get used for things where a user of a CVCS wouldn't normally use branching. For example, each bug in Bugzilla could have a new branch associated with it; if a couple of independent features are being worked on concurrently, they'd get their own branch; if you needed to drop back to do maintenance work on an ECLIPSE_35 branch, then you'd switch to a branch for that as well. Branches get created at least as frequently as
211 <a href="http://www.peterfriese.de/using-cvs-change-sets/" target="egit_external">changesets</a> might in CVS, if not more so.
212 </p>
213 <dl>
214 <dd>&#9758;
215 <b>Create a new branch for each Bugzilla or feature item that you work on</b>
216 </dd>
217 <dd>&#9758;
218 <b>Think of branches as throwaway changesets</b>
219 </dd>
220 </dl>
221 <h2 id="Merging">Merging</h2>
222 <p>With great power comes great flexibility, but ultimately, you want to get your changes into some kind of merged stream (like HEAD). One of the fears of unconstrained branching is that of unconstrained merge pains later on. SVN makes this slightly less difficult than CVS, but unless you merge to HEAD frequently, you can easily get lost – particularly when refactorings start happening.</p>
223 <dl>
224 <dd>&#9758;
225 <b>It's painful to merge in a CVCS; therefore branches tend not to happen</b>
226 </dd>
227 </dl>
228 <p>Fortunately, DVCSs are all about merging. Given that each node in the changeset tree contains a pointer to its previous node (and transitively, to the beginning of time), it's much more powerful than the standard flat CVCS diff. In other words, not only do you know what changes need to be made, but also
229 <i>what point in history they need to be made</i>. So, if you have a changeset which renames a file, and then merge in a changeset which points to the file as it was before it was renamed, then a CVCS will just fall over; but a DVCS will be able to apply the change
230 <i>before</i> the rename occurred, and then play forward the changes.
231 </p>
232 <p>Merges are just the weaving together of two (or more) local branches into one. The
233 <a href="http://www.kernel.org/pub//software/scm/git-core/docs/git-merge.html" target="egit_external">git merge</a> documentation has some graphical examples of this; but basically, it's just like any other merge you've seen. However, unlike CVCS, you don't have to specify anything about where you're merging from and to; the trees automatically know what their split point was in the past, and can work it out from there.
234 </p>
235 <dl>
236 <dd>&#9758;
237 <b>Merging is a DVCS like Git is trivial</b>
238 </dd>
239 </dl>
240 <h2 id="Pulling_and_pushing">Pulling and pushing</h2>
241 <p>So far, we've not talked much about the distributed nature of DVCS. Implicitly, though, the changes and ideas above are all to support distribution.</p>
242 <p>Given that a DVCS tree is merely a pointer to a branch (which transitively contains a long list of previous branches), and that each one of these nodes is identified by its hash, then you and I can share the same revision identifiers for common parts of our tree. There are three cases to consider for comparing our two trees:</p>
243 <ul>
244 <li>Your tip is an ancestor of my tip</li>
245 <li>My tip is an ancestor of your tip</li>
246 <li>Neither of our tips are direct ancestors; however, we both share a common ancestor</li>
247 </ul>
248 <p>The first two cases are trivial; if we synchronise trees, they just become a fast-forward merge. In fact, if that occurs, chances are you won't know who is ahead of the other; it will just happen.</p>
249 <p>The last case is only slightly more tricky; a common ancestor must be found; say, <code>746d6c</code>. Then I send changes between my tip and <code>746d6c</code>, and you send changes between your tip and <code>746d6c</code>. That way, we both end up with the same contents on our repositories.</p>
250 <p>Changes flow between repositories by
251 <i>push</i> and
252 <i>pull</i> operations. In essence, it doesn't matter whether I push my changes to you, or you pull my changes from me; the net result is the same. However, in the case of Eclipse.org infrastructure, it's likely that a central Git repository will only be writable by Eclipse committers. Thus, if I contribute a fix, I can ask a committer to pull the fix from my repository, and then they (after reviewing, and optionally rebasing) can push the fix to the Eclipse.org repository.
253 </p>
254 <p>The best part of a DVCS is that it takes care of all the paperwork for you. You don't need to use SVN-like <code>314:321</code> tags to remind you where you branched from; you don't even have to worry if you haven't updated recently. It all just works.</p>
255 <dl>
256 <dd>&#9758;
257 <b>Pulling and pushing in a DVCS like Git is trivial</b>
258 </dd>
259 </dl>
260 <h2 id="Cloning_and_remotes">Cloning and remotes</h2>
261 <p>Where you can push (or pull) to is configured on a per (local) repository basis. Typically, if you clone an existing project, then a
262 <i>remote name</i> called
263 <i>origin</i> is automatically set up for you. For example, if you wanted to get hold of
264 <a href="http://git.eclipse.org/cgit.cgi/babel/org.eclipse.babel.server.git/" target="egit_external"> org.eclipse.babel.server.git</a>, then you could do:
265 </p>
266 <pre>git clone git://git.eclipse.org/gitroot/babel/org.eclipse.babel.server.git
267 </pre>
268 <p>We can then keep up-to-date with what's happening on the remote server by executing a pull from the remote:</p>
269 <pre>git pull origin
270 </pre>
271 <p>...but we're not limited to one repository. Let's say we wanted to create a separate copy on
272 <a href="http://www.github.com" target="egit_external">GitHub</a> for easy forking; we can do that by adding another remote Git URL and then pushing to that:
273 </p>
274 <pre>git remote add github http://github.com/alblue/babel.git
275 git push github
276 </pre>
277 <p>We can now use <code>git push</code> and <code>git pull</code> to move items between the two git repositories. By default, they both refer to the special-named
278 <i>origin</i>, but you can specify whatever remote to talk to on the command line.
279 </p>
280 <dl>
281 <dd>&#9758;
282 <b>Origin is the name of the default remote, but you can have many remotes per repository.</b>
283 </dd>
284 </dl>
285 <h2 id="Initialising.2C_committing_and_branching">Initialising, committing and branching</h2>
286 <p>To create a new Git repository, the <code>git init</code> command is used. This creates an empty repository in the current directory. They can, but often don't, end with <code>.git</code> – typically it's only repositories pushed to remote servers that use the <code>.git</code> extension. As noted above, a Git repository should ideally only hold one or a few highly related/coupled projects.</p>
287 <dl>
288 <dd>&#9758;
289 <b>'git init' creates a fresh repository in the current directory</b>
290 </dd>
291 </dl>
292 <p>Git allows you to commit files, much like any other VCS. Each commit may be a single file, or many files; and a message goes along with it. Unlike other VCS, Git has a separate concept of an
293 <i>index</i>, which is a set of files that would be committed. You can think of it as an active changeset; as you're working on multiple files, you only want some changes to be committed as a unit. These files get <code>git add</code>ed to the index first, then <code>git commit</code>ted subsequently. (If you don't like this behaviour, there's a <code>git commit -a</code> option, which performs like CVS or SVN would.)
294 </p>
295 <dl>
296 <dd>&#9758;
297 <b>'git add' is used to add files and track changes to files</b>
298 </dd>
299 <dd>&#9758;
300 <b>'git commit' is used to commit tracked files</b>
301 </dd>
302 </dl>
303 <p>To create branches, you can use <code>git branch</code> (which creates, but does not switch to, the new branch) and <code>git checkout</code> (which switches to the new branch). A shorthand for new branches is <code>git checkout -b</code>, which creates-and-switches to a branch. At any point, <code>git branch</code> shows you a list of branches and marks the current one with a * next to the name.</p>
304 <dl>
305 <dd>&#9758;
306 <b>'git branch' is used to create and list branches</b>
307 </dd>
308 <dd>&#9758;
309 <b>'git checkout' is used to switch branches</b>
310 </dd>
311 <dd>&#9758;
312 <b>'git checkout -b' is used to create and then switch branches</b>
313 </dd>
314 </dl>
315 <h2 id="Worked_example">Worked example</h2>
316 <p>Here's a transcript of working with setting up an initial repository, then copying data to and from a 'remote' repository, albeit in a different directory on the same system. The instructions are for a Unix-like environment (e.g. Cygwin on Windows).</p>
317 <pre>$ mkdir /tmp/example
318 $ cd /tmp/example
319 $ git init
320 Initialized empty Git repository in /tmp/example/.git/
321 $ echo "Hello, world" &amp;gt; README.txt
322 $ git commit # Won't commit files by default
323 # On branch master
325 # Initial commit
327 # Untracked files:
328 # (use "git add &amp;lt;file&amp;gt;..." to include in what will be committed)
330 # README.txt
331 nothing added to commit but untracked files present (use "git add" to track)
332 $ git add README.txt # Similar to Team -&amp;gt; Add to Version Control
333 $ # git commit # Would prompt for message
334 $ git commit -m "Added README.txt"
335 [master (root-commit) 0dd1f35] Added README.txt
336 1 files changed, 1 insertions(+), 0 deletions(-)
337 create mode 100644 README.txt
338 $ echo "Hello, solar system" &amp;gt; README.txt
339 $ git commit
340 # On branch master
341 # Changed but not updated:
342 # (use "git add &amp;lt;file&amp;gt;..." to update what will be committed)
343 # (use "git checkout -- &amp;lt;file&amp;gt;..." to discard changes in working directory)
345 # modified: README.txt
347 no changes added to commit (use "git add" and/or "git commit -a")
348 $ git commit -a -m "Updated README.txt"
349 [master 9b1939a] Updated README.txt
350 1 files changed, 1 insertions(+), 1 deletions(-)
351 $ git log --graph --oneline # Shows graph nodes (not much here) and change info
352 * 9b1939a Updated README.txt
353 * 0dd1f35 Added README.txt
354 $ git checkout -b french 0dd1f35 # create and switch to a new branch 'french'
355 Switched to a new branch 'french'
356 $ cat README.txt
357 Hello, world
358 $ echo "Bonjour, tout le monde" &amp;gt; README.txt
359 $ git add README.txt # or commit -a
360 $ git commit -m "Ajouté README.txt"
361 [french 66a644c] Ajouté README.txt
362 1 files changed, 1 insertions(+), 1 deletions(-)
363 $ git log --graph --oneline
364 * 66a644c Ajouté README.txt
365 * 0dd1f35 Added README.txt
366 $ git checkout -b web 0dd1f35 # Create and checkout a branch 'web' from initial commit
367 $ echo '&amp;lt;a href="http://git.eclipse.org"&amp;gt;git.eclipse.org&amp;lt;/a&amp;gt;' &amp;gt; index.html
368 $ git add index.html
369 $ git commit -m "Added homepage"
370 [web d47e30c] Added homepage
371 1 files changed, 1 insertions(+), 0 deletions(-)
372 create mode 100644 index.html
373 $ git checkout master
374 $ git branch # See what branches we've got
375 french
376 * master
378 $ git merge web # pull 'web' into current branch 'master'
379 Merge made by recursive.
380 index.html | 1 +
381 1 files changed, 1 insertions(+), 0 deletions(-)
382 create mode 100644 index.html
383 $ git checkout french # Switch to 'french' branch
384 Switched to branch 'french'
385 $ git merge web # And merge in the same
386 Merge made by recursive.
387 index.html | 1 +
388 1 files changed, 1 insertions(+), 0 deletions(-)
389 create mode 100644 index.html
390 $ git log --graph --oneline
391 * e974231 Merge branch 'web' into french
393 | * d47e30c Added homepage
394 * | 66a644c Ajouté README.txt
396 * 0dd1f35 Added README.txt
397 $ git checkout master
398 $ git log --graph --oneline
399 * e3de4de Merge branch 'web'
401 | * d47e30c Added homepage
402 * | 9b1939a Updated README.txt
404 * 0dd1f35 Added README.txt
405 $ (mkdir /tmp/other;cd /tmp/other;git init) # Could do this in other process
406 Initialized empty Git repository in /tmp/other/.git/
407 $ git remote add other /tmp/other # could be a URL over http/git
408 $ git push other master # push branch 'master' to remote repository 'other'
409 Counting objects: 11, done.
410 Delta compression using up to 2 threads.
411 Compressing objects: 100% (7/7), done.
412 Writing objects: 100% (11/11), 981 bytes, done.
413 Total 11 (delta 1), reused 0 (delta 0)
414 Unpacking objects: 100% (11/11), done.
415 To /tmp/other
416 * [new branch] master -&amp;gt; master
417 $ git push --all other # Push all branches to 'other'
418 Counting objects: 8, done.
419 Delta compression using up to 2 threads.
420 Compressing objects: 100% (3/3), done.
421 Writing objects: 100% (5/5), 567 bytes, done.
422 Total 5 (delta 0), reused 0 (delta 0)
423 Unpacking objects: 100% (5/5), done.
424 To /tmp/other
425 * [new branch] french -&amp;gt; french
426 * [new branch] web -&amp;gt; web
427 $ cd /tmp/other # Switch to 'other' repository
428 $ ls # Nothing to be seen, but it's there
429 $ git branch
430 french
431 * master
433 $ git checkout web # Get the contents of the 'web' branch in other
434 $ ls
435 README.txt index.html
436 $ echo '&amp;lt;h1&amp;gt;Git rocks!&amp;lt;/h1&amp;gt;' &amp;gt;&amp;gt; index.html
437 $ git commit -a -m "Added Git Rocks!"
438 [web 510621a] Added Git Rocks
439 1 files changed, 1 insertions(+), 0 deletions(-)
440 $ cd /tmp/example # Back to first repo
441 $ git pull other web # Pull changes from 'other' repo 'web' branch
442 remote: Counting objects: 5, done.
443 remote: Compressing objects: 100% (3/3), done.
444 remote: Total 3 (delta 0), reused 0 (delta 0)
445 Unpacking objects: 100% (3/3), done.
446 From /tmp/other
447 * branch web -&amp;gt; FETCH_HEAD
448 Merge made by recursive.
449 index.html | 1 +
450 1 files changed, 1 insertions(+), 0 deletions(-)
451 $ git log --graph --oneline
452 * 146932f Merge branch 'web' of /tmp/other
454 | * 510621a Added Git Rocks
455 * | e3de4de Merge branch 'web'
456 |\ \
457 | |/
458 | * d47e30c Added homepage
459 * | 9b1939a Updated README.txt
461 * 0dd1f35 Added README.txt
462 </pre>
463 <h2 id="Rebasing_and_fast-forwarding">Rebasing and fast-forwarding</h2>
464 <p>Often, you'll work on a branch for a while and then want to commit it to the repository. You can do this at any point, but it's considered good practice to
465 <i>rebase</i> your local branch before doing so. For example, you can end up with multiple branches in the log (with <code>git log --graph --oneline</code>):
466 </p>
467 <pre>* f0fde4e Merge change I11dc6200
469 | * 86dfb92 Mark the next version as 0.6
470 * | 0c8c04d Merge change I908e4c77
471 |\ \
472 | |/
473 |/|
474 | * 843dc8f Add support for logAllRefUpdates configuration parameter
475 * | 74ba6fc Remove TODO file and move to bugzilla
476 * | ba7c6e8 Fix SUBMITTING_PATCHES to follow the Eclipse IP process
477 * | c5e8589 Fix tabs-to-spaces in SUBMITTING_PATCHES
478 * | 677ca7b Update SUBMITTING_PATCHES to point to Contributor Guide
479 * | 8847865 Document protected members of RevObjectList
480 * | a0a0ce8 Make it possible to clear a PlotCommitList
481 * | 4a3870f Include description for missing bundle prereqs
483 * 144b16d Cleanup MANIFEST.MF in JGit
484 </pre>
485 <p>What happened here was that two branches split off from change <code>144b16d</code>, ultimately driving another branch at <code>74ba6fc</code> and a few merges (at <code>0c8c04d</code> and <code>f0fde4e</code>). (You can see a similar effect in
486 <a href="http://code.google.com/p/wave-protocol/source/list" target="egit_external">Google Code's Hg view of Wave Protocol</a>.) Ultimately, whilst the DVCS can handle these long-running branches and subsequent merges, humans tend to prefer to see fewer branches in the final repository.
487 </p>
488 <p>A
489 <i>fast-forward</i> merge (in Git terms) is one which doesn't need any kind of merge operation. This usually happens when you are moving from an older branch to a newer branch on the same timeline; such as when updating to a newer version from a remote repository. These are essentially just moving the HEAD pointer further down the branch.
490 </p>
491 <p>A
492 <i>rebase</i> is uprooting the branch from the original commit, and re-writing history as if it had been done from the current point in time. For example, in the above Git trace, <code>1441b16d</code> to <code>843dc8f</code> to <code>0c8c0fd</code> was only one commit off the main tree. Had the change been rebased off of <code>74ba6fc</code>, then we would have only seen a single timeline across those commits. It's generally considered good practice to rebase changes prior to pushing to a remote tree to avoid these kind of fan-outs, but it's not necessary to do so. Furthermore, the rebase operation changes the <code>sha1</code> hashes of your tree, which can affect those who have forked your repository. Best practice is to frequently rebase your changes in your own local repository, but once they've been made public (by pushing to a shared repository) to avoid rebasing further.
493 </p>
494 <dl>
495 <dd>&#9758;
496 <b>Rebasing replants your tree; but do it on local branches only</b>
497 </dd>
498 </dl><hr/>
499 <table class="navigation" style="width: 100%;" border="0" summary="navigation">
500 <tr>
501 <td style="width: 20%" align="left"></td>
502 <td style="width: 60%" align="center"></td>
503 <td style="width: 20%" align="right">
504 <a href="Updating-This-Document.html" title="Updating This Document">
505 <img alt="Next" border="0" src="../../images/next.gif"/>
506 </a>
507 </td>
508 </tr>
509 <tr>
510 <td style="width: 20%" align="left" valign="top"></td>
511 <td style="width: 60%" align="center"></td>
512 <td style="width: 20%" align="right" valign="top">Updating This Document</td>
513 </tr>
514 </table>
515 </body>
516 </html>