Git全解析之用起來先

- 1. Git全解析之用起來先
- 1.1. 先安裝Git環境
- 1.2. 配置
- 1.3. 簡單了解Git
- 1.3.1. Git對象模型 SHA
- 1.3.2. Git目錄與工作目錄 </ol> </li>
- 1.4. 可以開始使用了
- 1.4.1. 獲取倉庫
- 1.4.2. 正常的工作流程
- 1.4.3. 分支與合并
- 1.4.3.1. 基本操作命令
- 1.4.3.2. 分支沖突
- 1.4.3.3. 撤銷合并 </ol> </li>
- 1.4.4. log記錄
- 1.4.5. 比較差異DIFF </ol> </li>
- 1.5. 附錄 .gitconfig文件配置參考
- 1.6. 參考
- 1.7. 捐贈 </ol> </li> </ol> </div>
- blob用來存儲文件數據
- tree有點像一個目錄,用來管理一些blob與tree
- commit,一個commit只指向一個tree,用來標記項目某一個特定時間點的狀態,即一次提交
- tag,一個tag用來標記一個commit
-
創建倉庫,在工作目錄下執行
git init .
-
clone一個倉庫
#通過http(s)協議 git clone https://github.com/git/git.git #通過ssh協議 git clone git@github.com:git/git.git
-
修改文件,將它們更新的內容添加到索引中
git add file1 file2 file3 #也可以通過git add . 來添加所有變動到暫存區 git add .
-
查看當狀態
$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: file1 # modified: file2 # modified: file3 #
-
提交commit
#執行后會進入編輯器進行注釋編輯 $ git commit #如果注釋很短也可以使用 $ git commit -m "注釋內容" #如果只是修改了文件,沒有添加新的文件,可以省略git add $ git commit -am "注釋內容"
Git全解析之用起來先
先安裝Git環境
下載安裝: http://git-scm.com/download/
配置
配置user與email,用來標識用戶
$ git config --global user.name "wustrive" $ git config --global user.email "wustrive2008@gmail.com"
也可以直接編輯配置文件,配置文件默認路徑在用戶目錄下的.gitconfig文件中,結構是:
[user] name = wustrive email = wustrive2008@gmail.com
簡單了解Git
Git是一個快速的分布式版本控制系統
Git對象模型 SHA
所有用來表示項目歷史信息的文件是通過一個40個字符“對象名”來索引的。每一個“對象名”都是對“對象”內容做SHAI哈希計算來的。這個對象名是全局唯一的,不同的對象生成的“對象名”不同。
Git中四種類型的對象:”blob”,”tree”,”commit”和”tag”。每個對象包括三個部分:類型,大小和內容。
Git目錄與工作目錄
git目錄是為你的項目存儲所有歷史和元信息的目錄,包括所有對象,這些對象指向不同的分支,每個項目只能有一個Git目錄,這個叫’.git’的目錄一般在項目的根目錄下,
這個目錄下的重要文件有:
. |-- FETCH_HEAD #指向著目前已經從遠程倉庫取下來的分支的末端版本。 |-- HEAD #這個git項目當前處在哪個分支里 |-- ORIG_HEAD #HEAD指針的前一個狀態 |-- branches/ #項目的所有分支 |-- config/ #項目的配置信息,git config命令會改動它 |-- description #項目的描述信息 |-- hooks/ #系統默認鉤子腳本目錄 |-- index #索引文件 |-- info/ #包含倉庫的一些信息 |-- logs/ #各個refs的歷史信息 |-- objects/ #Git本地倉庫的所有對象 (commits, trees, blobs, tags) |-- packed-refs #運行 git gc, refs 下的所有文件都會消失。Git 會將這些文件挪到 .git/packed-refs 文件中去以提高效率 |-- refs/ #標識你項目里的每個分支指向了哪個提交(commit)
工作目錄就是你的項目源代碼目錄,即是你簽出(checkout)用來編輯的文件,當在不同的分支間切換時,工作目錄里的內容會隨之替換或刪除,所有的操作歷史都保存在Git目錄中,工作目錄是用來臨時保存checkout文件的地方。
可以開始使用了
獲取倉庫
提示:創建和clone后默認的分支是master,默認的repository引用名稱origin
正常的工作流程
分支與合并
基本操作命令
#查看本地分支 *代表當前所在分支 [centos@bogon gittest]$ git branch * master #新建分支 [centos@bogon gittest]$ git branch br1 [centos@bogon gittest]$ git branch br1 * master #切換分支 [centos@bogon gittest]$ git checkout br1 Switched to branch 'br1' [centos@bogon gittest]$ git branch * br1 master #切換并合并分支,以當前分支為基礎新建分支 [centos@bogon gittest]$ ll total 0 -rw-rw-r--. 1 centos centos 0 Jan 6 23:32 file1 -rw-rw-r--. 1 centos centos 0 Jan 6 23:32 file2 -rw-rw-r--. 1 centos centos 0 Jan 6 23:32 file3 [centos@bogon gittest]$ git branch * br1 master [centos@bogon gittest]$ git checkout -b br2 Switched to a new branch 'br2' [centos@bogon gittest]$ ll total 0 -rw-rw-r--. 1 centos centos 0 Jan 6 23:32 file1 -rw-rw-r--. 1 centos centos 0 Jan 6 23:32 file2 -rw-rw-r--. 1 centos centos 0 Jan 6 23:32 file3 [centos@bogon gittest]$ git branch br1 * br2 master #合并分支,將其他分支合并到當前分支 [centos@bogon gittest]$ ll total 0 -rw-rw-r--. 1 centos centos 0 Jan 6 23:32 file1 -rw-rw-r--. 1 centos centos 0 Jan 6 23:32 file2 -rw-rw-r--. 1 centos centos 0 Jan 6 23:32 file3 [centos@bogon gittest]$ git merge br2 Updating da5068b..f9da174 Fast-forward br2-file | 1 + 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 br2-file [centos@bogon gittest]$ ll total 4 -rw-rw-r--. 1 centos centos 10 Jan 6 23:44 br2-file -rw-rw-r--. 1 centos centos 0 Jan 6 23:32 file1 -rw-rw-r--. 1 centos centos 0 Jan 6 23:32 file2 -rw-rw-r--. 1 centos centos 0 Jan 6 23:32 file3 #刪除分支,刪除已經被合并過的分支,安全刪除分支 [centos@bogon gittest]$ git branch br1 br2 * master [centos@bogon gittest]$ git branch -d br2 Deleted branch br2 (was f9da174). [centos@bogon gittest]$ git branch br1 * master #強制刪除分支 [centos@bogon gittest]$ git branch br1 * master [centos@bogon gittest]$ git branch -D br1 Deleted branch br1 (was da5068b). [centos@bogon gittest]$ git branch * master
分支沖突
如果執行自動合并沒有成功的話,git會在索引和工作樹里設置一個特殊的狀態, 提示你如何解決合并中出現的沖突。
[centos@bogon gittest]$ git merge br1 Auto-merging file1 CONFLICT (content): Merge conflict in file1 Automatic merge failed; fix conflicts and then commit the result. [centos@bogon gittest]$ git status # On branch master # Unmerged paths: # (use "git add/rm <file>..." as appropriate to mark resolution) # # both modified: file1 # no changes added to commit (use "git add" and/or "git commit -a")
有沖突(conflicts)的文件會保存在索引中,在commit之前要解決沖突,解決沖突的方式就是編輯沖突文件,重新commit。
撤銷合并
#如果合并分支后又后悔了,可以撤銷合并 [centos@bogon gittest]$ git reset --hard HEAD HEAD is now at 2fa716d file1 master [centos@bogon gittest]$ git status # On branch master nothing to commit (working directory clean) #如果已經把合并后的代碼提交了,可以執行 [centos@bogon gittest]$ git reset --hard ORIG_HEAD HEAD is now at 2fa716d file1 master
log記錄
$ git log v2.5.. # commits since (not reachable from) v2.5 $ git log test..master # commits reachable from master but not test $ git log master..test # commits reachable from test but not master $ git log master...test # commits reachable from either test or # master, but not both $ git log --since="2 weeks ago" # commits from the last 2 weeks $ git log Makefile # commits that modify Makefile $ git log fs/ # commits that modify any file under fs/ $ git log -S'foo()\' # commits that add or remove any file data # matching the string 'foo()' $ git log --no-merges # dont show merge commits
示例:
#查看當前分支log [centos@bogon gittest]$ git log commit 2fa716df1d841ac2347cd9b6d371cfdf71682dfe Author: wubaoguo <wustrive_2008@126.com> Date: Wed Jan 6 23:53:20 2016 +0800 file1 master commit f9da1748bddb6cfcc0f492f60328abcd54f97663 Author: wubaoguo <wustrive_2008@126.com> Date: Wed Jan 6 23:44:08 2016 +0800 br2 commit da5068b35246dc26b77105a6dc6c2aa6e430fcad Author: wubaoguo <wustrive_2008@126.com> Date: Wed Jan 6 23:38:39 2016 +0800 init #查看詳細變動 [centos@bogon gittest]$ git log --stat commit 2fa716df1d841ac2347cd9b6d371cfdf71682dfe Author: wubaoguo <wustrive_2008@126.com> Date: Wed Jan 6 23:53:20 2016 +0800 file1 master file1 | 1 + 1 files changed, 1 insertions(+), 0 deletions(-) commit f9da1748bddb6cfcc0f492f60328abcd54f97663 Author: wubaoguo <wustrive_2008@126.com> Date: Wed Jan 6 23:44:08 2016 +0800 br2 br2-file | 1 + 1 files changed, 1 insertions(+), 0 deletions(-) commit da5068b35246dc26b77105a6dc6c2aa6e430fcad Author: wubaoguo <wustrive_2008@126.com> Date: Wed Jan 6 23:38:39 2016 +0800 init #格式化log輸出結果 [centos@bogon gittest]$ git log --pretty=oneline 2fa716df1d841ac2347cd9b6d371cfdf71682dfe file1 master f9da1748bddb6cfcc0f492f60328abcd54f97663 br2 da5068b35246dc26b77105a6dc6c2aa6e430fcad init #更友好的格式化,歷史多了效果明顯 [centos@bogon gittest]$ git log --pretty=format:'%h : %s' --graph * 2fa716d : file1 master * f9da174 : br2 * da5068b : init
比較差異DIFF
#比較分支間的差異 [centos@bogon gittest]$ git diff master..br1 diff --git a/file1 b/file1 index 3325d54..76e65f5 100644 --- a/file1 +++ b/file1 @@ -1 +1 @@ -file1 master +file1 br1 #工作目錄與暫存區(staged)差異 [centos@bogon gittest]$ git diff diff --git a/file2 b/file2 index e69de29..35d5537 100644 --- a/file2 +++ b/file2 @@ -0,0 +1,2 @@ +diff 的使用 + #暫存區與上次提交之間的差異 [centos@bogon gittest]$ git add . [centos@bogon gittest]$ git diff --cached diff --git a/file2 b/file2 index e69de29..35d5537 100644 --- a/file2 +++ b/file2 @@ -0,0 +1,2 @@ +diff 的使用 + #工作目錄與上次提交之間的差異 [centos@bogon gittest]$ git diff HEAD diff --git a/file2 b/file2 index e69de29..35d5537 100644 --- a/file2 +++ b/file2 @@ -0,0 +1,2 @@ +diff 的使用 + diff --git a/file3 b/file3 index e69de29..1bf6afb 100644 --- a/file3 +++ b/file3 @@ -0,0 +1 @@ +還沒commit 工作目錄又改動了
附錄 .gitconfig文件配置參考
[user] name = zhangsan email = zhangsan@gmail.com [color] branch = auto diff = auto status = auto ui = auto [core] quotepath=false edit = vim autocrlf = true filemode = false [i18n] commitencoding = UTF-8 [gui] encoding = utf-8 [alias] stage = add unstage = reset HEAD hb = merge --no-ff rmv = remote -v ci = commit cia = commit --amend co = checkout br = branch st = status dc = diff --cached dw = diff --word-diff aa = add -A rmall = !git ls-files --deleted | xargs git rm ll = log --pretty=format:"%C(yellow)%h%Cred%d%Creset\\ %cn\\ %Cblue%cr%Creset\\ %Cgreen%s%Creset" --decorate --numstat lg = log --pretty=format:"%C(yellow)%h%Cred%d%Creset\\ %cn\\ %Cblue%cr%Creset\\ %Cgreen%s%Creset" --decorate lt = log --pretty=format:"%C(yellow)%h%Cred%d%Creset\\ %cn\\ %Cblue%cr%Creset\\ %Cgreen%s%Creset" --graph [receive] denyCurrentBranch = ignore
更多配置文件參考: https://github.com/wustrive2008/conf-file
參考
《Git Community Book》
捐贈
如何覺得本文章對你有幫助,歡迎捐贈
來自: http://wustrive2008.github.io/2016/01/06/版本控制/Git全解析之先用起來/