当我使用 git checkout $commit $filename
check out 文件时我忘了 $commit
但还记得$filename
,我如何找出什么$commit
曾是?
最佳答案
首先是非 git 答案。检查您的 shell 命令历史记录。好吧,如果您没有使用带有命令历史记录的 shell,那么您就不会...
git 答案。您通常找不到 $commit。通常,相同的内容可能是许多提交的一部分,我不认为 git 会记录您已 check out 的单个文件(它会记录 HEAD 以前的值)
这是一个蛮力脚本 git-find-by-contents。使用 $filename 作为参数调用它,它将显示包含此文件的所有提交。正如名字所说
它按内容搜索。因此,只要内容匹配,它就会找到任何名称的文件。
#! /bin/sh
tmpdir=/tmp/$(basename $0)
mkdir $tmpdir 2>/dev/null
rm $tmpdir/* 2>/dev/null
hash=$(git hash-object $1)
echo "finding $hash"
allrevs=$(git rev-list --all)
# well, nearly all revs, we could still check the log if we have
# dangling commits and we could include the index to be perfect...
for rev in $allrevs
do
git ls-tree --full-tree -r $rev >$tmpdir/$rev
done
cd $tmpdir
grep $hash *
rm -r $tmpdir
如果有更优雅的方式,我不会感到惊讶,但这在类似情况下对我有用过几次。
编辑:同样问题的更技术版本出现在这里:Which commit has this blob?
关于cherry-pick - 如何找出 check out 文件来自哪个提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6957441/