Git——對象——commit

  commit對象由兩部分組成。

  概念:定義,標識。

  操作:單個commit操作(新增,查看,查ID,查關係),多個commit操作(比較)

1、概念

1.1  定義

  A commit is used to record changes to a repository

  提交用於記錄版本庫的變更。它指明瞭提交的用途。

  Git compares the current state of the index to the previous snapshot and so derives a list of affected files and directories. Git create new blobs for any file that has changed and new tree for any directory that has changed, and it reuses any blob or tree object that has not changed.

  當進行一次提交時,會列出變更集。若變更集中存在新文件或新文件夾,會創建新的blob和tree對象,若存在同樣的內容,同樣的文件夾,會複用blob和tree對象。

  There is a one-to-one correspondence between a set of changes in the repository and a commit. A commit is the only method of introducing changes to a repository, and any change in the repository must be introduced by a commit

  commit對象與變更集的關係時一一對應的。

  Regardless of the number of directories, files, lines, or bytes that change with a commit, either all changes apply or none do

  變更集入庫,遵循原子性,全部成功或失敗。

1.2  標識

  commit對象的標識有兩種。

  ID:SHA1 hash ID,通常前6位具有唯一性。

  引用:有三種類型的引用,tag, HEAD等內置引用,特定提交和符號的組合。

1.2.1  ID

  查看commit對象的ID,輸出指令:git log -1 commitId

commit 253a497823248dc388c7ce5291b060fe7dd4f641
Author: WorldInNet <[email protected]>
Date:   Thu Jun 25 16:52:20 2020 +0800

    提交hello.txt

  第一行:commitId

  第二行:作者和郵箱。

  第三行:提交的日期。

  第四行:提交的日誌信息。

1.2.2  tag

  第一步,創建tag, 關聯commit,first_commit爲tag的名稱。

git tag -f -m “初始化提交” first_commit HEAD

  第二步,使用,git log -1 first_commit。可以在./git/refs/tag目錄, 查看tag對象。

1.2.3  內置引用

  HEAD:always refers to the most recent commit on the current branch

HEAD通常指向當前分支的最近一次提交。後續會看到有些命令可以自定義HEAD引用關聯的commit對象。

ORGI_HEAD:current operations such as merge and reset, record the previous version of HEAD in ORIG_HEAD just prior to adjusting it to a new value

ORGI_HEAD是進行某些操作時HEAD的備份,例如merge,reset操作時。

FETCH_HEAD:when remote repositories are used, git fetch records the heads of all branches fetched in the file .git/FETCH_HEAD.

當使用遠程版本庫時,本地版本庫拉取遠程版本庫時,會把遠程版本庫的分支記錄在.git/FETCH_HEAD中。

MERGE_HEAD: when a merge is in progress, the tip of the other branch is temporarily recorded in the symref MERGE_HEAD

進行分支合併操作時,MERGE_HEAD記錄被合併分支的最近一次提交。例如dev分支合併到master分支,MERGE_HEAD記錄dev分支的最近一次提交,ORGI_HEAD記錄master分支的最近一次提交。

1.2.4  符號

The tilde(~) is used to go back before an ancestral parent and select a preceding generation

~符號會查找當前提交的父提交,祖父提交,直到root提交(初始提交)

The caret(^) is used to select a different parent

  當提交有多個父提交時,^基於後面的數值尋找不同的父提交,例如head^1尋找第一個父提交,head^2尋找第二個父提交。一個提交存在多個父提交的情形只有合併分支時纔會有,父提交的數量等於分支的數量。例如dev,test, 合併到master分支上,此時提交有三個父提交。通常只有兩個。

  

  A~1和A^1是相同的,都指向第一個父提交。A可以是ID,HEAD等內置引用,tag中的任意一種。通常內置引用較爲常見。

2、操作

  新增,查看,查ID, 查關係,比較,commit對象不能刪除。

2.1  commit(新增)

  git commit命令的格式如下:

git commit [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend]
           [--dry-run] [(-c | -C | --fixup | --squash) <commit>]
           [-F <file> | -m <msg>] [--reset-author] [--allow-empty]
           [--allow-empty-message] [--no-verify] [-e] [--author=<author>]
           [--date=<date>] [--cleanup=<mode>] [--[no-]status]
           [-i | -o] [--pathspec-from-file=<file> [--pathspec-file-nul]]
           [-S[<keyid>]] [--] [<pathspec>…]

  提交信息相關:除了-m,其他基本無需配置。

  1. –-date:指定提交的日期
  2. -m:指定提交的日誌,從命令行讀取。 -f從文件中讀取。
  3. –author:指定提交的作者。

  commit對象一旦創建,只能修改它的日誌信息,添加 --amend,-m 選項,可以修改。

2.2  show(查看)

  git show命令的格式如下:

git show [<options>] [<object>…]

  options:大部分是顯示相關的設置,例如—abbrev-commit, --encoding, --online等等。

  object:任意的對象ID。

  示例如下:

commit 4ea609fd5e88b4d7968576b06e292621062d0de8
Author: xx<[email protected]>
Date:   Sun Jul 5 17:43:02 2020 +0800

    Squash other分支上的變更

diff --git a/other.txt b/other.txt
index e69de29..b6c17e7 100644
--- a/other.txt
+++ b/other.txt
@@ -0,0 +1,3 @@
+A new Line
+A other Line
+A other_sum line
\ No newline at end of file

  第一部分顯示的提交的ID,作者,日期。第二部分顯示的是提交的變更集。

2.3  rev-parse(查ID)

  根據引用查詢commit對象的ID。git rev-parse命令的格式如下:

git rev-parse commitName

  commitName的格式有以下三種:

  第一種引用:例如HEAD, MERGE_HEAD, ORGI_HEAD等

  第二種符號引用:例如HEAD~1, HEAD^1等

  第三種符號引用表達式:HEAD~1..HEAD,HEAD~1...HEAD, HEAD^@(HEAD提交的所有父提交對象),HEAD^!(只包含HEAD,不包含所有父對象)。

  示例如下:

git rev-parse HEAD~1..HEAD
9c8c5ed60ab2f65522b8ed30cba94a87e78596d0
^04899af01ba5a0c19f7ea137372b2be1823b8ed7

2.4  cat-file(查關係)

  git cat-file有多種格式,查詢關係的格式爲

git cat-file (-e | -p) <object>

  e,exist,校驗當前對象是否存在。

  p,print,打印當前對象的信息。

  執行git cat-file -p commitID, 結果如下:

tree dc0be9aedfa329e9285c1d63daef14363dc7f530
parent 05eb815196683212ee3371df47eb942fcdfa0b58
author xx<[email protected]> 1593942182 +0800
committer xx <[email protected]> 1593942182 +0800

Squash other分支上的變更

  tree:關聯的tree對象。 parent:父commit對象的ID。

2.5  diff(對比)

  git diff有多種格式,比較兩個commit對象的格式爲:

git diff [<options>] <commit> <commit> [--] [<path>…]

  Options,略。

  Path是指文件或文件夾的路徑,它縮小了比較的範圍。

  它也可以用於比較分支和分支,當<commit> <commit>都是分支名稱時,比較二者最近一次提交對象。

  執行git diff HEAD~1 HEAD,結果如下:

$ git diff HEAD^1 HEAD
diff --git a/differ.txt b/differ.txt
deleted file mode 100644
index e69de29..0000000
diff --git a/file.txt b/file.txt
deleted file mode 100644
index 9d0caac..0000000
--- a/file.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-修改後的內容
-追加的內容
-master分支對內容的修改
\ No newline at end of file
diff --git a/hello/newHello.txt b/hello/newHello.txt
deleted file mode 100644
index 5e1c309..0000000
--- a/hello/newHello.txt
+++ /dev/null
@@ -1 +0,0 @@
-Hello World
\ No newline at end of file

  由於比較結果可讀性較差,實際使用比較工具或IDE。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章