日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Git 索引文件(index file)

發布時間:2024/10/14 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Git 索引文件(index file) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這次重點講解索引文件(index file)的作用。

我們在提交工作時,使用最多的命令就是 git commit -a 了,但是這個將提交你所做的所有工作。其實,如果你了解 commit 的工作機制,你會知道我們可以自定義提交哪些部分到哪些工作樹中,其實自由度很大的。

一、diff 到底在和誰對比

還記得之前我們建立的 test-project 工作目錄么。我們繼續在這個目錄下演示講解。

[rocrocket@wupengchong test-project]$ echo “hello world,again”>>file.txt

這次,我們不急著執行 commit 命令,而是先用 git diff 看看差別情況:

[rocrocket@wupengchong test-project]$ git diff diff –git a/file.txt b/file.txt index 60be00d..a559610 100644 — a/file.txt +++ b/file.txt @@ -1 +1,2 @@ Hi,rocrocket! +hello world,again

好了,我們可以看到 git? 報告了我們剛才所做的修改。下面我們來 add 一下,然后再 git diff,看看 diff 有什么變化呢:

[rocrocket@wupengchong test-project]$ git add file.txt [rocrocket@wupengchong test-project]$ git diff [rocrocket@wupengchong test-project]$

大家可以看到在 add 之后的 git diff 的輸出竟然為空了,但是此時我們尚未執行 commit 啊。如果這個時候你執行 git diff HEAD,你仍然會看到修改報告:

[rocrocket@wupengchong test-project]$ git diff HEAD diff –git a/file.txt b/file.txt index 60be00d..a559610 100644 — a/file.txt +++ b/file.txt @@ -1 +1,2 @@ Hi,rocrocket! +hello world,again

這就說明了一個問題:git diff 不是在和 HEAD 比,而是另外一個“神秘”內容在比,而這個“神秘”內容就是“索引文件”!

二、索引文件

索引文件(index file)就是 .git/index 文件,它是二進制形式的文件。我們可以用 ls-files 來檢查它的內容。

[rocrocket@wupengchong test-project]$ git ls-files –stage //一定要記住,此命令是用于查看index file的!! 100644 a55961026a22bdd4e938dcc90a4a83823a81f776 0 file.txt [rocrocket@wupengchong test-project]$ git cat-file -t a5596 blob [rocrocket@wupengchong test-project]$ git cat-file blob a5596 Hi,rocrocket! hello world,again

很明顯,我們可以看到其內容已經是改進后的代碼了,怪不得 git-diff 會輸出空呢!

我們的結論就是 git add 的作用就是創建一個 blob 文件來記錄最新的修改代碼,并且在 index file 里添加一個到此 blob 的鏈接。

如果在 git-diff 后面加上參數 HEAD,則 git-diff 會顯示當前工作目錄和最近一次提交之間的代碼區別。

[rocrocket@wupengchong test-project]$ echo ‘again?’>>file.txt [rocrocket@wupengchong test-project]$ git diff HEAD diff –git a/file.txt b/file.txt index 60be00d..dfb67dc 100644 — a/file.txt +++ b/file.txt @@ -1 +1,3 @@ Hi,rocrocket! +hello world,again +again?

如果使用參數 –cached,則會比較 index file 和最近一次提交之間的代碼區別。

[rocrocket@wupengchong test-project]$ git diff –cached diff –git a/file.txt b/file.txt index 60be00d..a559610 100644 — a/file.txt +++ b/file.txt @@ -1 +1,2 @@ Hi,rocrocket! +hello world,again

?三、小結

按我的認識,更清楚且通俗的解釋就是:git 維護的代碼分成三部分,“當前工作目錄”<------>“index file”<------>“git倉庫”。

  • git add 會將“當前工作目錄”的改變寫到“index file”。
  • git commit 會將 index file 中的改變寫到 git 倉庫。
  • “commit -a”則會直接將“當前工作目錄”的改動同時寫到“index file”和“git倉庫”。
  • git diff 總會拿 git 倉庫來作為比較對象之一。如果 git diff 的參數是 HEAD,則另一個比較對象就確定為“當前工作目錄”;如果參數是 –cached,則另一個比較對象就被確定為“index file”。
  • 由此可見,commit(不加-a選項)的作用是使用 index 來建立 commit,而和當前目錄無關。

  • git diff:是查看 working tree 與 index file 的差別的。
  • git diff --cached:是查看 index file 與 commit 的差別的。
  • git diff HEAD:是查看 working tree 和 commit 的差別的,在這里 HEAD 代表的是最近的一次commit的信息。

?四、其他

status 命令會顯示當前狀態的一個簡單總結:

[rocrocket@wupengchong test-project]$ git status # On branch master # Changes to be committed: # (use “git reset HEAD <file>…” to unstage) # # modified: file.txt # # Changed but not updated: # (use “git add <file>…” to update what will be committed) # # modified: file.txt #

上面兩行黑體字中的 Changes to be committed 表示在 index 和 commit 的區別狀況。Changed but not updated 表示當前目錄和 index 的區別狀況。

轉載于:https://blog.csdn.net/kuitar/article/details/52413039

(SAW:Game Over!)

總結

以上是生活随笔為你收集整理的Git 索引文件(index file)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。