西西軟件園多重安全檢測下載網(wǎng)站、值得信賴的軟件下載站!
軟件
軟件
文章
搜索

首頁編程開發(fā)其它知識 → Linux系統(tǒng)中g(shù)it 入門教程

Linux系統(tǒng)中g(shù)it 入門教程

相關(guān)軟件相關(guān)文章發(fā)表評論 來源:西西整理時間:2014/3/9 9:15:04字體大。A-A+

作者:西西點擊:49次評論:1次標(biāo)簽: git

  • 類型:文件處理大小:204KB語言:中文 評分:5.0
  • 標(biāo)簽:
立即下載

Git 起源

同生活中的許多偉大事件一樣,Git 誕生于一個極富紛爭大舉創(chuàng)新的年代。Linux 內(nèi)核開源項目有著為數(shù)眾廣的參與者。絕大多數(shù)的 Linux 內(nèi)核維護工作都花在了提交補丁和保存歸檔的繁瑣事務(wù)上(1991-2002年間)。到 2002 年,整個項目組開始啟用分布式版本控制系統(tǒng) BitKeeper 來管理和維護代碼。到了 2005 年,開發(fā) BitKeeper 的商業(yè)公司同 Linux 內(nèi)核開源社區(qū)的合作關(guān)系結(jié)束,他們收回了免費使用 BitKeeper 的權(quán)力。這就迫使 Linux 開源社區(qū)(特別是 Linux 的締造者 Linus Torvalds )不得不吸取教訓(xùn),只有開發(fā)一套屬于自己的版本控制系統(tǒng)才不至于重蹈覆轍。他們對新的系統(tǒng)制訂了若干目標(biāo):

Git 和其他版本控制系統(tǒng)的主要差別在于,Git 只關(guān)心文件數(shù)據(jù)的整體是否發(fā)生變化,而大多數(shù)其他系統(tǒng)則只關(guān)心文件內(nèi)容的具體差異。這類系統(tǒng)(CVS,Subversion,Perforce,Bazaar 等等)每次記錄有哪些文件作了更新,以及都更新了哪些行的什么內(nèi)容.

Git 并不保存這些前后變化的差異數(shù)據(jù)。實際上,Git 更像是把變化的文件作快照后,記錄在一個微型的文件系統(tǒng)中。每次提交更新時,它會縱覽一遍所有文件的指紋信息并對文件作一快照,然后保存一個指向這次快照的索引。為提高性能,若文件沒有變化,Git 不會再次保存,而只對上次保存的快照作一鏈接.這是 Git 同其他系統(tǒng)的重要區(qū)別。它完全顛覆了傳統(tǒng)版本控制的套路,并對各個環(huán)節(jié)的實現(xiàn)方式作了新的設(shè)計。Git 更像是個小型的文件系統(tǒng)

1、創(chuàng)建新倉庫  

 git init  

 touch  test.txt

 git add --a

 git commit -m "fist commit"

 初始化新倉庫,在當(dāng)前目錄下由一個.git的目錄,所有g(shù)it需要的數(shù)據(jù)和資源都放在這個目錄中,在當(dāng)面目錄下添加文件后,需要通過git add 添加到文件追蹤管理(添加到暫存區(qū),數(shù)據(jù)存放在.git/index 目錄索引,數(shù)據(jù)內(nèi)部保存在.git/objects 中), git commit -m "提交說明備注" 提交的信息會提交到數(shù)據(jù)倉庫,數(shù)據(jù)提交到正式倉庫,具體保存在.git/objects 中,如以上提交會包含一個commit,tree ,blob 對象。

2、從現(xiàn)有倉庫克隆

git clone  url

git clone git@github.com:torvalds/linux.git 

如從gitHub上克隆一份linux的源碼,不僅是克隆最新版本的源碼,還克隆了所有數(shù)據(jù)倉庫的歷史版本,每個文件的每一個版本,這個時候及時服務(wù)器github 發(fā)生故障,可以用本地數(shù)據(jù)倉庫重建服務(wù)器上的倉庫?梢曰貜(fù)到從服務(wù)器克隆或最后更一次從服務(wù)器拉去的狀態(tài)。在.git 目錄中,已經(jīng)保存了所有版本記錄,本地文件夾即工作目錄的所有文件刪除了,然后從中取出最新版本的文件拷貝。

3、檢查文件更新狀態(tài)

    要求確定當(dāng)前工作區(qū)和暫存區(qū)文件的狀態(tài),可以通過git status 命令。在工作區(qū)和暫存區(qū)的目錄狀態(tài)可以查看。

  git status

 On branch master  nothing to commit, working directory clean

當(dāng)前在默認(rèn)master 分支,當(dāng)前工作目錄和暫存區(qū)沒有任何跟蹤的文件,也沒有任何文件提交后更改,也沒有新增加,未被跟蹤的文件。

notepad test.txt

notepad t.txt

修改test.txt文件,新添加一個t.txt 文件,查看當(dāng)前文件狀態(tài)。

$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working di
#
#       modified:   test.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       t.txt
no changes added to commit (use "git add" and/or "git commit -a")

新增加的文件t.txt 在未跟蹤文件范圍Untracked files 范圍,需要通過git add 把改文件添加的暫存區(qū),歸入的版本跟蹤管理。

4、 添加文件到暫存區(qū)

 

$ git add .
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   h.txt
#       new file:  test.txt

git add . 把當(dāng)前所有目錄文件所有新文件和修改文件添加到暫存區(qū),如果test.txt 文件提示是 Changes not staged for commit ,說明此跟蹤文件已經(jīng)發(fā)生修改,但是還未添加到暫存區(qū),把修改的文件通過git add 命令添加到暫存區(qū)后。提示Changes to be committed. 文件已經(jīng)暫存,隨時可以提交到倉庫 。h.txt 新添加文件從未跟蹤狀態(tài)Untracked files ,通過git add命令添加到暫存區(qū),已加入跟蹤文件的范圍。

5、版本提交

$ git commit -m "this is test commit"
[master d4a498a] this is test commit
    git commit --amend --reset-author

2 files changed, 3 insertions(+)
create mode 100644 t.txt

通過git commit -m "xxx" 將當(dāng)前暫存區(qū)的內(nèi)容提交到倉庫,本次commit 提交文件是在默認(rèn)master分支,提交commit 對象存放在.git/objects 的d4/1498a... 的文件中,該文件指向一個樹tree對象。

6、查看當(dāng)前提交日志記錄

 

$ git log
commit d4a498a197c24421acee5c5ff96cfbc7e5c3be9e
Author: andy<andy@gmail.com>
Date:   Sat Mar 8 14:23:37 2014 +0800

    this is test commit

commit 80071e48614361dc282473d6482e3faa9fec17d9
Author:andy<andy@gmail.com>
Date:   Sat Mar 8 13:35:13 2014 +0800

    1st commit

git log 命令查看當(dāng)前版本

7、文件差異比較

 

 工作區(qū)和暫存區(qū)文件比較用git diff 命令,暫存區(qū)和最近一天提交版本之間的差異,可以用git diff --cached/staged. 如下:

  目前在test.txt 文件只有1111111 文件內(nèi)容,我在文件test.txt中添加22222222等內(nèi)容,比較當(dāng)前暫存區(qū)和工作文件差異

$ notepad test.txt

$ git diff
diff --git a/test.txt b/test.txt
index 0147537..f33d264 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,4 @@
11111111111111
+22222222222222

$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working d
#
#       modified:   test.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

$ git diff --staged

可以發(fā)現(xiàn)工作區(qū)比暫存區(qū)test.txt文件多增加了22222222222222 內(nèi)容。暫存區(qū)和數(shù)據(jù)倉庫內(nèi)容是完全相同的。同時看看當(dāng)前工作區(qū)狀態(tài),F(xiàn)在我們吧剛剛修改的內(nèi)容添加到暫存區(qū),同時比較暫存區(qū)和數(shù)據(jù)倉庫文件差異。

$ git add test.txt

$ git diff

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   test.txt
#

把工作區(qū)修改的內(nèi)容更新到暫存區(qū)后,可以看出此時暫存區(qū)和工作區(qū)文件完全相同。狀態(tài)是是已暫存,帶提交狀態(tài)。與此同時,我們可以比較暫存區(qū)和數(shù)據(jù)殘酷之間的差異和比較。

$ git diff --staged
diff --git a/test.txt b/test.txt
index 0147537..f33d264 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,4 @@
11111111111111
+22222222222222

我們可以很清楚的看到當(dāng)前暫存區(qū)和數(shù)據(jù)倉庫版本比較。暫存區(qū)test.txt 內(nèi)容比最近一次提交內(nèi)容多22222222222222 一行數(shù)據(jù)。提交數(shù)據(jù)到數(shù)據(jù)倉庫。我們現(xiàn)在可以把工作區(qū)目錄和數(shù)據(jù)倉庫比較,看看test.txt 直接的文件內(nèi)容差異。

$ git diff head
diff --git a/test.txt b/test.txt
index 0147537..f33d264 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,4 @@
11111111111111
+22222222222222

可以很立即看出工作區(qū)文件內(nèi)容比較倉庫有新修改的內(nèi)容。此時我們提交更新所有文件都沒有差異了?纯次募町。

$ git commit -m "test git diff "
[master fc0166f] test git diff
Committer: andy<andy@gmail.com>

    git commit --amend --reset-author

1 file changed, 1 insertion(+)

$ git diff
$ git diff --staged
$ git diff head

8、文件刪除和移動

所有的工作區(qū)rm xxx刪除后,可以直接從數(shù)據(jù)倉庫獲取最近一次提交版本的內(nèi)容 git rm xxx。直接從數(shù)據(jù)倉庫刪除此文件內(nèi)容。

$ ls
h.txt  test.txt


$ rm h.txt

$ ls
test.txt

$ git diff
diff --git a/h.txt b/h.txt
deleted file mode 100644
index 456f979..0000000
--- a/h.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-welcome to here
-very good

可以通過文某比較當(dāng)前的工作目錄h.txt 文件已經(jīng)被刪除了,工作區(qū)目錄和暫存區(qū)比較文件差異也可以返現(xiàn)文件刪除模式。接下來刪除暫存區(qū)目錄

$ git diff --staged

$ git rm h.txt
rm 'h.txt'


$ git diff --staged
diff --git a/h.txt b/h.txt
deleted file mode 100644
index 456f979..0000000
--- a/h.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-welcome to here
-very good
-

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       deleted:    h.txt
#

通過刪除暫存區(qū)文件前后可以比較出文件的差異。此時文件也不在跟蹤狀態(tài)。還有我們僅僅希望刪除暫存區(qū)的內(nèi)容,不刪除工作區(qū)的內(nèi)容

git rm --cached -f h.txt  的方式來實現(xiàn),特別是針對工作區(qū)有修改,要刪除暫存區(qū)內(nèi)容。

$ git reset --hard
HEAD is now at fc0166f test git diff

$ ls
h.txt  test.txt

$ git rm --cached h.txt
rm 'h.txt'

$ git diff

$ git diff head
diff --git a/h.txt b/h.txt
deleted file mode 100644
index 456f979..0000000
--- a/h.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-welcome to here
-very good
-
$ git diff -cached
error: invalid option: -cached

$ git diff --cached h.txt
diff --git a/h.txt b/h.txt
deleted file mode 100644
index 456f979..0000000
--- a/h.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-welcome to here
-very good

移動

git 移動文件操作是是相對先移動復(fù)制一個新文件,刪除原文件,添加新文件到跟蹤范圍。

$ ls
h.txt  test.txt

$ git mv h.txt d.txt   結(jié)果是等同于,相當(dāng)于以下三條命令

$ ls
d.txt  test.txt

$ mv d.txt to.txt

$ git rm d.txt
rm 'd.txt'

$ git add to.txt

9、查看提交歷史

$ git log
commit fc0166f53a45cfc4c17079e5e3454fb7e3136cb6
Author: andy<andy@gmail.com>
Date:   Sat Mar 8 15:52:10 2014 +0800

    test git diff

commit d6eab3a38aee0b25ac395899c82edd48723a2ea9

Author: andy<andy@gmail.com>

Date:   Sat Mar 8 15:36:53 2014 +0800

    enforce commit'

commit 85ec024140442df6db8625a07c2ee7369cceb704

Author: andy<andy@gmail.com>

Date:   Sat Mar 8 15:35:44 2014 +0800

    com 3

git log  查看提交歷史,git log -p -n  最近n 次提交的內(nèi)能和差異。查看歷史記錄統(tǒng)計信息 git log --stat,查看提交的歷史記錄格式可自由選擇。

$ git log --pretty=format:"%h - %an, %ar : %s"
fc0166f - yyf, 48 minutes ago : test git diff
d6eab3a - yyf, 63 minutes ago : enforce commit'
85ec024 - yyf, 65 minutes ago : com 3
d4a498a - unknown, 2 hours ago : this is test commit
80071e4 - unknown, 3 hours ago : 1st commit

列出了常用的格式占位符寫法及其代表的意義。

選項   說明
%H  提交對象(commit)的完整哈希字串
%h  提交對象的簡短哈希字串
%T  樹對象(tree)的完整哈希字串
%t  樹對象的簡短哈希字串
%P  父對象(parent)的完整哈希字串
%p  父對象的簡短哈希字串
%an 作者(author)的名字
%ae 作者的電子郵件地址
%ad 作者修訂日期(可以用 -date= 選項定制格式)
%ar 作者修訂日期,按多久以前的方式顯示
%cn 提交者(committer)的名字
%ce 提交者的電子郵件地址
%cd 提交日期
%cr 提交日期,按多久以前的方式顯示
%s  提交說明

$ git log --pretty=format:"%h %s" --graph
* 78d907a dev branch commit
* fc0166f test git diff
* d6eab3a enforce commit'
* 85ec024 com 3
* d4a498a this is test commit
* 80071e4 1st commit

選項 說明
-p 按補丁格式顯示每個更新之間的差異。
--stat 顯示每次更新的文件修改統(tǒng)計信息。
--shortstat 只顯示 --stat 中最后的行數(shù)修改添加移除統(tǒng)計。
--name-only 僅在提交信息后顯示已修改的文件清單。
--name-status 顯示新增、修改、刪除的文件清單。
--abbrev-commit 僅顯示 SHA-1 的前幾個字符,而非所有的 40 個字符。
--relative-date 使用較短的相對時間顯示(比如,“2 weeks ago”)。
--graph 顯示 ASCII 圖形表示的分支合并歷史。
--pretty 使用其他格式顯示歷史提交信息?捎玫倪x項包括 oneline,short,full,fuller 和 format(后跟指定格式)

10、撤銷操作

可以修改最后一次提交的內(nèi)容,當(dāng)你發(fā)現(xiàn)最近一次提交內(nèi)容不正確時候,可以通過  git commit --amend 修改

$ git commit --amend -m "modify commit"
[master c660522] modify commit

$ git log --oneline
c660522 modify commit
fc0166f test git diff
d6eab3a enforce commit'
85ec024 com 3
d4a498a this is test commit
80071e4 1st commit

11、取消文件修改

git reset    | git reset head    將head指向的目錄樹重置的暫存區(qū)

git  reset  --soft  head^          工作區(qū)和暫存區(qū)不變,但是引用向前回退一次,當(dāng)對最新提交不滿意的時候,撤銷最新提交以便更改

git commit -e -F  .git/COMMIT_EDITMSG    以上兩個命令相當(dāng)于git commit --amend

git reset  head^

git reset --mixed  head^   暫存區(qū)和引用回退到上一次提交之前

git reset --hard head^      引用 工作區(qū) 暫存區(qū) 徹底消除


    相關(guān)評論

    閱讀本文后您有什么感想? 已有人給出評價!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過難過
    • 5 囧
    • 3 圍觀圍觀
    • 2 無聊無聊

    熱門評論

    最新評論

    發(fā)表評論 查看所有評論(1)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字?jǐn)?shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)