Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Show all of the various changes to a single line in a specified file over the entire git history

Tags:

git

People also ask

Which command is used to view the history of all the changes to a file?

Using git log --follow -p bar will show the file's entire history, including any changes to the file when it was known as foo .

How can you view all the commits for a single file?

Use git log --all <filename> to view the commits influencing <filename> in all branches.


Since git 1.8.4, there is a more direct way to answer your question.

Assuming that line 110 is the line saying var identifier = "SOME_IDENTIFIER";, then do this:

git log -L110,110:/lib/client.js

This will return every commit which touched that line of code.

[Git Documentation (see the "-L" command line paramenter)]


See the man page for git-log and gitdiffcore. I believe this command would do it, but it might not be quite right:

git log -G "var identifier =" file.js

EDIT: Here's a rough start for a bash script to show the actual lines. This might be more what you're looking for.

for c in $(git log -G "something" --format=%H -- file.js); do
    git --no-pager grep -e "something" $c -- file.js
done

It uses git log -G to find the interesting commits, using --format=%H to produce a list of commit hashes. It then iterates over each interesting commit, asking git grep to show the lines from that commit and file that contain the regex, prefaced with the commit hash.


EDIT: Changed to use -G instead of -S as suggested in comments.


You can also do this with gitk:

gitk file.js

In the "commit" drop down, choose "adding/removing string:" and in the text box next to it, enter "var identifier =", and any commits that add or remove lines that contain that string will be highlighted.


If you adapt @rob's answer just a bit, git log will basically do this for you, if all you need is a visual comparison:

git log -U0 -S "var identifier =" path/to/file

-U0 means output in patch mode (-p), and show zero lines of context around the patch.

You can even do this across branches:

git log -U0 -S "var identifier =" branchname1 branchname2 -- path/to/file

There may be a way to suppress the diff header, but I don't know of one.


In magit, you can do this with

l, =L

It will then ask you for file and start,end lines.