gpt4 book ai didi

bash - 如何按日期自动压缩 git 历史记录?

转载 作者:行者123 更新时间:2023-12-03 08:54:09 26 4
gpt4 key购买 nike

我有一个 git 存储库,用作文件夹同步系统:每当我更改笔记本电脑、个人电脑或移动设备中的文件中的某些内容时,更改都会自动提交。无分支机构,单用户。

这会导致大量提交,例如每天 50 次。我想编写一个 bash cron 脚本来自动压缩历史记录,每天进行一次提交,无论评论如何,只要保留日期即可。

我尝试了git-rebase -i SHA~count,但我不知道如何自动化该过程,即选择第一个提交并压缩其他计数提交。

有什么建议吗?

我可以编写 bash 来查找日期的第一个 SHA 并计算要 merge 的提交数,对此进行一些循环即可解决问题:

git log --reverse|grep -E -A3 ^commit| \
grep -E -v 'Merge|Author:|--|^$'|paste - -| \
perl -pe 's/commit (\w+)\s+Date:\s+\w+\s+(\w+)\s+(\d+).+/\2_\3 \1/'

最佳答案

我根据 Alderath 建议分享结果:我使用 git filter-branch 来解析历史记录并仅保留当天的最后一次提交。 git log 上的第一个循环会将需要保留的提交时间戳(当天的最后一个)写入临时文件中;然后使用 git filter-branch ,我只保留文件中存在时间戳的提交。

#!/bin/bash

# extracts the timestamps of the commits to keep (the last of the day)
export TOKEEP=`mktemp`
DATE=
for time in `git log --date=raw --pretty=format:%cd|cut -d\ -f1` ; do
CDATE=`date -d @$time +%Y%m%d`
if [ "$DATE" != "$CDATE" ] ; then
echo @$time >> $TOKEEP
DATE=$CDATE
fi
done

# scan the repository keeping only selected commits
git filter-branch -f --commit-filter '
if grep -q ${GIT_COMMITTER_DATE% *} $TOKEEP ; then
git commit-tree "$@"
else
skip_commit "$@"
fi' HEAD
rm -f $TOKEEP

关于bash - 如何按日期自动压缩 git 历史记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56851561/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com