找到两个不共享历史记录的 git 存储库之间最近的提交?



我有两个git存储库,它们都包含相同的代码库,但其中一个已被剥夺了其历史记录。根据特定文件的存在,我不知道第二个存储库的来源超出了模糊的日期范围,我将如何自动扫描历史记录以在最接近第二个回购状态的第一个回购中找到提交?

假设您有2个repos RepoA&RepoBRepoA恰好与大多数历史记录。

RepoA中,提交RepoB

的提交历史记录
git remote add old /path/to/RepoB
git fetch old

现在,您可以使用log命令查看Repoa的主分支中不存在的提交

git log old/master..master

我可以访问文件时使用以下脚本,该文件在GIT历史记录中的某个时刻已被复制。使用此脚本,应该很容易地确定相关的提交。

#!/bin/bash
KNOWN_FILE=$1
KNOWN_SUM=$(cat $KNOWN_FILE | sha1sum)
FILE_REVISION="$2"
shift
shift
SEPARATOR="---n"
echo "The file $KNOWN_FILE($FILE_REVISION) is located in the following commits"
git log "$@" --pretty=format:'%H' | while read commit ; do
    if [ "${KNOWN_SUM}" == "$(git show $commit:$FILE_REVISION 2>&- | sha1sum)" ]; then
        echo "$commit"
        SEPARATOR="---n"
    else
        echo -ne "${SEPARATOR}"
        SEPARATOR=""
    fi
done

它至少需要两个参数,第一个参数是您知道的文件中存在于存储库的GIT历史记录中,第二个参数是GIT存储库中的相对文件路径。其余参数将给予 git log

示例用法

$ ~/bin/history_locate.sh ../pom.xml.old pom.xml --all
The file ../pom.xml.old(pom.xml) is located in the following commits
---
7144ad970b1db19356941b97bc72564290403497
---
51c1cacc2b04ebeed47a4b27b9bf8b8301f5fe7c
e69d6f60edbb76ed3f377f2aac5be6289b19eee5
2b0f446fca4dec8d84781877f0b38d020e5ac2af
46d412ff0a95bea1cf340e736f5a87a90f13bedd
---

最新更新