Contango
Contango

Reputation: 80398

How to search a Git repository by commit message?

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?

Upvotes: 1277

Views: 675522

Answers (13)

shelhamer
shelhamer

Reputation: 31170

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

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

To do so while ignoring case in the grep search:

git log --all -i --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.

And to do this while ignoring case, use:

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

Note that this searches the entire content of the commit at each stage, and not just the diff changes. To search just the diff changes, use one of the following:

git log -S[searchTerm]
git log -G[searchTerm]

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'

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

Upvotes: 1925

suhailvs
suhailvs

Reputation: 21740

in gitk you can search by commit message:

enter image description here

You can install gitk by command sudo apt install gitk

Upvotes: -2

ratfury
ratfury

Reputation: 203

Just a small addition to the git log --all --grep command: In my case I had to escape the brackets inside the message:

git log --all --grep="\[C\] Ticket-1001: Fix nasty things"

Upvotes: 10

Rida Rezzag
Rida Rezzag

Reputation: 3963

first to list the commits use

git log --oneline

then find the SHA of the commit (Message), then I used

 git log --stat 8zad24d

(8zad24d) is the SHA assosiated with the commit you are intrested in (the first couples sha example (8zad24d) you can select 4 char or 6 or 8 or the entire sha) to find the right info

Upvotes: 9

Alex Howansky
Alex Howansky

Reputation: 53646

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

Upvotes: 112

Marvin Irwin
Marvin Irwin

Reputation: 1030

For anyone who wants to pass in arbitrary strings which are exact matches (And not worry about escaping regex special characters), git log takes a --fixed-strings option

git log --fixed-strings --grep "$SEARCH_TERM" 

Upvotes: 7

ryenus
ryenus

Reputation: 17441

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 :/keyword(s)

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

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.

Upvotes: 78

joseph
joseph

Reputation: 1008

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

git reflog

and then checkout the commit id

Upvotes: 3

Samarth S
Samarth S

Reputation: 335

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>

Upvotes: 9

simhumileco
simhumileco

Reputation: 34643

This:

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

or this:

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

commands work best for me.

Upvotes: 2

Noufal Ibrahim
Noufal Ibrahim

Reputation: 72855

git log --grep="Build 0051"

should do the trick

Upvotes: 36

Josh Lee
Josh Lee

Reputation: 177875

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

Upvotes: 31

Nic
Nic

Reputation: 13761

Try this!

git log | grep -b3 "Build 0051"

Upvotes: 18

Related Questions