首页 > 解决方案 > 如何将文件的所有 git 版本保存到磁盘?

问题描述

在 git 中有一个file.pywhich 有三个版本和三个唯一的提交哈希。

那么如何以编程方式将所有版本恢复到特定文件中,例如:

0_<git_hash>_file.py
1_<git_hash>_file.py
2_<git_hash>_file.py

解决方案不一定是 Python,但git目前正在查看 Python 包。

标签: pythongitgithubgitlab

解决方案


n=0
git log --pretty= --diff-filter=d --raw -- $file | 
while read m1 m2 h1 h2 rest; do
        eval git show $h2 > $((n++))_${h2}.$file
done

或者

n=0
git log --pretty=%h --diff-filter=d -- $file |
while read; do
        eval git show $REPLY:$file > $((n++))_$REPLY.$file
done

取决于您是否希望在结果文件名中包含 blob 或提交的哈希。


推荐阅读