#第四章 通過實(shí)踐操作 學(xué)習(xí)Git
4.1 基本操作
- git init---初始化倉庫
$ mkdir git-tutorial
$ cd git-tutorial
$ git init
Initialized empty Git repository in /Users/hirocaster/github/github-book
/git-tutorial/.git/
執(zhí)行了 git init命令的目錄下就會生成 .git 目錄。這個 .git 目錄里存儲著管理當(dāng)前目錄內(nèi)容所需的倉庫數(shù)據(jù)姆涩。
- git?status——查看倉庫的狀態(tài)
$ git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
稍加改變后
$ touch README.md
$ git status
# On branch master
#
# Initial commit
## Untracked files:# (use "git add <file>..." to include in what will
be committed)#
# README.md
nothing added to commit but untracked files present (use "git add" to
track)
- git?add——向暫存區(qū)中添加文件
$ git add README.md
$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: README.md
#
可以明顯的看到狀態(tài)的變化
- git?commit——保存?zhèn)}庫的歷史記錄
-記述一行提交信息
$ git commit -m "First commit"
[master (root-commit) 9f129ba] First commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README.md
-m 參數(shù)后的 "First commit"稱作提交信息援岩,是對這個提交的
概述变逃。
-記述詳細(xì)提交信息
● 第一行:用一行文字簡述提交的更改內(nèi)容
● 第二行:空行
● 第三行以后:記述更改的原因和詳細(xì)內(nèi)容
- 查看提交后的狀態(tài)
$ git status
# On branch master
nothing to commit, working directory clean
- git?log——查看提交日志
$ git log
commit 9f129bae19b2c82fb4e98cde5890e52a6c546922
Author: hirocaster <hohtsuka@gmail.com>
Date: Sun May 5 16:06:49 2013 +0900
First commit
不妨養(yǎng)成這樣一個好習(xí)慣:在執(zhí)行 git commit命令之前先執(zhí)行g(shù)it diff HEAD命令欧引,查看本次提交與上次提交之間有什么差別,等確認(rèn)完畢后再進(jìn)行提交重贺。這里的 HEAD 是指向當(dāng)前分支中最新一次提交的指針耿导。
4.2 分支的操作
通過靈活運(yùn)用分支声怔,可以讓多人同時高效地進(jìn)行并行開發(fā)。
- git?branch——顯示分支一覽表
$ git branch
* master
git?checkout?-b——創(chuàng)建舱呻、切換分支
切換到 feature-A 分支并進(jìn)行提交
git?branch——顯示分支一覽表
- 切換回上一個分支
$ git checkout -
Switched to branch 'feature-A'
- 特性分支
特性分支顧名思義醋火,是集中實(shí)現(xiàn)單一特性(主題),除此之外不進(jìn)
行任何作業(yè)的分支箱吕。
- git?merge——合并分支
$ git merge --no-ff feature-A
4.3 更改提交的操作
- git?reset——回溯歷史版本
- 要讓倉庫的 HEAD芥驳、暫存區(qū)、當(dāng)前工作樹回溯到指定狀態(tài)茬高,需要用
到 git rest --hard命令兆旬。
- 創(chuàng)建 fix-B 分支
$ git checkout -b fix-B
Switched to a new branch 'fix-B'
- 使用 git reflog命令,查看當(dāng)前倉庫的操作日志怎栽。
- 消除沖突
- git?commit?--amend——修改提交信息
- git?rebase?-i——壓縮歷史
- 修正拼寫錯誤
$ git rebase -i HEAD~2
4.4 推送至遠(yuǎn)程倉庫
- git?remote?add——添加遠(yuǎn)程倉庫
- git?push——推送至遠(yuǎn)程倉庫
- 推送至 master 以外的分支
$ git checkout -b feature-D
$ git push -u origin feature-D
4.5 從遠(yuǎn)程倉庫獲取
- git?clone——獲取遠(yuǎn)程倉庫
- git?pull——獲取最新的遠(yuǎn)程倉庫分支
- 如果兩人同時修改了同一部分的源代碼丽猬, push 時就很容易發(fā)生沖突。所以多名開發(fā)者在同一個分支中進(jìn)行作業(yè)時熏瞄,為減少沖突情況的發(fā)生脚祟,建議更頻繁地進(jìn)行 push 和 pull 操作。
4.6 幫助大家深入理解 Git 的資料
- Pro?Git
[Pro Git]http://git-scm.com/book/zh/v1
[LearnGitBranching]http://pcottle.github.io/learnGitBranching/
[tryGit]http://try.github.io/