分佈式版本控制系統 Git | 二

分佈式版本控制系統 Git | 二


關於 Git 的一些基本內容,在下面的文章中作了簡單的介紹及一些命令的使用。git

分佈式版本控制系統 Git微信

今天的篇幅將延續上一篇的內容繼續講解 Git 基礎這一塊的內容。編輯器

Git 基礎(續)


記錄更新到倉庫

當手上有真實項目的 Git 倉庫時,從倉庫取出全部文件的拷貝。通常流程都是修改文件,完成目標,提交更新到倉庫。分佈式

這個過程當中,工做目錄下的文件只有兩種狀態:已跟蹤或未跟蹤。.net

已跟蹤的文件指被歸入版本控制的文件,上一次的快照中有它們的記錄,工做一段時間後,它們的狀態多是未修改,已修改或已放入暫存區。版本控制

除此以外,工做目錄中除已跟蹤的文件之外的全部其餘文件都屬於未跟蹤文件,它們即不存在於上次的快照,也沒有放入暫存區。rest

檢查當前文件的狀態

使用 git status 命令能夠查看文件的狀態。當克隆倉庫未作任何修改時,使用這個命令將會看到如下的結果:code

$ git status
On branch master
nothing to commit, working directory clean

這裏表示工做目錄是乾淨的。也便是已跟蹤的文件在上次提交後都不曾修改過。blog

這些信息也表示沒有出現任何未跟蹤的新文件,不然這裏會有所顯示。get

On branch master 這個信息表面當前的分支是 master,這是默認分支。至於 Git 分支 會在後面另開篇幅詳細討論。

嘗試建立新文件 readme.txt,使用 git status 命令查看狀態:

$ echo 'Something you need to know about git' > readme.txt

$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        readme.txt

Untracked files 表示 readme.txt 屬於未跟蹤文件,Git 並不會自動將其歸入跟蹤範圍,因此須要對新文件進行跟蹤管理。

跟蹤新文件

上一篇文章講了 git add 命令,該命令的做用便是用來跟蹤新文件,如今嘗試使用跟蹤 readme.txt 文件:

$ git add readme.txt

這時使用 git status 命令,能夠查看 readme.txt 文件的狀態,此時已經處於暫存狀態:

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   readme.txt

這部分的內容在分佈式版本控制系統 Git 也作了簡單的介紹,這裏就當作熟悉命令的使用。

Changes to be committed:,這行下面的就表示是已暫存的狀態。若是此時提交的話,那這個文件在 git add 時的版本將會留在歷史記錄裏。

暫存已修改文件

當已經追蹤的文件被修改時,使用 git status 命令查看狀態:

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   readme.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   about_git.txt

在這些信息中,能夠看到 about_git.txt 文件發生了改變,可是並無放入暫存區中。這裏也提示可使用 git add 命令暫存這次更新。

git add 可以跟蹤新文件,也可以將已跟蹤的文件放到暫存區。能夠將這個命令理解爲「添加內容到下一次提交中」。

使用 git addabout_git.txt 文件放到暫存區,而後用 git status 查看狀態:

$ git add about_git.txt

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   about_git.txt
        new file:   readme.txt

此時新文件以及修改後的追蹤文件,都已經暫存,等下次提交時,將一併更新到倉庫中。

若這時修改 about_git.txt 文件的內容,編輯保存後。想對兩個文件進行提交時,使用 git status 先查看狀態:

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   about_git.txt
        new file:   readme.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   about_git.txt

這裏會發現,about_git.txt 文件同時出如今暫存區和非暫存區。這裏出現這樣結果的緣由,是由於 Git 只暫存了運行 git add 命令時的版本。這個時候如果進行提交,about_git.txt 的版本也會是執行 git add 命令時的版本,而不是工做區當前的版本。因此當運行了 git add 後,又對文件進行了修改,須要從新使用 git add 把最新的版本暫存起來:

$ git add about_git.txt

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   about_git.txt
        new file:   readme.txt
查看已暫存和未暫存的修改

git status 命令輸出的結果是指工做區的狀態,至於要知道具體修改了什麼地方,可使用 git diff

git diff 不一樣於 git status 以列出文件名的形式輸出結果,而是經過文件補丁的格式顯示具體哪行發生了改變。

假如再次修改 readme.txt 進行暫存,修改 about_git.txt 文件不進行暫存,使用 git status 命令查看效果:

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   readme.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   about_git.txt

要查看未暫存部分究竟更新了哪些內容,直接使用不加參數的 git diff

$ git diff
diff --git a/about_git.txt b/about_git.txt
index 8596ae8..39f1422 100644
--- a/about_git.txt
+++ b/about_git.txt
@@ -4,4 +4,4 @@ Change something

 Add something

-Something different here
+Something changed here

這個命令比較的是工做區中,當前文件和暫存區域快照之間的差別。也就是修改以後還未暫存起來的變化。

如果須要查看已暫存待提交的內容變化,使用 git diff --staged 命令。這個命令比較的是已暫存文件與最後一次提交的文件的差別。

$ git diff --staged
diff --git a/readme.txt b/readme.txt
index 35b128b..a68a8b2 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1 +1,3 @@
 Add something here
+
+Change something here
注意, git diff 自己只顯示未暫存的改動,而不是自上次提交以來作的全部改動。因此有時候一次暫存全部更新的文件後,使用 git diff 會發現什麼都沒有,就是這個緣由。

下面再嘗試前面說起的一種狀況,對 about_git.txt 暫存後再次編輯的狀況。

先使用 git status 查看結果:

$ git add about_git.txt
$ echo "# test line" >> about_git.txt
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   about_git.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   about_git.txt

再使用 git diff 查看未暫存的修改:

$ git diff
diff --git a/about_git.txt b/about_git.txt
index 39f1422..5962c89 100644
--- a/about_git.txt
+++ b/about_git.txt
@@ -5,3 +5,4 @@ Change something
 Add something

 Something changed here
+# test line

而後使用 git diff --staged 查看已暫存修改的部分:

$ git diff --staged
diff --git a/about_git.txt b/about_git.txt
index 8596ae8..39f1422 100644
--- a/about_git.txt
+++ b/about_git.txt
@@ -4,4 +4,4 @@ Change something

 Add something

-Something different here
+Something changed here
提交更新

這部份內容,一樣在分佈式版本控制系統 Git 作了介紹。

可使用 git commit 打開編輯器輸入更新信息進行提交更新。

也可使用 git commit -m <message>message 處添加更新信息進行提交更新。

以下示例:

$ git commit -m "Fix issue 007"
[master dd6c67d] Fix issue 007
 2 files changed, 4 insertions(+), 1 deletion(-)

這裏要注意,提交時,記錄的是放在暫存區域的快照。而其餘未暫存的文件的仍然保持已經修改的狀態,這部分的內容,能夠在下次提交歸入版本管理。

其實每次提交都是對項目作一次快照,後面也能夠回到這個狀態,或者進行比較。

總結


  1. 使用 git status 能夠查看文件的狀態;
  2. 使用 git add 可以跟蹤新文件,也可以暫存已修改文件(即已跟蹤文件修改);
  3. 使用不帶參數的 git diff 可以查看未暫存文件的修改;
  4. 使用帶參數的 git diff --staged 可以查看已暫存文件的修改部分;
  5. 使用 git commit -m <message> 提交更新。

以上就是本篇的主要內容

未完待續

歡迎關注微信公衆號《書所集錄》
相關文章
相關標籤/搜索