Using path wildcards in git log
Asked Answered
L

3

13

I have a file down deep in my git tree:

$ git ls-files | grep /Expression.java
sm/src/main/java/cl/utilities/sm/Expression.java

I'd like to get a log of its activity without having to type the whole path. Basically I want this output:

$ git log --oneline -2 sm/src/main/java/cl/utilities/sm/Expression.java
2718cdc cleaned up some warnings
f30cf15 Added missing @Overrides

... but without having to type sm/src/main/java/cl/utilities/sm. I tried lots of things, but none of them worked:

$ git log -- \*/Expression.java
$ git log -- \*Expression.java
$ git log -- \*\*/Expression.java
$ git log -- '*/Expression.java'
$ git log -- '**/Expression.java'
Lucylud answered 22/3, 2012 at 14:8 Comment(2)
git log -- */Expression.java should work, weirdAlex
In my Windows 7 command shell git log "*/Expression.java" works, but single quotes do not work.Mazel
M
8

Use a wildcard, no escapes or quotes required:

git log -- */Expression.java

Tested on Windows 7 in cmd shell and git bash.

Depending on your shell, you may need quotes -- if single quotes don't work, try double quotes.

Mazel answered 2/5, 2014 at 17:48 Comment(1)
Confirmed that this works for me now. It looks like I was the victim of a bug present in a few versions of git available around this time. The fix, as mentioned in git-blame.blogspot.com/2012/01/git-1776-1784-and-179-rc2.html, is github.com/git/git/commit/5c8eeb8. It first showed up in 1.7.7.6, released just a couple of months before I asked my question.Lucylud
M
3

use xargs:

find . -name 'Expression.java' | xargs git log --oneline -2
Maxillary answered 22/3, 2012 at 14:16 Comment(1)
That assumes the file exists locally. What if you want to log a file that has been deleted?Mazel
P
3

With git 2.8 (March 2016), wildcards are more firmly supported both as pathspec or refspec.

See commit aac4fac, commit df714f8, commit 1cc777d (10 Feb 2016) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit e6a6a76, 24 Feb 2016)

That means that:

  • wilcard works with pathspecs:

      git log -- "*.t"
      # or
      git log    "*.t"
    
  • wildcard works with refspecs (when searching for a commit message starting with 'b' for instance):

      git log "HEAD^{/b.*}" --
      # or
      git log "HEAD^{/b.*} 
    
Precarious answered 25/2, 2016 at 8:37 Comment(2)
would you mind linking to docs or explaining what exactly the refspecs format does here?Ligetti
@Ligetti Sure thing. I have edited the answer to add the requested link.Precarious

© 2022 - 2024 — McMap. All rights reserved.