[Learn Android Studio 汉化教程]第七章 : Git 介绍 (上)

Introducing Git

The Git version control system (VCS) is fast becoming the de facto standard, not only in Android application development, but for software programming in general. Unlike earlier version control systems that require the use of a central server, Git is distributed, which means that each copy of the repository contains the entire history of the project, and no contributor is privileged. Git was developed by Linus Torvalds of Linux fame in order to manage the development of the Linux operating system. Like the open source movement itself, Git is systemically nonhierarchical and encourages collaboration.
While Git offers a wealth of features from the command line, this chapter focuses primarily on using Git from within Android Studio. The IntelliJ platform underpinning Android Studio has offered outstanding support for several VCS systems over the years, including Git.
The consistency with the different supported systems is presented in a way that makes it easy for both newcomers and professionals to be proficient. However, it is important to understand the differences between using Git from within Android Studio and using Git
from the command line. This chapter explains everything you need to get started with Git in great detail. You’ll reuse the Reminders app that you began in earlier chapters to learn the fundamentals of committing, branching, pushing, and fetching, among other important commands. You’ll work with both local and remote Git repositories and see how to use Git and Android Studio in a collaborative environment.
Open the HelloWorld project you created in Chapter 1. If you skipped that chapter, create a new project from scratch named HelloWorld. Use all of the default settings as you progress through the wizard. You will use this project briefly to understand the basics of Git setup.

【翻译】Git介绍

Git版本控制系统(VCS)快速成为Android应用程序开发以及常规的软件编程领域内的事实标准。有别于需要中心服务器支持的早期版本控制系统,Git是分步式的,这意味着每一个版本库副本都记录了项目的整个历史,并且项目贡献者不享有特权。因Linux成名的Linux之父林纳斯.托瓦兹,为了方便管理Linux操作系统的开发工作而开发了Git。就像开源运动本身,Git鼓励合作,不按等级划分。
尽管Git为命令行窗口提供了多种功能,本章主要集中探讨如何在Android Studio中使用Git。支撑Android Studio的IntelliJ平台多年来为包括Git在内的数种VCS系统提供了出色的支持。
而且不同支撑系统具有的兼容性也使得新手以及行家都能轻而易举地精通它。然而,在Android Studio中使用Git与在命令行窗口中使用Git是不同的,了解这一点非常重要。本章将向你详解在Android Studio上学习git的一切细节。通过在前面几章中学习Reminders应用程序,你学习了committ、branch、push和fetch等重要命令的基础知识,现在请重新启动这个应用程序。接着你需要使用本地及远程版本库,了解如何在协作环境下使用Git和Android Studio。
打开你在第一章中创建的Hello World项目。如果你跳过了那一章,那么新建一个项目,并把它命名为 HelloWorld。在向导界面中进行下一步的时候,一律使用默认设置。通过这个项目,了解在Android Studio上安装Git的基本原理。

Installing Git

Before you can begin using Git, you need to install it. Point your browser to http://git-scm. com/downloads. Click the Download button for your operating system, as shown in
Figure 7-1.

【翻译】安装Git

想要使用Git,先安装它。单击操作系统上的下载按钮,如图7-1所示。
Figure 7-1. Git download page

【翻译】图7-1 Git下载页面





We recommend installing Git in the C:\java\ directory on Windows or in the ~/java directory on Mac or Linux. Wherever you decide to install it, be sure that the entire path is free from spaces. For example, do not install Git in the C:\Program Files directory, because there is a space between Program and Files. Command line oriented tools like
Git can potentially have trouble with directories that have a space in their name. Once your installation is complete, you must be sure that the C:\java\git\bin\ directory is part of your PATH environmental variable. See Chapter 1 for detailed instructions on how to add a path to the PATH environmental variable.
Launch the Git Bash terminal by clicking the Git Bash icon. If you’re running a Mac or Linux, just open a terminal. You need to configure Git with your name and e-mail so that your commits will have an author. From Git Bash, issue the following commands and replace John Doe’s name and e-mail address with your own. Figure 7-2 shows an example.
$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

【翻译】在Windows操作系统中,我们建议你把Git安装在C:\java\directory文件夹中。在Mac或Linux操作系统中,你则需把Git安装在~/java文件夹中。但是无论你在哪里安装Git,确保那儿有足够的存储空间。比如说,不要把Git安装在C:\Program Files上, 因为Program和Files之间有一个空格。像Git这样以命令行为导向的工具,如果安装目录的名字中有空格,它们的文件目录很可能会出故障。一旦安装结束,必须确保 C:\java\git\bin\文件夹是你PATH环境变量的一部分。如需了解PATH PATH环境变量中添加一个路径的详细说明,详见第一章。
点击Git Bash图标,启动Git Bash终端。如果你使用的是Mac或Linux,打开终端即可。把你的名字和邮箱记到Git上,这样你的提交中才能显示相应的发起人。在Git Bash上输入下列命令,把John Doe以及以它命名的邮箱替换成你自己的。请看图7-2 中的示例图。
$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

Figure 7-2. Adding your name and e-mail to Git
【翻译】图7-2 在Git上添加你的名字和邮箱





Return to Android Studio to continue setting up Git integration with Android Studio. Navigate to File ➤ Settings, and then find Git under the Version Control section in the left pane. Click the ellipsis button and navigate to the Git binary you just installed. Click the Test button to ensure that your Git environment is operational. You should see a pop-up indicating that Git executed successfully, as well as the version of Git you installed.
Navigate to VCS ➤ Import into Version Control ➤ Create Git Repository. When the dialog box prompts you to select the directory where the new Git repository will be created, make sure you choose the project root directory HelloWorld. You can optionally click the little Android Studio icon in the directory chooser dialog box. This icon will navigate to the project’s root directory, as illustrated in Figure 7-3. Click the OK button, and your local Git repository will be created.
【翻译】回到Android Studio,继续安装Git。找到File ➤ Settings,然后在左窗格中的版本控制部分下方找到Git。点击ellipsis按钮,找到你刚刚安装的Git二进制文件(Git Binary)。为确保你的Git环境能正常运行,点击Test按钮来进行测试。接着你会看到一个显示Git及你刚安装的那个Git版本成功运行的弹出菜单。
前往VCS ➤ Import into Version Control ➤Create Git Repository。当对话框提示你勾选新建Git版本库所在的文件夹时,确保勾选HelloWorld项目的根目录文件夹。你也可以点击chooser对话框中的little Android Studio图标,这个图标可以链接到HelloWorld项目的根目录文件夹,如图7-3所示。点击确定按钮,你的Git本地版本库就被创建好了。

Figure 7-3. Selecting the directory for your Git repository
【翻译】图7-3 为你的Git版本库选择文件夹



You will notice that most of the file names in your Project tool window have turned brown. This means that these files are recognized by Git locally but are not being tracked by Git and not scheduled to be added. Git manages commits in a two-stage approach (which is different from the approach used by other VCS tools such as Subversion and Perforce).
The staging area is where Git organizes changes prior to a commit. The differences between the changes in progress, the staging area changes, and the committed changes are significant and can overwhelm new users. As a result, Android Studio does not expose these differences. Instead you get one simple changes interface that allows you to manage modified files and commit them with ease.
【翻译】你会注意到项目工具窗口上的大部分文件名都变成了棕色,这就意味着这些文件在本地中被Git识别,但是不被它追踪或添加至计划列表。不同于Subversion和Perforce等其他VCS工具的使用方法,Git分两个阶段完成提交。
暂存区是提交之前Git进行更改的区域。因为项目进行区的更改、暂存区的更改与提交后的更改都是不同的,而且这些不同会让新手们不知所措,所以, Android Studio并不显示这些不同点。反而,你会看到一个相当简洁的更改界面,它能让你管理并轻松提交被修改好的文档。

Ignoring Files

When you create the local repository, Android Studio generates special .gitignore files that prevent certain paths from being tracked. Unless you specify otherwise, Git will continue to track all the files in this directory and its subdirectories. However, .gitignore files can tell Git to ignore certain files or entire directories.
Typically, you will have one .gitingorefile for the root directory, and one .gitignorefile for each project. In HelloWorld, one .gitignoreis located in the root of HelloWorld, and one
.gitignoreis located in the root of the appfolder. Open the .gitignorefile located in the root of HelloWorld and inspect its contents. Figure 7-4 illustrates the generated .gitignorefile in the
project’s root directory. By default, Android Studio sets certain files to be excluded from your Git repository. The list includes files that are either generated by the project build or control settings specific to your local machine. For instance, the /.idea/workspace.xmlfile controls settings
for your local configuration of Android Studio. Though it is possible to track this in Git, it is not necessarily a part of the project you are building and may in fact pose a problem because this file is unique to every workspace e.g. computer. Notice that one of the entries in .gitignoreis
/local.properties. Like workspace.xml, local.properties is unique to every computer.
Pay attention to the /build entry in the list. Gradle, the Android Studio build system covered in depth in Chapter 13, places all of its output here as you compile and run your project.
Because this folder will contain everything from .class files to .dex files to the final installable
Android package, and because its contents are constantly changing, it makes little sense to track it with Git. Find the local.properties file in the Project tool window. You will notice that it’s black, whereas the other files are brown.

【翻译】忽略文件

当你创建本地仓库时,Android Studio会生成一个特殊的、可以预防某些路径被追踪的.gitignore文件夹。除非你专门说明, 否则Git会持续追踪这个文件夹及其子文件夹内所有的文件。不过 .gitignore 文件夹能够命令Git忽略某些文件及文件夹。
一般来讲,根目录文件夹中会有一个.gitignore文件,每个项目中也有一个 .gitignore文件。在HelloWorld中,一个gitignore文件位于HelloWorld根目录中,另一个gitignore文件位于app文件夹中。打开位于 HelloWorld根目录中的.gitignore文件,检查它的内容。图7-4 举例说明在项目根目录中生成的.gitignore文件。
默认情况下, Android Studio会限制某类文件进入Git版本库,这类文件既有创建项目时生成的文件,也有专属于你本地计算机上的控制设定文件。比如说,Android Studio本地配置时需要的/.idea/workspace.xml文件就属于此类。
Android Studio本地配置中。虽然你可以在Git上追踪这个控制设定文件,但它并不一定是你在建项目的一部分。事实上,它还可能造成问题,因为每台电脑的控制设定文件都是不同的。注意.gitignore文件中的一项条目就是/local.properties文件。就像workspace.xml,每台电脑的local.properties文件也都是不同的。
留意列表中的/build条目。在第13章被详细介绍的Android Studio build系统Gradle, 在你编译和运行项目时,会在此处显示它所有的输出结果。
因为这个文件夹会包含.class文件、 .dex 文件以及你最终可安装的安卓文件包上的一切东西,也因为这个文件夹内的内容时刻在改变,所以在Git上追踪它几乎没有意义。找到在项目工具窗口中的local.properties文件,你会注意到它的文件名颜色是黑色的,有别于其他棕色的文件。

Figure 7-4. The root .gitignore file contents
【翻译】图7-4 root .gitignore文件内容



Android Studio uses a color scheme that allows you to easily identify what your version control system will see as you work. As we’ve already stated, brown indicates that a a file is recognized by Git locally but is not being tracked by Git, and is not scheduled to be added. Blue indicates a file that is being tracked by Git and has been changed. Green is used for brand-new files that are being tracked by Git. Black indicates files that either have not been changed or are not being tracked. Android Studio constantly keeps track of files that are added to your project and prompts you as necessary to keep these files in sync with Git.
【翻译】Android Studio的配色方案能让你轻易查清你的VCS追踪过的工作记录。棕色文件名暗示这个文件被当地Git识别但未被追踪,也未被添加到追踪计划中,这我们之前提过。蓝色文件名暗示这个文件正在被Git追踪,而且已被Git更改。绿色文件名则用于正在被Git追踪的新建文件。黑色意味着这个文件要么还没有被更改,要么正未被Git追踪。Android Studio不停地追踪被添加到你项目中的文件,并提示你有必要通过Git来同步这些文件。

Adding Files

Open the Changes view at the bottom of the screen. It includes two sections: Default and Unversioned Files. The Default section, initially empty, represents the active changelist. As you modify and create files, they will fall under this section, because it holds files that are ready to be committed to your VCS. The Unversioned Files section contains everything that is not being tracked by VCS.
Because all of the project files are not yet tracked, they fall under the Unversioned Files section. You will want to add these to your repository. On the left side of the Changes view are two columns of icons. In the right column, click the third icon from the top (a folder icon); see the circled icon in Figure 7-5. This is a toggle that enables you to group files by folder
to better understand their relative location within your project. Right-click the Unversioned Files section header and click Add to VCS from the context menu to add these files to the Git index. Alternatively, you can click and drag the entire section to the bold Default section.

【翻译】添加文件

打开界面底部的更改视图。它包含两个部分:default和unversioned文件。最初为空白文件的default,代表可运行的变更表。当你修改并创建文件时,他们这些文件会被自动归入default部分,因为它保留那些即将被提交到VCS系统中的文件。unversioned文件包含所有不被vcs追踪的文件。
因为所有的项目文件还未被追踪,所以它们会被归入到unversioned文件中。你需要把这些文件添加到你的版本库中。更改视图的左边是两栏图标,在右栏,从顶部(一个文件夹图标)点击第三个图标。在图7-5中查看被圈出的图标。这个图标是一个切换键,它允许你按文件夹来对文件进行分组
以让你更好地了解它们在你项目内的相对位置。右击Unversioned文件的标头,接着在内容菜单键中点击Add to VCS以把这些文件添加到Git索引中。或者,你也可以点击并拖动整个文件到bold default部分。

Figure 7-5. Group files by folders
【翻译】图7-5 按文件夹对文件进行分组



After adding all the files, click the VCS icon with the green arrow pointing upward. This opens the familiar Commit dialog box you began using in Chapter 5. Click Commit to record your changes, and the Default section will eventually empty out. You can also press Ctrl+K | Cmd+K to perform the same action. From this point on, each file you touch while in Android Studio will be tracked under Git.
【翻译】添加完所有文件后, 用向上指的绿色箭头点击VCS图标,这样你就打开了你在第5章中了解使用过的提交对话框。单击Commit以记录你的更改,最终清空默认文件。你也可以按住Ctrl+K | Cmd+K来进行同样的操作。从现在起,你在Android Studio中操作过的每一个文件都可以被Git追踪了。

Cloning the Reference App: Reminders

This section extends the Reminders app that you created in Chapters 5 and 6. We invite you to clone this project using Git in order to follow along, though you will be recreating this project with a new Git repository based forked from the repostory used in Chapters 5 and 6. If you do not have Git installed on your computer, see Chapter 7. Open a Git-bash session in Windows (or a terminal in Mac or Linux) and navigate to C:\androidBook\reference\ (If you do not have a reference directory, create one. On Mac navigate to /your-labs-parent-dir/reference/) and issue the following git command: git clone https://bitbucket.org/csgerber/reminders-git.git RemindersGit. You will use Git features to modify the project as if you were working on a team.
Through the process, you will learn how to fork and clone a project, and set-up and maintain branches as you develop features. Before beginning this exercise, rename the Reminders project you completed in chapter 6 to RemindersChapter6 because you will be recreating this folder shortly. In windows you can right click the folder in Explorer and choose rename. On Linux or Mac run the following command: mv ~/androidBook/Reminders ~/androidBook/ RemindersChapter6.

【翻译】克隆Reference App: Reminders

本节将继续讲解你在第5和第6章中创建的Reminders app.为了紧跟我们的讲解步骤,建议你使用Git来克隆这个项目,尽管你会一直用一个基于fork分支的、在第5和第6章中使用的新建Git版本库来创建这个项目。如果你的电脑上没有安装Git,请看第7章。打开Windows中的Git-bash会话,(在Mac或Linux系统中,打开Terminal)前往C:\androidBook\reference\ 文件夹。(如果你还没有reference文件夹,新建一个。)在Mac系统中,前往/your-labs-parent-dir/reference/)文件夹并输入下列Git命令: git clone RemindersGit.https://bitbucket.org/csgerber ... sGit.如果你是在团队中工作,你可以用Git功能去修改这个下项目。
通过这个步骤, 你不仅能学会如何对一个项目进行分支和克隆,你还可以学习如何在开发功能时设置和保留分支。在开始练习前, 重命名你在第6章完成的Reminders项目为RemindersChapter6,因为你马上就要开始重新创建这个文件夹。在Windows中,你可以右击Explorer中的文件夹并选择重命名。在Linux或Mac上,运行下列命令:mv ~/androidBook/Reminders ~/androidBook/ RemindersChapter6.

Forking and Cloning

Forking a remote repository involves making a clone from one remote account/partition to another remote account/partition on a single web-hosting service. Fork is not a Git
command; it is an operation of a web-hosting service such as Bitbucket or GitHub. As far as we know, the two more popular web-hosting services, Bitbucket and GitHub, do not allow forks between their servers. Forking a project is the process of copying a project from its original remote repository to your own remote Git repository for the sake of changing it or making derivative work.
Historically, forking had a somewhat negative connotation, because it was often the result of different end goals or disagreements among project members. These differences often resulted in alternate versions of seemingly identical software from multiple groups, and no clear official version that the user community could rely on. These days, however, forking is strongly encouraged thanks to Git. Forking is now a natural part of collaboration. Many open source projects use forks as a means of improving the overall source base. Members encourage others to fork and make improvements to the code. These improvements are pulled back into the original project by means of a pull request, or an individual’s personal request to pull a bug fix or feature back into the main line. Because merging and branching are so flexible with Git, you can pull anything into your repository, from a single commit to an entire branch.
This chapter doesn’t cover the entirety of pull requests and open source collaboration but does cover the features that fuel this powerful form of collaboration. Log into your Bitbucket account and find the case studies on Bitbucket. If you do not yet have a Bitbucket account, navigate your browser to bitbucket.org and sign-up. Signing-up takes about 30 seconds.
Once you’ve logged into Bitbucket, you can find the Reminders repository by using the search box in the upper right corner of the Bitbucket web interface. In that search box, type csgerber/reminders. Again, do not confuse this with the finished reminders-git repository which you cloned earlier as a reference. To fork this project, click the Fork button along the left margin as shown in Figure 7-6. When prompted by the subsequent window, accept the defaults and click the Fork repository button as showing in Figure 7-7.

【翻译】分支和克隆

分支一个远程版本库需要把一项代码托管服务中的一个远程账户/分区克隆到另外一个远程账户/分区上。分支不是一项Git命令;
它是Bitbucket或GitHub等提供代码托管服务的平台上的一项操作。据我们所知,Bitbucket和GitHub这两类最受欢迎的代码托管服务平台,不允许用户在它们的服务器之间进行分支。分支一个项目是把远程版本库上的项目复制到你自己的远程版本库上,其目的是为了改变或拷贝这个项目。
以往, 分支会带有一些消极含义,因为它往往是项目成员的不同目标与分歧的后果。这些差异经常在多个群体内产生外表雷同的数个软件版本,而且用户群没有明晰规范的官方版本来参考。然而在现在,受益于Git,我们强烈推荐使用分支功能。分支成为合作项目中不可或缺的部分。许多开源项目都把分支视为提高整个开源基础的一种有效工具。成员们鼓励其他人通过分支来完善代码。通过提交申请(pull request)或个人针对主线的错误修正、功能恢复申请,这些改良请求得以被添加到初始项目中。因为Git中的合并和分支也是非常灵活的,所以你可以把一切东西(从单项提交到整个分支)放入你的版本库。
这一章虽没有覆盖全部的提取请求和开源合作,但包含了启动这一高效合作方式的所有功能。登陆你的Bitbucket账号,找到Bitbucket上的案例研究。如果你还没有Bitbucket账户,打开你的浏览器到bitbucket.org并注册。注册大概会花30秒。
一旦登陆到Bitbucket上, 你可以通过使用Bitbucket网页界面右上角的搜索框去寻找Reminders respository。在那个搜索框中,输入csgerber/reminders.再说一次,不要把它与你之前克隆为参照物的reminders-git版本库混肴。为了分支这个项目,点击左边空白部分的Fork按钮,如图7-6所示。当随后出现的窗口发出提示时,接受默认,点击fork repository按钮,如图 7-7所示。

Figure 7-6. Click Fork in the Reminders repository left margin controls
图7-6 单击左边空白控制器内Reminders respository中的Fork按钮



Figure 7-7. Click the Fork repository button
【翻译】图7-7 单击Fork版本库按钮


Now, we’re going to clone the repository that you just forked. In Git, cloning is the process of copying an entire Git project from another location, usually from a remote to local.
Find your fork of the project and copy the URL. You can do this by typing your-bitbucket- username/reminders in the search box of the Bitbucket web interface. Directly below the search box, along the upper-right of the Bitbucket web interface, you will find the clone box in which there will be a URL that should look something like: git@bitbucket.org:csgerber/ reminders.git or your-bitbucket-username@bitbucket.org/your-" rel="nofollow" target="_blank">https://your-bitbucket-usernam ... cket- username/reminders.git.
If you don’t have an http URL then click the button next to the URL which should be labeled SSH as seen in Figure 7-8. This will expose a dropdown allowing you to select an http URL. Navigate to VCS > Checkout from Version Control > Git. The dialog box shown in Figure 7-9 opens, prompting you for a VCS Repository URL, a Parent Directory, and a Directory Name. The VCS Repository URL is a URL from the clone box earlier, and the combination of Parent Directory and Directory Name is where you want the copy to be placed on your local computer. By default, the name of the project in the Directory Name is lower-case. We recommend you name your projects in upper-case, so please change that according to Figure 7-9.
【翻译】现在,我们要开始克隆你刚刚分支过的储存库了。在Git上,克隆是把整个Git项目从一处复制到另一处的过程,通常是从远程复制到本地。
找到你项目中的分支并复制URL.你也可以通过在Bitbucket网站界面上的搜索框中输入your-bitbucket- username/reminders来完成这一步操作。在搜索框的正下面,沿着Bitbucket网站界面的右上角,找到克隆框,在它里面会有一个URL,它看起来就像: git@bitbucket.org:csgerber/
如果你没有相应网址,点击URL边上的标签名为SSH的按钮,如图 7-8所示。页面就会显示一个允许你选择一个网址的下拉菜单。从VCS > Checkout中找到Version Control > Git,接着图7-9中显示的对话框会打开,提示你VCS版本库网址、一个根目录以及一个目录名。VCS版本库网址是来自之前克隆窗口的一个网址,至于根目录和目录名,它们在你存储复制副本到你本地电脑的目标路径上。默认情况下,目录名中的项目名称是小写字体,我们推荐你使用大写字体来命名你的项目,所以请根据图7-9来调整。

Figure 7-8. The Bitbucket Share URL
【翻译】图7-8 Bitbucket共享网址



Figure 7-9. Cloning the repository with the Git GUI
【翻译】图7-9 使用Git GUI来克隆版本库


Click Clone, and the source code will be copied locally.
【翻译】单击克隆选项,源代码就会被复制到本地

Using the Git Log

The Git log is a powerful feature that gives you the ability to explore the commit history of your project. Open the Changes tool window by clicking its tool button or pressing Alt+9 | Cmd+9 and then select the Log tab to expose the log. Figure 7-10 illustrates the history of the Reminders project through the final commit at the end of Chapter 6. This view shows the timelines associated with the individual branches in the repository.

【翻译】使用Git日志

Git日志拥有强大的功能,它能让你查看你的项目提交历史。在点击工具按钮或按住Alt+9 | Cmd+9ji键,选择日志选项卡以查看过往记录之后,打开更改工具窗。图7-10通过第6章结尾部分的最后提交说明了Reminders项目历史。这个视图显示了与版本库内个人分支有关的时间轴。

Figure 7-10. Exploring the Git log
【翻译】图7-10 查看Git日志



Clicking individual entries in the timeline reveals the files in a changelist to the right; these are the files that were changed as part of the commit. Click the files from any particular commit and press Ctrl+D | Cmd+D (or simply double-click them) to get a visual text diff, which is a side by side comparison highlighting the changes to the files. You can use the toolbar buttons above the changelist to edit the source, open the repository version of a file, or revert selected changes. You can also use the window below the log to see the committing author, date, time, and a hash code ID. These hash codes are unique IDs that can be used to identify individual commits when using some of Git’s more advanced features.
【翻译】单击时间轴中的个人键会显示右边更改列表中的文件,它们曾被更改为提交的一部分。单击任意一个提交中的文件,按住Ctrl+D | Cmd+D (或仅双击它们),你会看到一个可视化文本diff,它是并排显示比较结果,突出显示了文件的更改。你可以使用更改列表上的工具栏按钮去编辑这一资源,打开一个文件的版本库版本或者还原被选中的更改。你也可以使用日志下面的窗口去查看提交发起人、日期、时间和哈希代码标识符。这些哈希代码都是独特的标识符,它们能在你使用Git的一些高级功能时鉴别个人提交。

Branching

Until now, you’ve made all your commits on a single branch called master, which is the default branch name. However, you don’t need to remain on master. Git allows you to create as many branches as you want, and branches can serve several purposes in Git. Here’s a likely scenario. Say you’re working with a team of developers and you’ve each been assigned specific tasks during a development cycle. Some of those tasks are features and some are bug fixes. One logical way to approach this work is for each task to become a branch. The developers all agree that when a task is complete and tested, the developer will merge the task branch into a branch called dev and then delete the task branch. At the end of the development cycle, the dev branch is tested by the QA team, which either rejects the changes and kicks the project back to the development team, or signs-off on the cycle and merges dev into master. This process is called Git Flow, and it is the recommended way to develop software on a team with Git. You can read more about Git Flow here:https://guides.github.com/intr ... .html
Git Flow works great with large teams, but if you’re developing solo or working with only one or two other developers, you may want to agree on a different workflow. Whatever your workflow, the branching functionality in Git is flexible and will allow you to adapt your workflow to Git. In this section, we’ll assume you are working on a team project and have been given the task of adding a feature in the Reminders app which allows users to schedule a Reminders at particular times throughout the day.

【翻译】分支

【翻译】到现在,你已经在默认分支名为master的单个分支进行了所有提交。然而,你并不需要锁定master这一个分支上,Git允许你新建尽可能多的、有多种用途的分支。下面是一个可能出现的场景:比如说你与一组开发人员合作,你们每个人都在这个开发周期内被安排了特别的任务,这些任务中有些是关于功能的,有些是有关漏洞调试的。为了完成这项工作,为每一个任务设一个分支是一个合理的方法。开发人员一致同意当一个开发人员在完成一项任务并接着测试它时,他需要把这个任务分支合并到一个叫dev的分支上,然后删除这个任务分支。在开发末期,质检小组测试dev 分支。他们要么拒绝更改并把这个项目退回原开发团队,要么中止这个周期,把dev 分支合并到主分支中。这个被叫做 Git工作流的方法,经常被推荐给使用Git来开发软件的团队。想了解更多有关Git工作流的信息,点击这里:https://guides.github.com/intr ... .html

在大型团队中,Git工作流有极佳的应用效果。但如果你一个人或仅与一两个人合作开发,你可能需要另外的工作流。无论你的工作流是什么,Git中的分支功能都是灵活的,而且还能把你的工作流改编整合到Git上。在这一节,我们假设你是在团队中开发一个项目,你被分配的任务是在Reminders app中添加“允许用户在一天中的任何时间段编写一个提醒事项”的功能。

Developing on a Branch

Open the Reminders-Git project you cloned earlier by choosing File ➤ Import Project.Right-click the Reminders-Git root folder in the project view and choose Git ➤ Repository ➤ Branches to open the Branches prompt window. This prompt allows you to explore all the available branches. Click New Branch from the prompt. Name your branch
ScheduledReminders, as in Figure 7-11.

【翻译】在分支上开发

【翻译】选中➤Import Project文件,打开你之前克隆的Reminders-Git项目。右击项目视图中的Reminders-Git根文件夹,选择Git ➤ Repository ➤分支打开分支提示窗口。这个提示窗口允许你查看所有能查看的分支点击提示窗口中的New Branch把你的分支命名为ScheduledReminders, 如图7-11所示

Figure 7-11. Creating a new branch with Git
【翻译】图7-11 用Git创建一个新分支


The new branch will be created and checked out for you to work on. Open the Changes view and click the green plus button to create a new changelist. Name it ScheduledReminders, like your new branch, as the next round of changes will introduce the feature which schedules reminders. Make sure the Make This Changelist Active check box is selected, as shown in Figure 7-12.

Figure 7-12. Creating a new changelist for the branch work
【翻译】图7-12 为分支工作创建一个新的更改列表


To begin your new feature, you need to add a new option to the dialog box that shows when a reminder is clicked. Open RemindersActivity.java and go to the top of your onItemClick() method in the first OnItemClickListener nested class which is attached to the mListViewvariable. Add Schedule Reminder as a third entry in the String array that builds the clickable options as shown in line 92 of Figure 7-13. Next you need to allow the user to set the time for the reminder when your new option is clicked. Find the second nested OnItemClickListener that you attach to the modeListView that creates the dialog box when individual reminders are clicked. This will be after the dialog.show() method
invocation. Look inside its onItemClick() method as seen on line 101 and make the changes shown in Figure 7-13. You will need to resolve the import for the Date class.
【翻译】为了创建新功能,你需要添加一个新选项到对话框上,该框能显示reminder被点击的时间。打开RemindersActivity.java 文件夹,去你的onItemClick() method的上方,这个method在第一个附加在mListViewvariable上的OnItemClickListener嵌套类中。添加Schedule Reminder 作为 String 列阵中的第三个项目,该列阵建立了如图7-13的92行所示的可点击的选项。接下来,你需要授权用户在你的新选项被点击时为提醒事项设置时间。找到你把它附在modeListView 上的第二个nestedOnItemClickListener,用它新建当个人提醒事项被点击时出现的对话框。这会紧跟在dialog.show() method
调用后面。查看如101行所示的onItemClick() method,作出如图7-13所示的更改。你需要解决日期类信息的导入问题。

Figure 7-13. Changes for scheduled reminders
【翻译】图7-13 为scheduled reminders作出更改


Here you change the else block where the reminders are deleted to an else if block, which checks for the position at index 1. You add an else block that runs when the third new option is clicked. This block creates a new Date representing today and uses it to build a TimePickerDialog. Run the app now to test the new option. Figure 7-14 shows the new feature in action.
【翻译】把提醒事项被删除的else block更改成在索引1中检验位置的else ifblock。添加一个当第三个新建选项被点击时能运行的elseblock。这个block能新建代表“今天”的Date 文件,接着用这个block来创建一个TimePickerDialog文件。运行这个程序来测试这个新选项。图7-14 显示了操作中的新功能。

Figure 7-14. Trying the Schedule Reminder option
【翻译】图7-14 尝试Schedule Reminder选项


Now that you have part of your new feature working, press Ctrl+K | Cmd+K to commit with the message Adds new scheduled time picker option. Go back to the IDE and move the two lines that find the reminder outside of the position==0 condition. Mark the reminder variable as final. See Figure 7-15 for an example.
【翻译】既然你的部分新功已经能成功运行了,点击Ctrl+K | Cmd+K提交,记得在提交中加上Adds new scheduled time picker option。返回IDE,移动position==0condition外的能找到提醒事项的两条线。把reminder变量标记为“final”。请看图例7-15

Figure 7-15. Move the reminder variable outside the if block
【翻译】图7-15 移动if block外的reminder变量

Next go to the else block you just added where you construct and show the time picker dialog box. Add the following code just before the line that shows the dialog box corresponding to line 113 in Figure 7-13:
【翻译】接下来去你刚刚添加的elseblock,在这儿你建立并演示了time picker对话框。在显示与图7-13中的113行相一致的对话框的这一行的前方,添加下列代码:
final Date today = new Date();
TimePickerDialog.OnTimeSetListener listener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hour, int minute) {
Date alarm = new Date(today.getYear(), today.getMonth(), today.getDate(), hour,
minute);
scheduleReminder(alarm.getTime(), reminder.getContent());
}
};

This creates a listener for the time picker dialog box. Inside this listener, you use today’s date as the base time for your alarm. You then include the hour and minute chosen from the dialog box to create the alarm date variable for your reminder. You use both the alarm time and the reminder’s content in a new scheduleReminder() method. Android Studio will flag the TimePicker as an unresolved class and flag the scheduleReminder() method as an unresolved method. Press Alt+Enter to resolve the import for the TimePicker class. Press F2 and Alt+Enter again to open the IntelliSense dialog box and then press Enter to have Android Studio generate the method for you, as shown in Figure 7-16.
【翻译】就这样,你就为time picker对话框创建了一个listener。在listener内部,把今天的日期设为你闹钟的初始日期。然后把从对话框中选择的小时和分钟囊括进去,为你的提醒事项建立闹钟日期变量。用一种新的scheduleReminder()方法,使用闹钟时间和提醒事项内容。Android Studio会把TimePicker标记为未被解决类,把scheduleReminder()method 标记为未解决method。按住Alt+Enter来解决TimePicker类的导入问题。再次按住F2和Alt+Enter,打开IntelliSense对话框,接着按住 Enter键让Android Studio为你生成相关方法,如图7-16所示。

Figure 7-16. Generate method using IntelliSense
【翻译】图7-16 用IntelliSense生成相关方法


Choose the RemindersActivity class, as shown in Figure 7-17.
【翻译】选择RemindersActivity 类,如图 7-17所示。

Figure 7-17. Selecting the RemindersActivity as the target class
【翻译】图7-17 选择RemindersActivity作为目标类


Add the following code to the new method body:
【翻译】把下列代码添加到新的method主体上:

Ala