cherry-pick - 如何找出 check out 文件来自哪个提交

标签 cherry-pick pre-commit git-checkout

当我使用 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/

相关文章:

git - 挑选一个提交包括以前的提交?

仅针对特定子文件夹的 Git 预提交 Hook ?

perl - 颠覆存储库中增量的格式是什么,如果我在预提交钩子(Hook)中更​​改它们,我能把它炸到什么程度?

Git:放弃自某个提交以来对单个文件的所有更改

windows - git checkout 时出错。路径太长

git - 如何在 TeamCity 中构建 git 标签?

git - 如何使用 gitcherry-pick 和不同的提交注释推送提交

git - 为什么“cherry-pick”会导致 git merge 丢弃我的部分提交?

git cherry-pick -x : link in details instead of in summary

python - Git 预提交 Hook : getting list of changed files