git 統計兩個不同版本程式碼修改行數
sourceTree 點選命令列模式,輸入以下命令:
1、統計標籤tag1、tag2之間的程式碼修改的行數
git log tag1..tag2 --pretty=tformat: --numstat | gawk '{ add += $1 ; subs += $2 ; loc += $1 + $2 } END { printf "added lines: %s removed lines : %s total lines: %s\n",add,subs,loc }'
mac升級最新系統後,取消了gawk,改用awk命令
git log tag1..tag2 --pretty=tformat: --numstat | awk '{ add += $1 ; subs += $2 ; loc += $1 + $2 } END { printf "added lines: %s removed lines : %s total lines: %s\n",add,subs,loc }'
輸出:added lines: 12174 removed lines : 8284 total lines : 20458
2、統計某個作者修改的行數
git log --author="$(git config --get user.name)" --pretty=tformat: --numstat | gawk '{ add += $1 ; subs += $2 ; loc += $1 + $2 } END { printf "added lines: %s removed lines : %s total lines: %s\n",add,subs,loc }'
3、統計兩個日期之間的修改的行數
git log --pretty=tformat: --since ==2018-4-25 --until=2018-6-27 --numstat | gawk '{ add += $1 ; subs += $2 ; loc += $1 + $2 } END { printf "added lines: %s removed lines : %s total lines: %s\n",add,subs,loc }'
git log --format='%aN' | sort -u | while read name; do echo -en "$name\t"; git log --author="$name" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done
git log 引數說明:
--author 指定作者
--stat 顯示每次更新的檔案修改統計資訊,會列出具體檔案列表
--shortstat 統計每個commit 的檔案修改行數,包括增加,刪除,但不列出檔案列表:
--numstat 統計每個commit 的檔案修改行數,包括增加,刪除,並列出文件列表:
-p 選項展開顯示每次提交的內容差異,用 -2 則僅顯示最近的兩次更新
例如:git log -p -2
--name-only 僅在提交資訊後顯示已修改的檔案清單
--name-status 顯示新增、修改、刪除的檔案清單
--abbrev-commit 僅顯示 SHA-1 的前幾個字元,而非所有的 40 個字元
--relative-date 使用較短的相對時間顯示(比如,“2 weeks ago”)
--graph 顯示 ASCII 圖形表示的分支合併歷史
--pretty 使用其他格式顯示歷史提交資訊。可用的選項包括 oneline,short,full,fuller 和 format(後跟指定格式)
例如: git log --pretty=oneline ; git log --pretty=short ; git log --pretty=full ; git log --pretty=fuller
--pretty=tformat: 可以定製要顯示的記錄格式,這樣的輸出便於後期程式設計提取分析
例如:git log --pretty=format:""%h - %an, %ar : %s""
下面列出了常用的格式佔位符寫法及其代表的意義。
選項 說明
%H 提交物件(commit)的完整雜湊字串
%h 提交物件的簡短雜湊字串
%T 樹物件(tree)的完整雜湊字串
%t 樹物件的簡短雜湊字串
%P 父物件(parent)的完整雜湊字串
%p 父物件的簡短雜湊字串
%an 作者(author)的名字
%ae 作者的電子郵件地址
%ad 作者修訂日期(可以用 -date= 選項定製格式)
%ar 作者修訂日期,按多久以前的方式顯示
%cn 提交者(committer)的名字
%ce 提交者的電子郵件地址
%cd 提交日期
%cr 提交日期,按多久以前的方式顯示
%s 提交說明
--since 限制顯示輸出的範圍,
例如: git log --since=2.weeks 顯示最近兩週的提交
選項 說明
-(n) 僅顯示最近的 n 條提交
--since, --after 僅顯示指定時間之後的提交。
--until, --before 僅顯示指定時間之前的提交。
--author 僅顯示指定作者相關的提交。
--committer 僅顯示指定提交者相關的提交。
一些例子: git log --until=1.minute.ago // 一分鐘之前的所有 log git log --since=1.day.ago //一天之內的log git log --since=1.hour.ago //一個小時之內的 log git log --since=`.month.ago --until=2.weeks.ago //一個月之前到半個月之前的log git
log --since ==2013-08.01 --until=2013-09-07 //某個時間段的 log git blame
看看某一個檔案的相關歷史記錄
例如:git blame index.html --date short