简体   繁体   中英

git log for a single line -L without showing diff or patch?

I'm trying to figure out at which point a line in a file was introduced and in which commits it was changed.

I can view the history of those lines (line 10 in the file example.txt):

git log -L 10,11:example.txt

However, for each log entry it includes a 'diff' or a 'patch' (I'm not really sure of the difference yet) as if I'd run:

git log -p

This makes the output more difficult to read - I just want to view a list of commit messages and their ids for commits that changed the line(s) I'm interested in.

For example, ordinarily git logs can be simplified by using the --pretty formats, enabling us to read through a clean list of commit messages to find the info we're interested in. For example, I use the --oneline option frequently:

git log --oneline

That however shows the full log - we want to filter this to only show commits that changed lines of interest. Something like the following would be great but the pretty oneline format doesn't seem to work in combination with the -L flag:

git log --oneline -L 10,11:example.txt

I've tried various combinations of options and arguments from the git log documentation but haven't found anything that works.

Does anyone else face this issue or have any ideas of what could work?

Thanks for reading this far

As mentioned by VonC here ,

git log -s -L <start>,<end>:file
# and 
git log -s -L :<funcname>:file

should both work to suppress the patch output with Git >= 2.22.0. This was added in commit 9f607cd09c (line-log: suppress diff output with "-s", 2019-03-07) .

Also, you mention that

git log --oneline -L 10,11:example.txt

does not work. --oneline should only apply to the commit header, and not the diff output ( --oneline -p would show the commit information on one line and then the diff ouptut).

I checked with Git 2.13.3 (released July 12 2017, so around the time you posted your question) and git log --oneline -L 10,11:example.txt does work (it does however output the full patch).

a couple of verbose solutions are to use the normal git log -L command to output the log for the lines of interest then pipe the output to grep or sed to print only the parts of the commit message you're interested in. Try which ever version you prefer. Would love to hear if anyone has a less verbose solution!

grep:

git log -L 10,11:example.txt | grep 'commit \w' -A 4

The grep command matches the first line of each log entry and the prints the next 4 lines with the -A flag

sed:

git log -L 10,11:example.txt | sed -n '/commit/{p;n;n;n;n;p}' 

when it finds a match, prints the line via the p command, skips the next lines via the n command then prints the next line using p. The leading -n option prevents all other printing. found thanks to this answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM