1
0
Fork 0
git-reference/zh/creating/index.html

124 lines
4.6 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
layout: zh_reference
---
<div class="box">
<h2>获取与创建项目</h2>
<div class="block">
<p>
你得先有一个 Git 仓库,才能用它进行操作。仓库是 Git 存放你要保存的快照的数据的地方。
</p>
<p>拥有一个 Git 仓库的途径有两种。在已有的目录中,初始化一个新的,其一。
比如一个新的项目,或者一个已存在的项目,但该项目尚未有版本控制。如果你想要复制一份别人的项目,
或者与别人合作某个项目,也可以从一个公开的 Git 仓库克隆,其二。本章将对两者都做介绍。
</p>
</div>
</div>
<div class="box">
<h2>
<span class="docs">
<a target="new" href="http://git-scm.com/docs/git-init">文档</a> &nbsp;
<a target="new" href="http://git-scm.com/book/en/Git-Basics-Getting-a-Git-Repository#Initializing-a-Repository-in-an-Existing-Directory"></a>
</span>
<a name="init">git init</a>
<span class="desc">将一个目录初始化为 Git 仓库</span>
</h2>
<div class="block">
<p>在目录中执行 <code>git init</code>,就可以创建一个 Git 仓库了。比如,我们恰好有个目录,里头有些许文件,如下:
</p>
<pre>
<b>$ cd konnichiwa</b>
<b>$ ls</b>
README hello.rb
</pre>
<p>
在这个项目里头,我们会用各种编程语言写 "Hello World" 实例。
到目前为止,我们只有 Ruby 的,不过,这才刚上路嘛。为了开始用 Git 对这个项目作版本控制,我们执行一下 <code>git init</code>
</p>
<pre>
<b>$ git init</b>
Initialized empty Git repository in /opt/konnichiwa/.git/
# 在 /opt/konnichiwa/.git 目录初始化空 Git 仓库完毕。
</pre>
<p>现在你可以看到在你的项目目录中有个 <code>.git</code> 的子目录。
这就是你的 Git 仓库了,所有有关你的此项目的快照数据都存放在这里。
</p>
<pre>
<b>$ ls -a</b>
. .. .git README hello.rb
</pre>
<p>恭喜,现在你就有了一个 Git 仓库的架子,可以开始快照你的项目了。
</p>
<p class="nutshell">
<strong>简而言之</strong>,用 <code>git init</code> 来在目录中创建新的 Git 仓库。
你可以在任何时候、任何目录中这么做,完全是本地化的。
</p>
</div>
</div>
<div class="box">
<h2>
<span class="docs">
<a target="new" href="http://git-scm.com/docs/git-clone">文档 </a> &nbsp;
<a target="new" href="http://git-scm.com/book/en/Git-Basics-Getting-a-Git-Repository#Cloning-an-Existing-Repository"></a>
</span>
<a name="clone">git clone</a>
<span class="desc">复制一个 Git 仓库,以上下其手</span>
</h2>
<div class="block">
<p>如果你需要与他人合作一个项目,或者想要复制一个项目,看看代码,你就可以克隆那个项目。
执行 <code>git clone [url]</code>[url] 为你想要复制的项目,就可以了。
</p>
<pre>
<b>$ git clone git://github.com/schacon/simplegit.git</b>
Initialized empty Git repository in /private/tmp/simplegit/.git/
remote: Counting objects: 100, done.
remote: Compressing objects: 100% (86/86), done.
remote: Total 100 (delta 35), reused 0 (delta 0)
Receiving objects: 100% (100/100), 9.51 KiB, done.
Resolving deltas: 100% (35/35), done.
<b>$ cd simplegit/</b>
<b>$ ls</b>
README Rakefile <span class="blue">lib</span>
</pre>
<p>上述操作将复制该项目的全部记录,让你本地拥有这些。并且该操作将拷贝该项目的主分支,
使你能够查看代码,或编辑、修改。进到该目录中,你会看到 <code>.git</code> 子目录。
所有的项目数据都存在那里。
</p>
<pre>
<b>$ ls -a</b>
. .. <span class="blue">.git</span> README Rakefile <span class="blue">lib</span>
<b>$ cd .git</b>
<b>$ ls</b>
HEAD description <span class="blue">info</span> packed-refs
<span class="blue">branches hooks logs refs</span>
config index <span class="blue">objects</span>
</pre>
<p>默认情况下Git 会按照你提供的 URL 所指示的项目的名称创建你的本地项目目录。
通常就是该 URL 最后一个 <code>/</code> 之后的任何东西。如果你想要一个不一样的名字,
你可以在该命令后加上它,就在那个 URL 后面。
</p>
<p class="nutshell">
<strong>简而言之</strong>,使用 <code>git clone</code> 拷贝一个 Git 仓库到本地,让自己能够查看该项目,或者进行修改。
</p>
</div>
</div>
<p><a href="/zh/basic">接下来:基本快照 &#187;</a></p>