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

时间:2017-02-27 19:53:09

标签: git

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

2 个答案:

答案 0 :(得分:0)

假设您有2个回购RepoA& RepoBRepoA恰好与大多数历史相关。

RepoA提取RepoB

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

现在您可以使用log命令查看RepoA主分支中RepoB / master中不存在的提交

git log old/master..master

答案 1 :(得分:0)

当我有权访问文件时,我使用以下脚本,这些文件在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
---
相关问题