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

Con*_*ngo 761 git git-log

我使用提交消息"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>
Run Code Online (Sandbox Code Playgroud)

更新

使用以下内容检索丢失的源代码:

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>
Run Code Online (Sandbox Code Playgroud)

现在,一切都在SmartGit中再次可见.问题解决了 - 你们是最好的,特别是@shelhamer!

she*_*mer 1176

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

git log --all --grep='Build 0051'
Run Code Online (Sandbox Code Playgroud)

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

git grep 'Build 0051' $(git rev-list --all)
Run Code Online (Sandbox Code Playgroud)

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

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

git log -g --grep='Build 0051'
Run Code Online (Sandbox Code Playgroud)

编辑:如果您似乎丢失了历史记录,请将其reflog作为您的安全网.在列出的其中一个提交中查找Build 0051

git reflog
Run Code Online (Sandbox Code Playgroud)

您可能只是将您设置HEAD为历史记录的一部分,其中"Build 0051"提交不可见,或者您可能实际上已将其炸毁.该混帐就绪引用日志 文章可能会有所帮助.

要从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
Run Code Online (Sandbox Code Playgroud)

  • 在Windows命令行中,我必须使用"而不是" (15认同)
  • git log -i -grep (6认同)
  • 如何使第一个命令不区分大小写? (5认同)
  • 在 Windows 上,我必须使用双引号而不是单引号才能使其正常工作。 (4认同)
  • 与git grep'Build 0051'$(git rev-list --all)我得到sh.exe“:/ bin / git:错误的文件号可以更改此方法吗? (2认同)

Ale*_*sky 76

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

[alias]
    find = log --pretty=\"format:%Cgreen%H %Cblue%s\" --name-status --grep
Run Code Online (Sandbox Code Playgroud)

然后我可以输入"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
Run Code Online (Sandbox Code Playgroud)

  • 谢谢。我发现别名非常有用。我不喜欢这种着色,发现单行格式非常适合这个。还添加 -i 使其不区分大小写是有帮助的。例如`find = log --oneline --name-status -i --grep` (7认同)
  • 我更喜欢打印完整的git消息(grep可能发生在那里,而不是在标题中),所以我最终得到:`find = log --all --pretty = \"format:%Cgreen%H%Cblue%s \n%b%Creset \" - name-status --grep`.注意`--all`和`%b`字符.对于`reset`提示,请选择@AshleyCoolman. (4认同)
  • 不错,但可能缺少颜色重置?`--pretty=\"格式:%Cgreen%H %Cblue%s%Creset\"` (2认同)

rye*_*nus 32

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

git show :/message
Run Code Online (Sandbox Code Playgroud)

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

git log -1 -p ":/a few words"
Run Code Online (Sandbox Code Playgroud)

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

git show 'HEAD^{/fix nasty bug}'
Run Code Online (Sandbox Code Playgroud)

请参阅:git修订手册.


Jos*_*Lee 21

git log --grep=<pattern>
            Limit the commits output to ones with log message that matches the
            specified pattern (regular expression).
Run Code Online (Sandbox Code Playgroud)


Nou*_*him 16

git log --grep="Build 0051"
Run Code Online (Sandbox Code Playgroud)

应该做的伎俩

  • 必须使用双引号 (") 而不是单引号 (')。 (3认同)

小智 10

只是对命令的一个小补充git log --all --grep:在我的例子中,我必须转义消息内的括号:

git log --all --grep="\[C\] Ticket-1001: Fix nasty things"
Run Code Online (Sandbox Code Playgroud)

  • 找到提交后,您可能想找到它的分支: `gitbranch -a --contains &lt;hash&gt;` (归功于 /sf/answers/189497731/) (2认同)

Nic*_*Nic 8

试试这个!

git log | grep -b3 "Build 0051"
Run Code Online (Sandbox Code Playgroud)

  • 你想使用`git grep`而不是普通的grep,并提供`--all`标志来搜索多个分支. (6认同)
  • @Gravitas,尝试使用reflog的示例:使用`git log -g`的示例.您可能在重置时丢失了历史记录,但如果没有垃圾回收,您仍然可以检查它. (3认同)

小智 8

搜索所有分支

git log --all --grep='Build 0051'
Run Code Online (Sandbox Code Playgroud)

一旦你知道你想要进行哪个提交

git checkout <the commit hash>
Run Code Online (Sandbox Code Playgroud)


Rid*_*zag 7

首先列出提交使用

git log --oneline
Run Code Online (Sandbox Code Playgroud)

然后找到提交的SHA(消息),然后我用

 git log --stat 8zad24d
Run Code Online (Sandbox Code Playgroud)

(8zad24d) 是与您感兴趣的提交相关的 SHA(第一对夫妇 sha 示例 (8zad24d) 您可以选择 4 个字符或 6 或 8 个或整个 sha)以找到正确的信息


Mar*_*win 7

对于任何想要传递完全匹配的任意字符串(并且不用担心转义正则表达式特殊字符)的人,git log 采用 --fixed-strings 选项

git log --fixed-strings --grep "$SEARCH_TERM" 
Run Code Online (Sandbox Code Playgroud)