Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git log for a portion of a file

Tags:

git

I would like to see all the git commits that affected what are currently lines x - y in one of my files. Is there a way I can do that?

like image 485
JonDrnek Avatar asked May 05 '11 13:05

JonDrnek


People also ask

How do I limit a git log?

We can limit the git log output by including the -<n> option. This enables us to specify how many log messages are printed in the terminal. We can add the --oneline flag to show each commit on one line.

How do I view files in git log?

To find out which files changed in a given commit, use the git log --raw command. It's the fastest and simplest way to get insight into which files a commit affects.

How can we locate a particular git commit?

Finding a Git commit by checksum, size, or exact file One of the “main” files in the repository that changes often is your best bet for this. You can ask the user for the size, or just a checksum of the file, and then see which repository commits have a matching entry.


2 Answers

Now git log supports the -L option like git blame. This was added in Git v1.8.4.

 -L <start>,<end>:<file>
 -L :<regex>:<file>

 Trace the evolution of the line range given by "<start>,<end>" (or
 the funcname regex <regex>) within the <file>. You may not give any
 pathspec limiters. This is currently limited to a walk starting from a
 single revision, i.e., you may only give zero or one positive revision
 arguments. You can specify this option more than once.

(git-log documentation)

like image 116
Colin D Bennett Avatar answered Oct 21 '22 01:10

Colin D Bennett


You can use git blame with the -L option:

-L <start>,<end> 

 Annotate only the given line range. <start> and <end> can take one of these forms: 

 number 

If <start> or <end> is a number, it specifies an absolute line number (lines count from 1).

 /regex/ 

This form will use the first line matching the given POSIX regex. If <end> is a regex, it will search starting at the line given by <start>.

 +offset or -offset 

This is only valid for <end> and will specify a number of lines before or after the line given by <start>.

So it will be something like below:

git blame -L 40,60 foobar

Note that git blame shows the latest revision for each line. You can also try with the --reverse option:

--reverse

Walk history forward instead of backward. Instead of showing the revision in which a line appeared, this shows the last revision in which a line has existed. This requires a range of revision like START..END where the path to blame exists in START.

http://www.kernel.org/pub/software/scm/git/docs/git-blame.html

You can probably also use

gitk foobar
like image 30
manojlds Avatar answered Oct 21 '22 01:10

manojlds