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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7124914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:38:18  来源:igfitidea点击:

How to search a Git repository by commit message?

gitgit-log

提问by Contango

I checked some source code into GIT with the commit message "Build 0051".

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

However, I can't seem to find that source code any more - how do I extract this source from the GIT repository, using the command line?

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

Update

更新

  1. Checked in versions 0043, 0044, 0045 and 0046 using SmartGIT.
  2. Checked out 0043, and checked in versions up to 0051 on a different branch.
  3. Checked out 0043 again.
  4. Now, 0051 has disappeared.
  1. 使用 SmartGIT 检入版本 0043、0044、0045 和 0046。
  2. 检出 0043,并检入不同分支上高达 0051 的版本。
  3. 再次检查了0043。
  4. 现在,0051已经消失了。

Update

更新

The source code is definitely there, now its a matter of checking it out:

源代码肯定在那里,现在是检查出来的问题:

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>

回答by shelhamer

To search the commit log (across all branches) for the given text:

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

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

To search the actual content of commits through a repo's history, use:

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

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

to show all instances of the given text, the containing file name, and the commit sha1.

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

Finally, as a last resort in case your commit is dangling and not connected to history at all, you can search the reflog itself with the -gflag (short for --walk-reflogs:

最后,作为万一您的提交悬而未决并且根本没有连接到历史记录的最后手段,您可以使用-g标志搜索引用日志本身(缩写为--walk-reflogs

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

EDIT: if you seem to have lost your history, check the reflogas your safety net. Look for Build 0051 in one of the commits listed by

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

git reflog

You may have simply set your HEADto a part of history in which the 'Build 0051' commit is not visible, or you may have actually blown it away. The git-ready reflogarticle may be of help.

您可能只是将您HEAD的历史设置为“Build 0051”提交不可见的一部分,或者您实际上可能已经将其吹走了。该混帐就绪引用日志文章可能会有所帮助。

To recover your commit from the reflog: do a git checkout of the commit you found (and optionally make a new branch or tag of it for reference)

要从 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

回答by Alex Howansky

I put this in my ~/.gitconfig:

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

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

Then I can type "git find string" and I get a list of all the commits containing that string in the message. For example, to find all commits referencing ticket #33:

然后我可以输入“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

回答by ryenus

Though a bit late, there is :/which is the dedicated notation to specify a commit (or revision) based on the commit message, just prefix the search string with :/, e.g.:

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

git show :/message

Here <message>can be a complex regex pattern consisting of whitespaces, so please make sure to quote/escape when necessary, e.g.:

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

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

Alternatively, a start point can be specified, to find the closest commit reachable from a specific point, e.g.:

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

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

See: git revisions manual.

请参阅:git 修订手册

回答by Josh Lee

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

回答by Noufal Ibrahim

git log --grep="Build 0051"

should do the trick

应该做的伎俩

回答by Nic

Try this!

尝试这个!

git log | grep -b3 "Build 0051"

回答by Ridha Rezzag

first use git log --onelineto find the SHA of the commit (Message), then I used git log --stat 8zad24dwith the SHA (the first couples sha char example (8zad24d)) to find the right info

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

回答by simhumileco

This:

这个:

git log --oneline --grep='Searched phrase'

or this:

或这个:

git log --oneline --name-status --grep='Searched phrase'

commands work best for me.

命令最适合我。

回答by Samarth S

To search across all the branches

搜索所有分支

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

Once you know which commit you want to get to

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

git checkout <the commit hash>

回答by joseph

If the change is not too old, you can do,

如果变化不是太旧,你可以这样做,

git reflog

git reflog

and then checkout the commit id

然后签出提交ID