理解 XCode 中的 Git 版本控制 已翻译 100%

isaced 投递于 2014/04/25 16:14 (共 40 段, 翻译完成于 04-30)
阅读 16891
收藏 124
11
加载中

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

已有 1 人翻译此段
我来翻译

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.

已有 1 人翻译此段
我来翻译

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.

已有 1 人翻译此段
我来翻译

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.

已有 1 人翻译此段
我来翻译

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.

已有 1 人翻译此段
我来翻译

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

已有 2 人翻译此段
我来翻译

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

已有 1 人翻译此段
我来翻译

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.

已有 1 人翻译此段
我来翻译

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.

已有 1 人翻译此段
我来翻译

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

已有 1 人翻译此段
我来翻译
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接。
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。
加载中

评论(22)

iHTCboy
iHTCboy
谢谢,现在才看到,走了很多弯路啊。
白色猿
很好学习了。貌似少了git的初始化配置,pull 和push的操作介绍?
开源中国匿名会员
开源中国匿名会员

引用来自“戴仓薯”的评论

既然大家都用“提交”,我也都改过来了。

引用来自“Tong_G”的评论

一般fork,commit,push,pull什么的都不翻译,直接写出来,因为翻译成汉语容易引起歧义

引用来自“风起q”的评论

的确

引用来自“开源中国匿名会员”的评论

我发音比较怪,所以我都是说, 订阅,提交,上传,下载。

引用来自“风起q”的评论

英语! 一定要说出来
随便啦,说出来有异议还不如说普通话。
z
zx32342342

引用来自“戴仓薯”的评论

既然大家都用“提交”,我也都改过来了。

引用来自“Tong_G”的评论

一般fork,commit,push,pull什么的都不翻译,直接写出来,因为翻译成汉语容易引起歧义

引用来自“风起q”的评论

的确

引用来自“开源中国匿名会员”的评论

我发音比较怪,所以我都是说, 订阅,提交,上传,下载。
英语! 一定要说出来
开源中国匿名会员
开源中国匿名会员

引用来自“戴仓薯”的评论

既然大家都用“提交”,我也都改过来了。

引用来自“Tong_G”的评论

一般fork,commit,push,pull什么的都不翻译,直接写出来,因为翻译成汉语容易引起歧义

引用来自“风起q”的评论

的确
我发音比较怪,所以我都是说, 订阅,提交,上传,下载。
oszbl
oszbl
代码
int a = 5;
int b = 10;

self.sum = a + b;

NSLog("The result is: %d", self.sum);

应该是笔误了。

最后一句应为
NSLog(@"The result is: %d", self.sum);
g
guanxf
come on!
改变时尚
改变时尚
Mark
戴仓薯
戴仓薯

引用来自“戴仓薯”的评论

既然大家都用“提交”,我也都改过来了。

引用来自“牛仔豆”的评论

你之前是用什么的?

引用来自“戴仓薯”的评论

之前用 commit ……没翻译

引用来自“牛仔豆”的评论

说commit可能懂的人更多,哈哈
嗯嗯~ 以后就保留了。
牛仔豆
牛仔豆

引用来自“戴仓薯”的评论

既然大家都用“提交”,我也都改过来了。

引用来自“牛仔豆”的评论

你之前是用什么的?

引用来自“戴仓薯”的评论

之前用 commit ……没翻译
说commit可能懂的人更多,哈哈
返回顶部
顶部