git log

煎雞蛋湯發表於2018-03-20

本文於2017年12月29號釋出在個人部落格中,因為個人部落格關閉,全部遷移到CSDN,以下是正文:


想知道都有哪些提交嗎?“git log”可以幫你實現啊

進入任何倉庫目錄,輸入命令:

$ git log
commit e9cec2032aadc0f630530ac1dd63f2c92c70d1dc
Author: anyscoding <anyscoding@sina.com>
Date: Sun Dec 10 08:21:56 2017 +0800

push code to remote

1. add a new line to README.md
2. add a new file

commit 4a3f515b52ac34d421d52b8fe13918a63b94ee62
Author: cug_heshun <cug_heshun@sina.com>
Date: Fri Dec 8 07:06:58 2017 +0800

undo commit

預設情況下,git log會按照時間逆序(提交越晚顯示越靠前)分頁顯示所有的提交記錄,每條提交記錄包括:

  • SHA-1 commit id
  • 作者,使用者名稱和郵箱
  • 時間
  • commit message

git log有很多引數,本文只列出幾個常用的,如下表:

選項 說明
-n 僅顯示最近的 n 條提交
-p 按補丁格式顯示每個更新之間的差異
–state 顯示每次更新的檔案修改統計資訊
–pretty 使用其他格式顯示歷史提交資訊。可用的選項包括 oneline,short,full,fuller 和 format(後跟指定格式)
–graph 顯示 ASCII 圖形表示的分支合併歷史
–abbrev-commit 僅顯示 SHA-1 的前幾個字元,而非所有的 40 個字元
–since, –after 僅顯示指定時間之後的提交
–until , –before 僅顯示指定時間之前的提交
–author 僅顯示指定作者相關的提交
–committer 僅顯示指定提交者相關的提交
–grep 僅顯示含指定關鍵字的提交
-S 僅顯示新增或移除了某個關鍵字的提交

簡單的引數,諸如:

-n, -p, --state, --graph, --abbrev-commit, --author, --committer等

就不做詳細的介紹了,下面講一講幾個稍微複雜點的引數

為了方便演示,接下來的示例都會帶上“–abbrev-commit”(僅顯示 SHA-1 的前幾個字元,而非所有的 40 個字元)引數

–pretty

–pretty命令的選項又有多個:

oneline,把提交記錄顯示在一行上,顯示的內容包括:
    SHA-1 "commit message title"
short,提交記錄的簡短介紹,顯示的內容包括:
    commit SHA-1前幾個字元
    Author: xxx
    commit message title
full,相比short多了Committer
fuller,相比full多了author date和commit date
format,後跟格式控制字元,命令為:git log --pretty=format:"xx xx",格式控制字元見下表
選項 說明
%H 提交物件(commit)的完整雜湊字串
%h 提交物件的簡短雜湊字串
%T 樹物件(tree)的完整雜湊字串
%t 樹物件的簡短雜湊字串
%P 父物件(parent)的完整雜湊字串
%p 父物件的簡短雜湊字串
%an 作者(author)的名字
%ae 作者的電子郵件地址
%ad 作者修訂日期(可以用 –date= 選項定製格式)
%ar 作者修訂日期,按多久以前的方式顯示
%cn 提交者(committer)的名字
%ce 提交者的電子郵件地址
%cd 提交日期
%cr 提交日期,按多久以前的方式顯示
%s 提交說明

來看一個format的示例:

$ git log --abbrev-commit --pretty=format:"%h %an %cn %s"
4cb5d87 anyscoding anyscoding first commit to develop branch
a3bdf3b anyscoding GitHub add
3a0b882 anyscoding GitHub delete lines for aws test
e386e10 anyscoding GitHub add new line for aws test again
d132579 anyscoding GitHub add new line for aws test build action output
77ccea1 anyscoding GitHub delete added lines in README.md
541efff anyscoding GitHub add new line for aws
cce0567 anyscoding GitHub Update README.md
e9cec20 anyscoding anyscoding push code to remote
4a3f515 cug_heshun cug_heshun undo commit
28ca9cc cug_heshun cug_heshun just test
9b4e410 anyscoding GitHub Initial commit

–since/–after和–until/–before

版本轉測在即,專案owner需要整理當前版本新增特性以及修復bug的列表,查詢指定時間內的歷史記錄就可以排上用場了

限制起始時間:–after, –since。–after/–since=”2017-12-07″表示2017-12-07之後,不包括2017-12-07

限制結束時間:–before, –until。–before/–until=”2017-12-11″表示2017-12-11之前,不包括2017-12-11

$ git log --pretty=format:"%h %cn %cd %s" --since="2017-12-07" --until="2017-12-11"
e9cec20 anyscoding Sun Dec 10 08:21:56 2017 +0800 push code to remote
4a3f515 cug_heshun Fri Dec 8 07:06:58 2017 +0800 undo commit
28ca9cc cug_heshun Fri Dec 8 07:02:42 2017 +0800 just test
9b4e410 GitHub Fri Dec 8 06:48:00 2017 +0800 Initial commit

$ git log --pretty=format:"%h %cn %cd %s" --after="2017-12-07" --before="2017-12-11"
e9cec20 anyscoding Sun Dec 10 08:21:56 2017 +0800 push code to remote
4a3f515 cug_heshun Fri Dec 8 07:06:58 2017 +0800 undo commit
28ca9cc cug_heshun Fri Dec 8 07:02:42 2017 +0800 just test
9b4e410 GitHub Fri Dec 8 06:48:00 2017 +0800 Initial commit

an@DESKTOP-IEU7HQD MINGW64 /d/GitHub/HelloWorld (master)
$

–grep

有些時候,可能是迴歸測試,也可能是為了給其他程式設計師演示某些特性的寫法,需要查詢commit message中包含了特定字元的提交,–grep就可以排上用場了

$ git log --grep="aws" --pretty=oneline
3a0b882ff42121f72f2490d9c1eced342cbcdef0 delete lines for aws test
e386e10bac03d41c2fc697d3148ed0b8a5cbe906 add new line for aws test again
d132579b93ce58c187a3e8d249400063b80ad97f add new line for aws test build action output
541efff5c876b971ce74174eb553bb10c484d42c add new line for aws

$

-S

想象一下,你的程式碼現在跑不起來了,錯誤資訊提示找不到某識別符號,肯定是被誰刪掉了,我們需要找出來這次提交記錄,做一番詳細的審查,-S命令就可以派上用場了

$ git log -S"first"
commit 9b4e410354a3c48bfc8993b4d096f24b89df828f
Author: anyscoding <34288556+anyscoding@users.noreply.github.com>
Date: Fri Dec 8 06:48:00 2017 +0800

Initial commit

$ git log -S"develop"
commit 4cb5d8773ac2867b813901dec98c7c072c59e4d0 (HEAD -> master)
Author: anyscoding <anyscoding@sina.com>
Date: Wed Dec 27 07:40:17 2017 +0800

first commit to develop branch

$

git show

如上,找到刪除或修改的提交記錄之後,還需要看個究竟,可以使用git show命令檢視指定commit的修改內容

$ git show 4cb5d8773ac2867b813901dec98c7c072c59e4d0
commit 4cb5d8773ac2867b813901dec98c7c072c59e4d0 (HEAD -> master)
Author: anyscoding <anyscoding@sina.com>
Date: Wed Dec 27 07:40:17 2017 +0800

first commit to develop branch

diff --git a/note.md b/note.md
new file mode 100644
index 0000000..c9764c1
--- /dev/null
+++ b/note.md
@@ -0,0 +1 @@
+this is a develop branch

$

特定路徑的歷史記錄

git log /path/to/file 命令可以檢視指定路徑的歷史記錄

$ git log --pretty=oneline README.md
a3bdf3b8a1b2332597ebd0417dab42a6a6b2eb5a (origin/master, origin/HEAD) add
3a0b882ff42121f72f2490d9c1eced342cbcdef0 delete lines for aws test
e386e10bac03d41c2fc697d3148ed0b8a5cbe906 add new line for aws test again
d132579b93ce58c187a3e8d249400063b80ad97f add new line for aws test build action output
77ccea126fdc3ff2ab3a92d5e9458af457090812 delete added lines in README.md
541efff5c876b971ce74174eb553bb10c484d42c add new line for aws
cce0567986c702792a28ff510fef6c585e68344f Update README.md
e9cec2032aadc0f630530ac1dd63f2c92c70d1dc push code to remote
9b4e410354a3c48bfc8993b4d096f24b89df828f Initial commit

相關文章