1page.title=Developing
2@jd:body
3
4<!--
5    Copyright 2013 The Android Open Source Project
6
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10
11        http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18-->
19<div id="qv-wrapper">
20  <div id="qv">
21    <h2>In this document</h2>
22    <ol id="auto-toc">
23    </ol>
24  </div>
25</div>
26
27<p>To work with the Android code, you will need to use both Git and Repo.  In most situations, you can use Git instead of Repo, or mix Repo and Git commands to form complex commands. Using Repo for basic across-network operations will make your work much simpler, however.</p>
28<p><strong>Git</strong> is an open-source version-control system designed to handle very large projects that are distributed over multiple repositories. In the context of Android, we use Git for local operations such as local branching, commits, diffs, and edits.  One of the challenges in setting up the Android project was figuring out how to best support the outside community--from the hobbiest community to large OEMs building mass-market consumer devices. We wanted components to be replaceable, and we wanted interesting components to be able to grow a life of their own outside of Android. We first chose a distributed revision control system, then further narrowed it down to Git.</p>
29<p><strong>Repo</strong> is a repository management tool that we built on top of Git. Repo
30unifies the many Git repositories when necessary, does the uploads to our
31<a href="https://android-review.googlesource.com/">revision control system</a>, and
32automates parts of the Android development workflow. Repo is not meant to
33replace Git, only to make it easier to work with Git in the context of
34Android. The repo command is an executable Python script that you can put
35anywhere in your path. In working with the Android source files, you will
36use Repo for across-network operations. For example, with a single Repo
37command you can download files from multiple repositories into your local
38working directory.</p>
39<p><strong>Gerrit</strong> is a web-based code review system for projects that use git. Gerrit encourages more centralized use of Git by allowing all authorized users to submit changes, which are automatically merged if they pass code review. In addition, Gerrit makes reviewing easier by displaying changes side by side in-browser and enabling inline comments. </p>
40<h2 id="basic-workflow">Basic Workflow</h2>
41<div style="float:right">
42  <img src="{@docRoot}images/submit-patches-0.png" alt="basic workflow diagram">
43</div>
44
45<p>The basic pattern of interacting with the repositories is as follows:</p>
46<ol>
47<li>
48<p>Use <code>repo start</code> to start a new topic branch.</p>
49</li>
50<li>
51<p>Edit the files.</p>
52</li>
53<li>
54<p>Use <code>git add</code> to stage changes.</p>
55</li>
56<li>
57<p>Use <code>git commit</code> to commit changes.</p>
58</li>
59<li>
60<p>Use <code>repo upload</code> to upload changes to the review server.</p>
61</li>
62</ol>
63<h2 id="task-reference">Task reference</h2>
64<p>The task list below shows a summary of how to do common Repo and Git tasks.
65For information about using repo to download source, see <a href="{@docRoot}source/downloading.html">Downloading the Source</a> and
66<a href="{@docRoot}source/using-repo.html">Using Repo</a>.</p>
67<h2 id="synchronizing-your-client">Synchronizing your client</h2>
68<p>To synchronize the files for all available projects: </p>
69<pre><code>$ repo sync
70</code></pre>
71<p>To synchronize the files for selected projects:</p>
72<pre><code>$ repo sync PROJECT0 PROJECT1 PROJECT2 ...
73</code></pre>
74<h2 id="creating-topic-branches">Creating topic branches</h2>
75<p>Start a topic branch in your local work environment whenever you begin a change, for example when you begin work on a bug or new feature. A topic branch is not a copy of the original files; it is a pointer to a particular commit. This makes creating local branches and switching among them a light-weight operation. By using branches, you can isolate one aspect of your work from the others. For an interesting article about using topic branches, see <a href="http://www.kernel.org/pub/software/scm/git/docs/howto/separating-topic-branches.txt">Separating topic branches</a>.</p>
76<p>To start a topic branch using Repo, navigate into the project to be modified and issue: </p>
77<pre><code>$ repo start BRANCH_NAME .
78</code></pre>
79<p>Please note, the period represents the project in the current working directory. To verify your new branch was created:</p>
80<pre><code>$ repo status .
81</code></pre>
82<h2 id="using-topic-branches">Using topic branches</h2>
83<p>To assign the branch to a particular project:</p>
84<pre><code>$ repo start BRANCH_NAME PROJECT_NAME
85</code></pre>
86<p>See <a href="https://android.googlesource.com/">android.googlesource.com</a> for a list of all projects. Again, if you've already navigated into a particular project directory, you may simply pass a period to represent the current project.</p>
87
88<p>To switch to another branch that you have created in your local work environment:</p>
89<pre><code>$ git checkout BRANCH_NAME
90</code></pre>
91<p>To see a list of existing branches:</p>
92<pre><code>$ git branch
93</code></pre>
94<p>or </p>
95<pre><code>$ repo branches
96</code></pre>
97<p>The name of the current branch will be preceded by an asterisk.</p>
98<p><em>Note: A bug might be causing <code>repo sync</code> to reset the local topic branch. If <code>git branch</code> shows * (no branch) after you run <code>repo sync</code>, then run <code>git checkout</code> again.</em></p>
99<h2 id="staging-files">Staging files</h2>
100<p>By default, Git notices but does not track the changes you make in a project. In order to tell git to preserve your changes, you must mark them for inclusion in a commit. This is also called "staging". </p>
101<p>You can stage your changes by running</p>
102<pre><code>git add
103</code></pre>
104<p>which accepts as arguments any files or directories within the project directory. Despite the name, <code>git add</code> does not simply add files to the git repository; it can also be used to stage file modifications and deletions.</p>
105<h2 id="viewing-client-status">Viewing client status</h2>
106<p>To list the state of your files:</p>
107<pre><code>$ repo status
108</code></pre>
109<p>To see uncommitted edits:</p>
110<pre><code>$ repo diff
111</code></pre>
112<p>The <code>repo diff</code> command shows every local edit that you have made that would <em>not</em> go into the commit, if you were to commit right now. To see every edit that would go into the commit if you were to commit right now, you need a Git command, <code>git diff</code>. Before running it, be sure you are in the project directory:</p>
113<pre><code>$ cd ~/WORKING_DIRECTORY/PROJECT  
114$ git diff --cached
115</code></pre>
116<h2 id="committing-changes">Committing changes</h2>
117<p>A commit is the basic unit of revision control in git, consisting of a snapshot of directory structure and file contents for the entire project. Creating a commit in git is as simple as typing</p>
118<pre><code>git commit
119</code></pre>
120<p>You will be prompted for a commit message in your favorite editor; please provide a helpful message for any changes you submit to the AOSP. If you do not add a log message, the commit will be aborted. </p>
121<h2 id="uploading-changes-to-gerrit">Uploading changes to Gerrit</h2>
122<p>Before uploading, update to the latest revisions:</p>
123<pre><code>repo sync
124</code></pre>
125<p>Next run</p>
126<pre><code>repo upload
127</code></pre>
128<p>This will list the changes you have committed and prompt you to select which branches to upload to the review server. If there is only one branch, you will see a simple <code>y/n</code> prompt.</p>
129<h2 id="recovering-sync-conflicts">Recovering sync conflicts</h2>
130<p>If a <code>repo sync</code> shows sync conflicts:</p>
131<ul>
132<li>View the files that are unmerged (status code = U).</li>
133<li>Edit the conflict regions as necessary.</li>
134<li>
135<p>Change into the relevant project directory, run <code>git add</code> and <code>git commit</code> for the files in question, and then "rebase" the changes. For example:</p>
136<pre><code>$ git add .
137$ git commit 
138$ git rebase --continue
139</code></pre>
140</li>
141<li>
142<p>When the rebase is complete start the entire sync again:</p>
143<pre><code>$ repo sync PROJECT0 PROJECT1 ... PROJECTN
144</code></pre>
145</li>
146</ul>
147<h2 id="cleaning-up-your-client-files">Cleaning up your client files</h2>
148<p>To update your local working directory after changes are merged in Gerrit:</p>
149<pre><code>$ repo sync
150</code></pre>
151<p>To safely remove stale topic branches: </p>
152<pre><code>$ repo prune
153</code></pre>
154<h2 id="deleting-a-client">Deleting a client</h2>
155<p>Because all state information is stored in your client, you only need to delete the directory from your filesystem:</p>
156<pre><code>$ rm -rf WORKING_DIRECTORY
157</code></pre>
158<p>Deleting a client will <em>permanently delete</em> any changes you have not yet uploaded for review.</p>
159<h2 id="git-and-repo-cheatsheet">Git and Repo cheatsheet</h2>
160<p><img src="{@docRoot}images/git-repo-1.png" alt="list of basic git and repo commands"></p>
161