![](/img/trans.png)
[英]Git gc and git prune warnings when git fetch origin is run
[英]git: check when the last fetch was run
我有一個腳本顯示最后幾個日志更改,但如果一天或兩天沒有運行提取,我希望腳本手動運行它。
以下是它如何工作的示例bash代碼:
#!/bin/bash
# this is what I need:
last_fetch_date=`git fetch --show-last-fetch-date`
# do the math to see how long ago
timestamp=`date -d "$last_fetch_date" +%s`
now=`date +%s`
diff=`echo $now - $timestamp | bc -l`
# two days
if [ `echo $diff' >= 2*24*60*60' | bc -l` == "1" ]; then
git fetch --tags
fi
無法知道何時運行最后一個git fetch
,因為git不存儲該信息。 如果你想知道,每次運行它時都必須在某個地方寫一個時間戳。
但是,你可以找出git fetch
最后實際獲取的東西 。 每次git fetch
獲取新提交時,它都會更新reflog。 要查看它,請使用eg
git reflog show --date=iso -n 1 origin/master
這將顯示帶有日期的origin/master
的最后一次更改。 警告:當你修改它時(通過推動)也會顯示。
最后,在沒有所有這些檢查的情況下,只要方便就可以更容易地獲取。 如果沒有東西可以獲取, git fetch
運行得非常快。
這是我最終做的,感謝@sleske
#!/bin/bash
# do the math to see how long ago was the last fetch
now=`date +%s`
last_fetch=`git reflog show --date=raw -n 1 origin/master | grep -oE '{[0-9]+' | sed 's%{%%'`
diff=`echo $now - $last_fetch | bc -l`
if [ `echo $diff' >= 2*24*60*60' | bc -l` == "1" ]; then
git fetch -q
git fetch --tags -q
fi
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.