这是之前写给同事看的《Git 快速入门》,对其他人可能也有用。这篇文档还在不断地调整和修改。
一点说明
这个教程里使用了大量的命令行。简单起见,所有 Command Line Prompt(命令行提示符)都是 Unix 式的(即美元符号$),例如:
$ echo "hello, world"
这篇文档里没有包含 Git 的安装方法,不过你可以参考:
文档开始
Git 的初始化
从远端服务器获取已经初始化的 Git 代码仓库
从远端服务器下载一个完整的 Repository(代码仓库,通常简称“Repo”)以开始编辑,使用
$ git clone
举例:
$ git clone git://github.com/h5bp/html5-boilerplate.git
在本地文件夹内初始化 Git
要在本地的文件夹里开始使用 Git ,需要对 Git 进行初始化:
$ cd /path/to/myapp/ // 进入要使用 Git 的文件夹
$ git init // 初始化 git ,这个操作会有两个文件(夹)被创建:
// .git -> 用于存储 Git 信息文件夹
// .gitignore -> 不需要 Git 追踪的文件,比如系统自动生成的 Thumbs.db 文件。
参考:.gitignore 模板集。
使用 Git 对代码进行快照
Git 在初始化后,并未追踪该 Repo 下的任何文件。因此要给 Git 一个命令,指定追踪范围并创建代码快照:
$ git add . // 把当前目录及其子目录下的所有文件加入 Git 并创建快照
这时快照已经建立,但并未正式进入代码树,这就好比用 Word 新建了一个文档,但并未保存到硬盘里。
下一步需要提交 commit(相当于“存盘”),使用
$ git commit // 其后可接参数 -m 以加入对 commit 的描述信息
很重要的是,如果在 $ git add . 操作完成后,又对代码进行了修改,则需要在提交 commit 前把 $ git add . 再做一次。
在之前的实例中,我们在 $ git add 中指定的追踪范围为“.”,也就是当前文件夹及其子目录。你也可以使用 $ git add 单独添加任何文件。
在一个项目中初始化 git 并进行第一次 commit 的完整步骤:
$ cd /path/to/myapp/ $ git init $ git add . $ git commit -m "initial commit"
调整控制 git 追踪的命令其实有三个:
git addgit rmgit mv
第一个已经讲过了。第二个和第三个分别表示删除和移动文件(mv也可以用来重命名)。
要删除和移动文件,不要直接操作,而应该用 git rm/mv 命令进行,以通知 git 修改其追踪文件范围。用法和基本的 Unix/Linux 命令相同。
举例:要删除文件 delete_me.html 和文件夹 legacy/
$ git rm delete_me.html $ git rm -r legacy/
举例:要重命名 rename_me.htm 文件为 rename_me.html
$ git mv rename_me.htm rename_me.html
查看当前代码仓库的变化,使用
$ git status
在每一个 commit 之前都检查一下代码仓库的状态永远是个好主意,这可以防止出现一些问题(比如手工删除了某个文件,而没有进行 git rm 以移除 git 代码仓库里的对应文件)。
$ git add . $ git status $ git commit -m "YOUR DESCRIPTION GOES HERE"
如果你希望查看代码历史,请使用
$ git log
系统会出现完整的日志,可以上下翻页,按 q 退出。
举例:
$ git log commit 3e45b235fb47095ed8bcccb4233d3ac92812313d Author: Marco YinDate: Mon Oct 17 22:18:55 2011 +0800 activate non-default modules for Devise commit b252e45f05c36f4467e559711a08d7a0731346e6 Author: Marco Yin Date: Mon Oct 17 22:03:36 2011 +0800 generate user model with Devise without any change commit af5233c3c4b5534fd63cfdf9b93ed36a42ea30be Author: Marco Yin Date: Mon Oct 17 21:54:15 2011 +0800 install Devise with related configration done commit ce948087ff3e91de0cd57e3af4dabccf294ccaac Author: Marco Yin Date: Mon Oct 17 21:31:35 2011 +0800 initial commit (END)
我们还可以使用 git checkout 快速地回到某个历史版本。
接着上面的例子:
$ git log commit 3e45b235fb47095ed8bcccb4233d3ac92812313d Author: Marco YinDate: Mon Oct 17 22:18:55 2011 +0800 activate non-default modules for Devise commit b252e45f05c36f4467e559711a08d7a0731346e6 Author: Marco Yin Date: Mon Oct 17 22:03:36 2011 +0800 generate user model with Devise without any change commit af5233c3c4b5534fd63cfdf9b93ed36a42ea30be Author: Marco Yin Date: Mon Oct 17 21:54:15 2011 +0800 install Devise with related configration done commit ce948087ff3e91de0cd57e3af4dabccf294ccaac Author: Marco Yin Date: Mon Oct 17 21:31:35 2011 +0800 initial commit (END) $ git checkout af5233c3c4b5534 // 我们只需要输入要回到的历史版本哈希值的前几位就可以了。
要回到最新版本,请使用命令行:
$ git checkout HEAD // HEAD 简单说就是当前工作分支的最新版本(不严谨说法)。
之前提到,每做一个功能都尽量使用分支。
要列出、创建、删除分支,使用 git branch 携带不同参数。
$ git branch // 列出当前所有分支 * create-workspaces // 带星号的表示当前工作分支 master // master 是主分支 $ git branch a-new-branch // 创建一个名为 a-new-branch 的分支 $ git branch // 列出当前所有分支 a-new-branch * create-workspaces master $ git checkout master // 切换到 master 分支 $ git branch -D a-new-branch // 删除 a-new-branch 分支
还有一个常见的做法就是直接创建并切换到新的分支:
$ git checkout -b a-new-branch $ git branch // 列出当前所有分支 * a-new-branch master
在切换分支的过程中,可能会遇到当前分支有已修改内容未 commit 的情况。这时候可以将其暂存起来,然后再切换:
$ git checkout master
error: Your local changes to the following files would be overwritten by checkout:
app/models/workspace.rb
db/schema.rb
Please, commit your changes or stash them before you can switch branches.
Aborting
$ git add .
$ git stash
Saved working directory and index state WIP on a-new-branch: 75c5d6e add authentication to workspaces
HEAD is now at 75c5d6e add authentication to workspaces
$ git checkout master
Switched to branch 'master'
当工作分支完成了它的使命,我们就可以把工作分支合并到主分支上来。
$ git checkout master // 先切换到主分支 $ git merge create-workspaces // 将 create-workspaces 合并到主分支上 $ git branch -D create-workspaces // 删除 create-workspaces 分支






