如何通过提交消息搜索Git存储库?

问题描述 投票:668回答:7

我使用提交消息“Build 0051”检查了一些源代码到GIT中。

但是,我似乎无法再找到源代码 - 如何使用命令行从GIT存储库中提取此源代码?

更新

  1. 使用SmartGIT检查版本0043,0044,0045和0046。
  2. 检出0043,并检查其他分支上的版本0051。
  3. 再次检查0043。
  4. 现在,0051已经消失了。

更新

源代码肯定存在,现在需要检查它:

C:\Source>git log -g --grep="0052"
commit 77b1f718d19e5cf46e2fab8405a9a0859c9c2889
Reflog: HEAD@{10} (unknown <Mike@.(none)>)
Reflog message: commit: 20110819 - 1724 - GL: Intermediate version. File version:  v0.5.0 build 0052.
Author: unknown <Mike@.(none)>
Date:   Fri Aug 19 17:24:51 2011 +0100

    20110819 - 1724 - GL: Intermediate version. File version: v0.5.0 build 0052.

C:\Source>
git git-log
7个回答
1091
投票

要搜索给定文本的提交日志(跨所有分支):

git log --all --grep='Build 0051'

要通过回购历史记录搜索提交的实际内容,请使用:

git grep 'Build 0051' $(git rev-list --all)

显示给定文本的所有实例,包含文件名和提交sha1。

最后,作为最后的手段,如果您的提交悬空并且根本没有连接到历史记录,您可以使用-g标志搜索reflog本身(--walk-reflogs的缩写:

git log -g --grep='Build 0051'

编辑:如果你似乎已经失去了你的历史,请检查reflog作为你的安全网。在列出的其中一个提交中查找Build 0051

git reflog

您可能只是将HEAD设置为历史记录的一部分,其中“Build 0051”提交不可见,或者您可能实际上将其吹走了。 git-ready reflog文章可能有所帮助。

要从reflog中恢复您的提交:执行您找到的提交的git checkout(并可选择创建一个新的分支或标记以供参考)

git checkout 77b1f718d19e5cf46e2fab8405a9a0859c9c2889
# alternative, using reflog (see git-ready link provided)
# git checkout HEAD@{10}
git checkout -b build_0051 # make a new branch with the build_0051 as the tip

71
投票

我把它放在我的〜/ .gitconfig中:

[alias]
    find = log --pretty=\"format:%Cgreen%H %Cblue%s\" --name-status --grep

然后我可以输入“git find string”,我会得到一条包含该消息中所有字符串的提交列表。例如,要查找引用故障单#33的所有提交:

029a641667d6d92e16deccae7ebdeef792d8336b Added isAttachmentEditable() and isAttachmentViewable() methods. (references #33)
M       library/Dbs/Db/Row/Login.php

a1bccdcd29ed29573d2fb799e2a564b5419af2e2 Add permissions checks for attachments of custom strategies. (references #33).
M       application/controllers/AttachmentController.php

38c8db557e5ec0963a7292aef0220ad1088f518d Fix permissions. (references #33)
M       application/views/scripts/attachment/_row.phtml

041db110859e7259caeffd3fed7a3d7b18a3d564 Fix permissions. (references #33)
M       application/views/scripts/attachment/index.phtml

388df3b4faae50f8a8d8beb85750dd0aa67736ed Added getStrategy() method. (references #33)
M       library/Dbs/Db/Row/Attachment.php

26
投票

虽然有点晚,但是有:/,它是基于提交消息指定提交(或修订)的专用符号,只是在搜索字符串前加上:/,例如:

git show :/message

这里<message>可以是由空格组成的复杂正则表达式模式,因此请务必在必要时引用/转义,例如:

git log -1 -p ":/a few words"

或者,可以指定起始点,以查找从特定点可到达的最接近的提交,例如:

git show 'HEAD^{/fix nasty bug}'

见:git revisions manual


20
投票
git log --grep=<pattern>
            Limit the commits output to ones with log message that matches the
            specified pattern (regular expression).

15
投票
git log --grep="Build 0051"

应该做的伎俩


8
投票

试试这个!

git log | grep -b3 "Build 0051"

3
投票

首先使用git log --oneline查找提交的SHA(消息),然后我使用带有SHA的git log --stat 8zad24d(第一对情侣sha char示例(8zad24d))来查找正确的信息

© www.soinside.com 2019 - 2024. All rights reserved.