2015-08-28 32 views
4

我需要的日期和格式提交一套github上的結果:如何使git日誌輸出只顯示日期和散列在一行上?

Date Commit 
19 Mar 2015 b6959eafe0df6031485509c539e22eaf2780919c 
1 Apr 2015 9a1f13339cc7d43930440c2350ea3060b72c8503 
1 Apr 2015 1e76036421ca4d72c371182fc494af514911b099 

我能夠得到這個命令的日期:

git log | grep Date: | awk '{print $4 " " $3 " " $6}' 

提交
git log | grep commit | awk '{print $2}' 

如何組合這些命令,以便將兩個結果打印在同一行中?

這裏是git的日誌的例子:

commit 1e76036421ca4d72c371182fc494af514911b099 
Author: xxxx 
Date: Wed Apr 1 17:35:36 2015 +0200 

    First web app commit 

    First web app commit 

commit 9a1f13339cc7d43930440c2350ea3060b72c8503 
Author: xxxx 
Date: Wed Apr 1 17:28:42 2015 +0200 

    change from app to website 

commit b6959eafe0df6031485509c539e22eaf2780919c 
Author: xxx 
Date: Thu Mar 19 18:58:33 2015 +0100 

    First remote commit: hello world 

    scheleton of the project now available 

commit a41d419de46a59e72dbc52e5f39c9d8a8a9af72a 
Author: xxxx 
Date: Thu Mar 19 17:31:16 2015 +0100 

    Initial commit 

回答

4

「git log」命令的功能足以處理這個問題。玩弄變化這個:

git log --date=iso --pretty=format:"date={%ad}, commit={%H}" 

對於我這種輸出:

date={2014-12-12 14:14:23 -0800}, commit={75390af8bcd42c4dde6d841dc53f66a9ca91f460} 
date={2014-12-09 18:53:32 -0800}, commit={8dcf9eb10611e6ac778e518cf37efb041c69f5b5} 
date={2014-12-11 17:17:26 -0800}, commit={c3c1120b6dda6b317e1de5c7fe4eed7c8741ea1a} 

達到你指定的確切格式(雖然日期格式稍有更合理),試試這個:

​git log --date=short --pretty=format:"%ad %H" 

,輸出喜歡的東西:

2015-08-18 1d0ca5a596f02958c4ba8ff269def3f235c64495 
2015-08-18 d92fc3ebb62d2ca3e24322db2b669fdaebde7ea1 
2015-08-18 6ba3970620e19699c2969b1eed5c479b07b8908a 
+0

謝謝!最後的建議完美地工作。 git log --date = short --pretty = format:「%ad%H」 – Botacco

1

你更多或更少從未需要grep | awkawk能爲你做的匹配。

因此grep Date: | awk '{print $4 " " $3 " " $6}'可以被重寫爲awk '/Date:/ {print $4 " " $3 " " $6}'並且grep commit | awk '{print $2}'可以被重寫爲awk '/commit/ {print $2}'

這應該足以讓你第一次瞭解如何解決你的問題。

結合awk腳本。

git log | awk ' 
    # Print the header line (remove if you do not want it). 
    BEGIN {print "Date Commit"} 
    /^commit/ { 
     # Save the current commit 
     commit=$2 
     # Skip to the next line 
     next 
    } 
    /^Date:/ { 
     # Print the fields from the current line and the saved commit 
     print $4, $3, $6, commit 
     # Clear the saved commit Just in Case 
     commit="" 
    } 
' 

這就是說,你可以讓你從git log本身要幾乎格式:如果你想,你都可以轉換成所需輸出一些AWK

$ git log --format='%ad %H' --date=short 
2015-03-19 b6959eafe0df6031485509c539e22eaf2780919c 
2015-04-1 9a1f13339cc7d43930440c2350ea3060b72c8503 
2015-04-1 1e76036421ca4d72c371182fc494af514911b099 

。 (移調日期字段是微不足道的,轉換爲一個月的名稱將需要更多的代碼/工作。)

+0

謝謝! 現在我還有一個小問題我解決不了...... 我需要啓動兩個命令的每一行: git的結帳###### 聲納聖農-Dsonar.projectDate =日期 例如,在你的git日誌中,第一行應該是: git checkout b6959eafe0df6031485509c539e22eaf2780919c sonar-runner -Dsonar.projectDate = 2015-03-19 – Botacco

+0

如果你需要這樣的輸出,那麼在這裏使用第二位,並使格式字符串看起來像什麼你要。它可以包含任何你想要的非格式文本。所以'git checkout%H; sonar-sunner -Dsonar.projectDate =%ad'或其他。 –

0

如何:

git log | awk '/^commit/{ commit = $2 } 
       /^Date:/{ printf "%s %s %s %s\n", $4, $3, $6, commit }' 

捕獲的變量提交的信息提交。當日期出現時,打印日期和提交信息。

grep的輸出流水至awk通常是一個壞主意; awk完全能夠執行grep所做的過濾。

1

爲什麼不只是做git log -2 --pretty=format:"%aD %H",這給像輸出:

Fri, 28 Aug 2015 18:11:24 +0900 4c087df377e58b16c45beb32b4aebae6d2d0fa35 
Sun, 23 Aug 2015 21:38:55 +0900 2d6543a124b46c5b9e8b7d645659e9a187dcd3c1 
Sun, 16 Aug 2015 18:27:26 +0900 0cb0811124baa18a6baf5c13660affc18d86e725 
Sun, 16 Aug 2015 15:21:55 +0900 072870e0f9659fc8ca8012099d689a9e0d020f6e 
0

這將做

git log -5 --pretty=format:"%ad %H" | awk '{print $3, $2, $5, $7}' 

10 Aug 2015 9fb4b1b5e7bcbc7f55e40ceae1758a7288673a51 
10 Aug 2015 3d613862ca53787c97a42e8e2c37e09c8752b637 
7 Aug 2015 727af2863dbe9f7b4da9189bc12f97a99aac5705 
7 Aug 2015 1438b43b5755079bb3e62c819bfef2629d8336e9 
7 Aug 2015 34303c19fb11482d66fc3c1102bc8d8b433af21a 
相關問題