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<div class="figure" style="width:200px"> 45 <img src="{@docRoot}images/submit-patches-0.png" alt="basic workflow diagram" height="153px" /> 46 <p class="img-caption"> 47 <strong>Figure 1.</strong> Basic Android workflow 48 </p> 49</div> 50 51<p>The basic pattern of interacting with the repositories is as follows:</p> 52<ol> 53<li> 54<p>Use <code>repo start</code> to start a new topic branch.</p> 55</li> 56<li> 57<p>Edit the files.</p> 58</li> 59<li> 60<p>Use <code>git add</code> to stage changes.</p> 61</li> 62<li> 63<p>Use <code>git commit</code> to commit changes.</p> 64</li> 65<li> 66<p>Use <code>repo upload</code> to upload changes to the review server.</p> 67</li> 68</ol> 69<h2 id="task-reference">Task reference</h2> 70<p>The task list below shows a summary of how to do common Repo and Git tasks. 71For information about using repo to download source, see <a href="{@docRoot}source/downloading.html">Downloading the Source</a> and 72<a href="{@docRoot}source/using-repo.html">Using Repo</a>.</p> 73<h2 id="synchronizing-your-client">Synchronizing your client</h2> 74<p>To synchronize the files for all available projects: </p> 75<pre><code>$ repo sync 76</code></pre> 77<p>To synchronize the files for selected projects:</p> 78<pre><code>$ repo sync PROJECT0 PROJECT1 PROJECT2 ... 79</code></pre> 80<h2 id="creating-topic-branches">Creating topic branches</h2> 81<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> 82<p>To start a topic branch using Repo, navigate into the project to be modified and issue: </p> 83<pre><code>$ repo start BRANCH_NAME . 84</code></pre> 85<p>Please note, the period represents the project in the current working directory. To verify your new branch was created:</p> 86<pre><code>$ repo status . 87</code></pre> 88<h2 id="using-topic-branches">Using topic branches</h2> 89<p>To assign the branch to a particular project:</p> 90<pre><code>$ repo start BRANCH_NAME PROJECT_NAME 91</code></pre> 92<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> 93 94<p>To switch to another branch that you have created in your local work environment:</p> 95<pre><code>$ git checkout BRANCH_NAME 96</code></pre> 97<p>To see a list of existing branches:</p> 98<pre><code>$ git branch 99</code></pre> 100<p>or </p> 101<pre><code>$ repo branches 102</code></pre> 103<p>The name of the current branch will be preceded by an asterisk.</p> 104<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> 105<h2 id="staging-files">Staging files</h2> 106<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> 107<p>You can stage your changes by running</p> 108<pre><code>git add 109</code></pre> 110<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> 111<h2 id="viewing-client-status">Viewing client status</h2> 112<p>To list the state of your files:</p> 113<pre><code>$ repo status 114</code></pre> 115<p>To see uncommitted edits:</p> 116<pre><code>$ repo diff 117</code></pre> 118<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> 119<pre><code>$ cd ~/WORKING_DIRECTORY/PROJECT 120$ git diff --cached 121</code></pre> 122<h2 id="committing-changes">Committing changes</h2> 123<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> 124<pre><code>git commit 125</code></pre> 126<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> 127<h2 id="uploading-changes-to-gerrit">Uploading changes to Gerrit</h2> 128<p>Before uploading, update to the latest revisions:</p> 129<pre><code>repo sync 130</code></pre> 131<p>Next run</p> 132<pre><code>repo upload 133</code></pre> 134<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> 135<h2 id="recovering-sync-conflicts">Recovering sync conflicts</h2> 136<p>If a <code>repo sync</code> shows sync conflicts:</p> 137<ul> 138<li>View the files that are unmerged (status code = U).</li> 139<li>Edit the conflict regions as necessary.</li> 140<li> 141<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> 142<pre><code>$ git add . 143$ git commit 144$ git rebase --continue 145</code></pre> 146</li> 147<li> 148<p>When the rebase is complete start the entire sync again:</p> 149<pre><code>$ repo sync PROJECT0 PROJECT1 ... PROJECTN 150</code></pre> 151</li> 152</ul> 153<h2 id="cleaning-up-your-client-files">Cleaning up your client files</h2> 154<p>To update your local working directory after changes are merged in Gerrit:</p> 155<pre><code>$ repo sync 156</code></pre> 157<p>To safely remove stale topic branches: </p> 158<pre><code>$ repo prune 159</code></pre> 160<h2 id="deleting-a-client">Deleting a client</h2> 161<p>Because all state information is stored in your client, you only need to delete the directory from your filesystem:</p> 162<pre><code>$ rm -rf WORKING_DIRECTORY 163</code></pre> 164<p>Deleting a client will <em>permanently delete</em> any changes you have not yet uploaded for review.</p> 165<h2 id="git-and-repo-cheatsheet">Git and Repo cheatsheet</h2> 166<img src="{@docRoot}images/git-repo-1.png" alt="list of basic git and repo commands" id="figure2" /> 167<p class="img-caption"> 168 <strong>Figure 2.</strong> Basic git and repo commands 169</p> 170