Linux/Centos 7:git上如何根据文件的MD5值获取commit id
程序员文章站
2024-01-13 18:24:16
...
爆款:git上如何根据文件的MD5值获取commit id
当我们使用git管理代码的时候,有时只知道某个文件的md5值,但是不知道是哪次提交的,打开show log一次一次的翻看吗???
NO,NO,NO,有了以下这个脚本,轻松获取commit id不是梦,拿走吧 ,不用谢!!!
#!/bin/sh
CHECKSUM=$1
FILE=$2
if [[ -z "$CHECKSUM" ]]; then
echo "Usage: $0 md5 file"
exit 1
elif [[ -z "$FILE" ]]; then
echo "Usage: $0 md5 file"
exit 1
fi
# Check if valid git repo
git status &> /dev/null
if [[ $? -ne 0 ]]; then
echo "Not a valid git repo."
exit 1
fi
# git revision for file
REVS=`git log --pretty=%H -- $FILE`
# check each revision for checksum
for rev in $REVS; do
git show $rev:$FILE > _file_to_check
# if you are on a Linux system, change md5 to md5sum
if [[ -n `md5sum _file_to_check | grep $CHECKSUM` ]]; then
echo $rev
fi
rm _file_to_check
done