Git全解析之用起來先

先安裝Git環(huán)境

下載安裝:http://git-scm.com/download/

配置

配置user與email,用來標(biāo)識(shí)用戶

$ git config --global user.name "wustrive"
$ git config --global user.email "wustrive2008@gmail.com"

也可以直接編輯配置文件,配置文件默認(rèn)路徑在用戶目錄下的.gitconfig文件中,結(jié)構(gòu)是:

[user]
    name = wustrive
    email = wustrive2008@gmail.com

簡(jiǎn)單了解Git

Git是一個(gè)快速的分布式版本控制系統(tǒng)

Git對(duì)象模型 SHA

所有用來表示項(xiàng)目歷史信息的文件是通過一個(gè)40個(gè)字符“對(duì)象名”來索引的。每一個(gè)“對(duì)象名”都是對(duì)“對(duì)象”內(nèi)容做SHAI哈希計(jì)算來的。這個(gè)對(duì)象名是全局唯一的,不同的對(duì)象生成的“對(duì)象名”不同。

Git中四種類型的對(duì)象:"blob","tree","commit"和"tag"。每個(gè)對(duì)象包括三個(gè)部分:類型,大小和內(nèi)容。

  • blob用來存儲(chǔ)文件數(shù)據(jù)
  • tree有點(diǎn)像一個(gè)目錄,用來管理一些blob與tree
  • commit,一個(gè)commit只指向一個(gè)tree,用來標(biāo)記項(xiàng)目某一個(gè)特定時(shí)間點(diǎn)的狀態(tài),即一次提交
  • tag,一個(gè)tag用來標(biāo)記一個(gè)commit

Git目錄與工作目錄

git目錄是為你的項(xiàng)目存儲(chǔ)所有歷史和元信息的目錄,包括所有對(duì)象,這些對(duì)象指向不同的分支,每個(gè)項(xiàng)目只能有一個(gè)Git目錄,這個(gè)叫'.git'的目錄一般在項(xiàng)目的根目錄下,
這個(gè)目錄下的重要文件有:

.
|-- FETCH_HEAD #指向著目前已經(jīng)從遠(yuǎn)程倉庫取下來的分支的末端版本。
|-- HEAD #這個(gè)git項(xiàng)目當(dāng)前處在哪個(gè)分支里
|-- ORIG_HEAD #HEAD指針的前一個(gè)狀態(tài)
|-- branches/ #項(xiàng)目的所有分支
|-- config/ #項(xiàng)目的配置信息,git config命令會(huì)改動(dòng)它
|-- description #項(xiàng)目的描述信息
|-- hooks/ #系統(tǒng)默認(rèn)鉤子腳本目錄
|-- index #索引文件
|-- info/ #包含倉庫的一些信息
|-- logs/ #各個(gè)refs的歷史信息
|-- objects/ #Git本地倉庫的所有對(duì)象 (commits, trees, blobs, tags)
|-- packed-refs #運(yùn)行 git gc, refs 下的所有文件都會(huì)消失。Git 會(huì)將這些文件挪到 .git/packed-refs 文件中去以提高效率
|-- refs/ #標(biāo)識(shí)你項(xiàng)目里的每個(gè)分支指向了哪個(gè)提交(commit)

工作目錄就是你的項(xiàng)目源代碼目錄,即是你簽出(checkout)用來編輯的文件,當(dāng)在不同的分支間切換時(shí),工作目錄里的內(nèi)容會(huì)隨之替換或刪除,所有的操作歷史都保存在Git目錄中,工作目錄是用來臨時(shí)保存checkout文件的地方。

可以開始使用了

獲取倉庫

  • 創(chuàng)建倉庫,在工作目錄下執(zhí)行
git init .
  • clone一個(gè)倉庫
#通過http(s)協(xié)議
git clone https://github.com/git/git.git
#通過ssh協(xié)議
git clone git@github.com:git/git.git

提示:創(chuàng)建和clone后默認(rèn)的分支是master,默認(rèn)的repository引用名稱origin

正常的工作流程

  1. 修改文件,將它們更新的內(nèi)容添加到索引中
git add file1 file2 file3
#也可以通過git add . 來添加所有變動(dòng)到暫存區(qū)
git add .
  1. 查看當(dāng)狀態(tài)
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: file1
# modified: file2
# modified: file3
#
  1. 提交commit
#執(zhí)行后會(huì)進(jìn)入編輯器進(jìn)行注釋編輯
$ git commit

#如果注釋很短也可以使用
$ git commit -m "注釋內(nèi)容"

#如果只是修改了文件,沒有添加新的文件,可以省略git add
$ git commit -am "注釋內(nèi)容"

分支與合并

基本操作命令
#查看本地分支 *代表當(dāng)前所在分支
[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

#切換并合并分支,以當(dāng)前分支為基礎(chǔ)新建分支
[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

#合并分支,將其他分支合并到當(dāng)前分支
[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   

#刪除分支,刪除已經(jīng)被合并過的分支,安全刪除分支
[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

#強(qiáng)制刪除分支
[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

分支沖突

如果執(zhí)行自動(dòng)合并沒有成功的話,git會(huì)在索引和工作樹里設(shè)置一個(gè)特殊的狀態(tài), 提示你如何解決合并中出現(xiàn)的沖突。

[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)的文件會(huì)保存在索引中,在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)

#如果已經(jīng)把合并后的代碼提交了,可以執(zhí)行
[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

示例:

#查看當(dāng)前分支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

#查看詳細(xì)變動(dòng)
[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輸出結(jié)果
[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

#工作目錄與暫存區(qū)(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 的使用
+

#暫存區(qū)與上次提交之間的差異
[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  工作目錄又改動(dòng)了

附錄 .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》

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • git 使用筆記 git原理: 文件(blob)對(duì)象,樹(tree)對(duì)象,提交(commit)對(duì)象 tree對(duì)象 ...
    神刀閱讀 3,848評(píng)論 0 10
  • 【書 目】《如何打造你的獨(dú)特觀點(diǎn)》 【課程領(lǐng)拆】崔莉莎老師 【上課時(shí)間】2017年7月25日 【總結(jié)】 1....
    QueenaLuo閱讀 311評(píng)論 0 1
  • 上下午抽空練功一遍,小妞持繼不舒服,假期忙亂中……
    菜菜2017閱讀 202評(píng)論 0 2
  • 寂天菩薩說:“問題若有辦法解決,則不需要擔(dān)心;問題若無辦法解決,則擔(dān)心也沒有用”。 這句話告訴我們,要用平和的心態(tài)...
    六爸啦啦啦閱讀 335評(píng)論 0 0
  • 一個(gè)高績(jī)效的團(tuán)隊(duì)額取決于三個(gè)方面,一是人、二是環(huán)境,第三個(gè)是關(guān)系,每個(gè)人都有自己的特長(zhǎng),都有自己的潛能,那當(dāng)然了,...
    獨(dú)立視角閱讀 406評(píng)論 0 0

友情鏈接更多精彩內(nèi)容