0

I quite frequently do git log -p looking for a line of code change, usually a removal. This opens less, and then I do a /search for what I'm looking for. This does work. However once I've found it I sometimes want to see the commit log for this file, usually to determine when. This usually involves a lot of scrolling due to many file commits. Is there a good way to stop scrolling, and just jump directly to the commit?

xenoterracide
  • 16,274
  • 24
  • 118
  • 243
  • why not using one of many graphical front-ends? My favorites: `git extenstions` (windows only), `SourceTree` (sucks on Windows), `GitKraken`. – Marek R Jan 20 '21 at 21:21
  • @MarekR in my current working environment I am not allowed to install unapproved software. I am looking for a new job. I don't know if I can use source tree, etc. – xenoterracide Jan 20 '21 at 21:28
  • How about `gitk`? it is usually shipped with `git` as default graphical front-end, so you may already have it. Its ugly and a bit strange UI flow, but it is turbo fast. – Marek R Jan 20 '21 at 21:32
  • @MarekR yeah probably. I'm just a terminal monkey – xenoterracide Jan 20 '21 at 21:33
  • I love `ungit`. It's a lovely way to visualise git commit trees. – evolutionxbox Jan 20 '21 at 22:03

2 Answers2

1

To find which file the hit's in is just ?^diff; then the search string to find the commit header is ?^commit , with that trailing space, if you're using the default header format, or ?^[a-f0-9]{5} if you're using --oneline

Apparently mercurial has a diff scraper that condenses the hits and metadata to one line each, I wrote an equivalent as a simple lex for fun.

jthill
  • 55,082
  • 5
  • 77
  • 137
1

If you are looking for commit which removed specific line there is better way to do it.

Using git log:

git log -S <string which was added/removed> path/to/file

Using git blame:

git blame --reverse START..END filename
Marek R
  • 32,568
  • 6
  • 55
  • 140
  • 1
    Also: `git log -G`. While `-S` and `-G` differ in several ways (in particular one defaults to simple string and one to regular expression), the *big* difference is that `-G` looks at the diff to see if it *contains* something, while `-S` looks at the diff to see if the *number of somethings changes*. For instance, suppose we have diff that changes the spelling of a word *W* that comes before a second word *X* that's either added or removed. `git log -S X` will find the diff because it adds/removes X. `git log -G X` will find the diff too. But `git log -S W` won't, while `git log -G W` will. – torek Jan 20 '21 at 22:25