加载中

During an application development process, a quite significant part is the way developers manage to keep track of the changes been made over time. It really consists of a necessary need to be able to store and handle copies of working code versions in various stable stages, and revert back to them when bugs or problems arise. Even more, when a number of programmers work at the same project, keeping track of all changes is a one-way path. Fortunately, developers don’t have to discover their own way to do all that, as there are special software solutions, called Version Control Systems.

A version control system, or in other words a revision control system, is actually a (software) mechanism that is capable of monitoring changes performed to code files over time and storing them for future reference. Besides that, it can also save extra essential data, such as the developer who made the changes, when did they happen, what was actually modified, and other kind of historical and not only data. Moreover, such a system provides the ability to compare various versions of the code, revert to a previous version of either specific files or a whole project if needed, and eventually implement a bug-free product by tracking down any malicious code.

git-source-code

在应用程序开发过程中,一个相当显著的部分是开发人员管理代码变更的方式。这些是必须包含的功能,存储和处理工作代码版本在不同阶段稳定阶段的副本,并能够恢复代码当存在缺陷或者产生问题的时候。更有甚者,当多个程序员工作在同一个项目时,跟踪所有的变更是一个单向的路径。幸运的是,开发人员不必去发明一种方法来做这些事情,有一个特别的软件解决方案,叫做版本控制系统(Version Control Systems

版本控制系统或者叫做修订控制系统,实际上是一种能够一直监视代码文件的变更并存储他们为了将来引用的机制(软件)。除了这些之外,版本控制系统也保存了额外的必要数据,例如哪个开发人员做了变更,变更发生在什么时候,实际上修订了什么,其他类型的历史不仅仅是数据。而且,版本控制系统提供了比较代码不同版本的功能,如果需要的话,可以恢复特定文件或者整个项目到以前的版本,并追查恶意的代码最终实现无缺陷产品。

git-source-code
 

Using a version control system, developers can work on different paths of a project, commonly named branches, and when their piece of project is ready, everything is put together so the final release of the application can be built. This process is called merging, and it consists of a special characteristic of version control systems. Actually, that fashion of work is mandatory in teams of developers and software companies, where each one if responsible for a project’s part, and at the end everything must be gathered up and put in one place.

For single developers it is not required to use a version control system, however is highly recommended, as with it’s much more easier to track down bugs or going back to stable and working versions of code, once a dead end appears or for some reason everything is messed up. The truth is that a great number of lone-rider programmers, especially the new ones, do not use such a system at all, and is common to manually keep copies of their projects when they are about to add new features or generally modify them. That’s a really bad habit, as a source control system does all that much better and efficiently, providing at the same time all the extra capabilities previously described.

使用版本控制系统,开发人员可以工作在项目的不同路径上,通常叫做分支(branch),当他们的代码完成时,所有的代码将合并到一起以便构建应用程序的最终发布版本。这个过程叫做合并代码(merging),它包含了版本控制系统的一个特殊特性。实际上,在开发团队和软件公司中代码的版本控制是一个强制性的工作,团队中的每个人负责项目的一部分,最后所有的代码被集中到一起放置在一个地方。

对个人开发者来说使用版本控制系统不是必要的,但是仍然是强烈推荐的。一旦遇到严重的问题或者说以代码都乱套了的时候,使用版本控制系统将更容易追查缺陷或者恢复代码到稳定版本和代码的工作版本。事实上,许多的个人程序员,特别是新人,根本不使用版本控制系统,当他们增加新功能或者是修改代码的时候,通常手动复制项目的副本。这是一个非常不好的习惯,源码控制能更好并且更有效率的完成这些工作,同时提供了前面描述的额外功能。

One of the most well-known (if not the most famous) version control systems, is the git, and it was originally developed by the creator of Linux, Linus Torvalds. Git organizes everything in virtual directories, named repositories, and any version tracking actually applies to them. It can be used either as a command line tool, or through desktop applications designed exactly for it. If git sounds new to you, then I would suggest to read a few more about it on the web, as discussing about it any further would be out of the scope of the tutorial.

Xcode, since version 5, incorporates an enriched capability for working with git, and there’s implemented an entire menu with various options and submenus in it for managing source control. As you’ll see later on, working with git and version control is pretty easy and fast, and after you’ll have the tutorial finished there will be no excuse for not using it.

作为最出名的版本控制系统之一,git由Linux创建者 Linus Torvalds开发。Git在虚拟目录(仓库)中组织一切,实际上,任何版本的跟踪都可以适用于他们。它既可以通过命令行使用,也可以通过桌面应用程序。如果git对你来说很陌生,那么我建议你在网络上阅读一些关于git的文章,而进一步讨论git超出了本教程的范围。

自从Xcode的第五版本起,就集成了git的丰富功能,包括各种选项菜单的按钮和管理源代码的子菜单。正像你稍后看到的,使用版本控制git是非常容易和快速的,在你看完本教程之后,没有理由不使用它。

Concluding this introduction, as you have already understood our mission is to learn how to work with git and version control through Xcode, and all this will be achieved by getting to know each related feature that Xcode provides. If you feel unfamiliar with all that stuff, or if you need to acquire some more knowledge before we begin, then please be my guest and search around the web for more information. I have to say in advance that during the tutorial I presume that you possess the minimum required knowledge about version control systems and what git is about, as we are going to take them for granted and focus mostly on how Xcode manages all that.

GIT Demo Overview

On the contrary with other tutorials’ demo applications, in this one we are not going to implement an app that demonstrates a specific feature of the iOS SDK, nor we will have at the end a developed sample product. What we will actually do, is to create a demo project just to add a few lines of code to various points, and we’ll use it as a tool to test all the source control management options Xcode provides. In other words, we will focus on IDE, and not on iOS.

总结而言之,你已经理解了我们的任务是学习怎样在Xcode中使用版本控制git,这一切将通过了解Xcode提供的每个相关特性来完成。如果你不熟悉所有的东西,或者在我们开始之前你需要获取更多的知识, 请在网络上搜索相关信息。我不得不说,在本教程中我假设你关于版本控制系统拥有最基本的知识,比如git是什么,因此我们要把它视为理所当然的事情,而关注Xcode怎么管理这一切。

Git示例演示

与其他教程的演示程序相反,本文中我们不会实现一个应用去展示iOS SDK的具体特性,也不会有一个最终的开发样本。实际上我们所做的就是去创建一个示例项目,仅仅在几个点上添加几行代码,我们将使用它作为一个工具去测试Xcode提供的所有源代码控制管理选项。换句话说,我们重点讨论的是IDE,而不是iOS。

In addition to the above, this time there is not an app to download. Instead, I invite you to come along and implement step by step the demo project, and add any code where is required manually (don’t worry, it’s not that much). It’s necessary to follow that way, as we are going to perform various repeated version control related actions and we must see the results in real time. That wouldn’t be possible if I would just provide an application with all actions already been made, as then the practice on your behalf would be equal to zero.

So, let’s get started, and let’s take a detailed look to the version control essentials using Xcode.

Creating a Git repository

Every time a new project is getting created in Xcode, the capability to add a local git repository is always given as an option to the developer. In a project creation, a three-step guided process is involved, and at the third and last step of it Xcode provides a check box with the appropriate message, which if selected the git repository is added to the project’s directory. It’s easy this option to be overlooked or to be considered as another unneeded Xcode feature, and usually that’s what happens, especially in cases of developers who have never worked with version control and git before, or they are new to programming.

除此之外,这一次不会有可供下载的例程了。取而代之的是,我邀请你一起来一步步实现这个 demo,并且在需要写代码的地方手动添加代码(别担心,不会很多)。这样跟下来是有必要的,因为我们将会重复进行多种与版本控制有关的操作,并且我们必须即时看到结果。如果我只是提供一个操作已经完成的例程,就不可能有这样的效果了,因为这样下来你自己的练习部分近乎于零。

那么,让我们开始吧!我们用 Xcode 来近距离地看一看版本控制系统的精华所在。

创建 Git 仓库

每次在 Xcode 中创建新项目,都会让开发者选择是否要添加一个本地 git repository。新建一个 project 涉及分为3步的引导过程,其中在第3步也就是最后一步中,Xcode 提供了一个勾选框和相应的说明,如果勾选了,一个 git repository 就会添加到保存 project 的目录中。这个选项很容易被忽略,或者被当做一个 Xcode 的没用特性,这种事经常发生,尤其是对于从来没用过版本控制和 git 的开发者,或者新手程序员。

To see everything in details, launch Xcode and create a new project. Firstly, as the application’s template select the Single View Application, in the Application category under the iOS section.

Version Control Xcode Project Template

Click on the Next button to proceed to the second step. In the Product Name field, set the GitDemo value as the project’s name, and also make sure that in the Devices drop down menu the iPhone value is selected. No need for an iPad or universal app here.

Version Control Xcode Project Template

Click once again on the Next button, and let the guide take you to the third and last step. In here, at first select a directory to save the project. Then, look at the bottom of the window, and notice a check box lying over there, with the text Create git repository on (My Mac ):

Version Control Xcode Project Template GIT

具体细节如下,启动Xcode并创建一个新项目。首先,选择“单视图应用程序(Single View Application)”作为应用程序的模板,同时在iOS选项部分,选择“应用程序(Application)”项。

点击“下一步(Next)”按钮到第二步,设定“产品名(Product Name)”字段为“GitDemo”,同时确保在“设备(Devices)”下拉菜单中选择“iPhone”。在这里不需要iPad或者普通应用。

再次点击下一步按钮,进入最后一步。在此,首先选择保存项目的目录。然后选中窗口的底部的单选框,并选中在“My Mac”上创建git仓库。

By default, this option is always selected and a git repo is created with every project. In case you don’t want to use git and version control in your project, you just have to deselect it, but I wouldn’t recommend this. Anyway, in this tutorial we want it enabled, there for make sure that you select the check box. Finally, click on the Create button.

Let the project to be created, and then open a Finder window and navigate to the directory that you stored our project. In there, look for the .git sub-directory, which is the one Xcode automatically created for storing the git repository related data.

Version Control Xcode GIT Directory

If you are unable to see the .git directory, then you must make the hidden files on your computer visible. To do that, open a Terminal window, and add the next commands:

For OS X Mavericks 10.9:

1

defaults write com.apple.finder AppleShowAllFiles TRUE

For previous OS X versions:

1

defaults write com.apple.Finder AppleShowAllFiles TRUE

Then, just reload the Finder app, so add one more command:

1

killall Finder

默认情况下,这个勾选框总是被选中的,然后每个 project 都会创建一个 git repo。如果你的项目不想用 git 和版本控制,只需取消选中,但我不建议这样做。总之,本教程中我们希望启用git,因此确保你选中了勾选框。最后,点击 Create 按钮。

等待 project 创建完成吧,然后打开一个 finder 窗口,来到我们保存 project 的目录下。在这里,找到 .git 子目录,这是 Xcode 自动创建的目录,用来存储 git repository 相关的数据。

Version Control Xcode GIT Directory

如果你看不到 .git 目录,你必须把电脑上的隐藏文件改为可见。首先,打开Terminal(终端) 窗口,然后输入以下命令:

对于 OS X Mavericks 10.9:


defaults write com.apple.finder AppleShowAllFiles TRUE

对于此前的 OS X 版本:


defaults write com.apple.Finder AppleShowAllFiles TRUE

然后,只需重启 Finder 应用,所以再输入一条命令:


killall Finder

So, as you see, here is the place where your local git repository actually gets saved for this app. Accordingly, a .git sub-directory is always created along with any new application you create, as long as you leave the respective option enabled.

It’s obvious that adding a git repository to a project is totally effortless using Xcode. However, what happens if you don’t include a git repository during a project setup, or if you want to add one at a later time? Well, the good news is that you can add a repository for your project at any time, but without using Xcode, and even that’s a rare case to happen, I’m going to show you how.

Note that if you wish, you may skip the rest and proceed to the next part of tutorial. However I would recommend to keep on reading, as the information provided right next is quite useful.

因此,如你所见,这个 app 的本地 git repository 实际就保存在这里。相应地,你创建的任何新应用都会随之带来一个 .git 子目录,只要你保持相应的选项是勾选的。

显然,用 Xcode 来为 project 添加 git repository 是不费吹灰之力的。然而,如果你在创建 project 时没有添加 git repository,或者想要稍后再添加,怎么办呢?好消息是,你随时都可以为 project 添加 repository,但是不用 Xcode 了。尽管这样的情况很少见,我还是来讲解一下。

注意,如果你不想看的话,可以尽管跳过本教程的下一节。但我建议还是读下去,因为紧接着再下一节的内容会非常重要。

Before demonstrating anything, you must download the Command Line Tools through Xcode, as we are going to work in Terminal and we need some extra tools available. If you have already downloaded this package, then keep forward. If not, then to install the command line tools, go to the Xcode > Preferences… menu on Xcode, and then select the Downloads tab. In the upper side of the window, under the Components section, click on the button with a down arrow existing at the right side of the Command Line Tools option. Once the download is over, a checkmark will appear instead of the download button:

Version Control Command Line Tool

Now, create one more Xcode project for the sake of this example, and delete it when we are through. Make sure to deselect the Create git repository option. We don’t want Xcode to prepare a repository for us in this case. Name the project NoGitExample and beyond that, save it in your Desktop, so you can follow along with the commands I will show.

在开始讲之前,你首先需要在 Xcode 里下载 Command Line Tools ,因为我们接下来要用 Terminal ,需要一些工具。如果你已经下载了这个包,就进行下一步。如果没有,要安装 command line tools,点击Xcode里的 Xcode > Preferences… 菜单,然后选择 Downloads 一项。在窗口的上部,Components 一栏下,点击 Command Line Tools 右侧画着向下箭头的按钮。一旦下载结束,下载按钮会变成对勾符号。

Version Control Command Line Tool

然后,为了这个例子再创建一个 Xcode project,我们一切完成之后再把它删除。这一次确保要取消勾选 Create git repository 选项。在这个例子里,我们不需要 Xcode 来为我们准备 repository 了。把这个项目命名为 NoGitExample,并且保存在桌面上,这样就能直接使用我接下来的提供的指令了。

Once everything is ready, open a Terminal window (if you have already opened any, make sure to close it first, so any changes made by installing the command line tools to be applied). For starters, navigate your self to the new project’s directory:

1

cd /Users/YOUR-USERNAME/Desktop/NoGitExample

Don’t forget to set your Mac username in the above command. Next:

1

git init

This will initialize an empty repository, and if you either go to Finder or use the ls terminal command, you’ll see that the .git sub-directory has been created. That’s great. Keep going:

1

git add .

With this one, all contents of the current directory (the dot [.] symbol) will be added to the repository. Finally, commit all (make the changes permanent):

1

git commit -m 'Initial commit'

A list of all files committed to the local git repository will appear to the terminal window. The next figure illustrates the terminal of mine:

Version Control Terminal

一切就绪,打开一个 Terminal 窗口(如果之前已经有打开的窗口,确定要先关闭再重启,这样才能应用安装 command line tools 时做出的改变)。首先,来到保存新 project 的目录下:


cd /Users/YOUR-USERNAME/Desktop/NoGitExample

别忘了把上面的指令改为你自己 Mac 的用户名。接下来:


git init

这会初始化一个空的 repository。然后如果你打开 Finder 或者在 terminal 输入 ls 指令,你会看到 .git 子目录已经创建出来了。太棒了。继续往下:


git add .

用这个指令,当前目录(点号[.])的所有内容都会添加到 repository 中。最后,提交全部(也就是持久保存所做的更改):


git commit -m 'Initial commit'

Terminal 窗口中会出现提交到本地 git repository 的文件列表。下图就是我的terminal的样子:

Version Control Terminal

返回顶部
顶部