Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search a Git repository by commit message?

Tags:

git

git-log

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

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?

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.

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> 
like image 434
Contango Avatar asked Aug 19 '11 16:08

Contango


People also ask

How do I find commit messages?

You can now search for commits from either the main search page or within a repository. Quickly discover who removed set -e or find commits that involved refactoring. Check out… You can now search for commits from either the main search page or within a repository.

Can we search a commit in GitHub?

You can search for commits on GitHub and narrow the results using these commit search qualifiers in any combination. You can search for commits globally across all of GitHub, or search for commits within a particular repository or organization.

What can you find from the body of a commit message?

Commit anatomy A commit message is comprised of a subject, body, and footer, with both the body and footer being optional. The subject is a single line that best sums up the changes made in the commit. The body text is used to provide more details regarding the changes made in the commit.


2 Answers

I put this in my ~/.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:

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 
like image 25
Alex Howansky Avatar answered Oct 21 '22 20:10

Alex Howansky


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.

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 -g flag (short for --walk-reflogs:

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

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

git reflog 

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

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)

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 
like image 147
shelhamer Avatar answered Oct 21 '22 20:10

shelhamer