Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve the commit log for a specific line in a file?

Tags:

git

Is there any way to get git to give you a commit log for just commits that touched a particular line in a file?

Like git blame, but git blame will show you the LAST commit that touched a particular line.

I'd really like to get a similar log of, not the list of commits to anywhere in the file, but just the commits that touched a particular line.

like image 918
jrochkind Avatar asked Dec 08 '11 17:12

jrochkind


People also ask

How do I see commits in a specific file?

On Linux you can use gitk for this. It can be installed using "sudo apt-get install git-gui gitk". It can be used to see commits of a specific file by "gitk <Filename>".

How do I see the commit history of a branch?

On GitHub.com, you can access your project history by selecting the commit button from the code tab on your project. Locally, you can use git log . The git log command enables you to display a list of all of the commits on your current branch. By default, the git log command presents a lot of information all at once.

How do you commit one line?

In the Commit window, select the file you want to partially commit, then select the text you want to commit in the right pane, then right-click on the selection and choose 'Stage selected lines' from the context menu.


2 Answers

See also Git: discover which commits ever touched a range of lines.


Since Git 1.8.4, git log has -L to view the evolution of a range of lines.

For example, suppose you look at git blame's output. Here -L 150,+11 means "only look at the lines 150 to 150+11":

$ git blame -L 150,+11 -- git-web--browse.sh a180055a git-web--browse.sh (Giuseppe Bilotta 2010-12-03 17:47:36 +0100 150)            die "The browser $browser is not a180055a git-web--browse.sh (Giuseppe Bilotta 2010-12-03 17:47:36 +0100 151)    fi 5d6491c7 git-browse-help.sh (Christian Couder 2007-12-02 06:07:55 +0100 152) fi 5d6491c7 git-browse-help.sh (Christian Couder 2007-12-02 06:07:55 +0100 153)  5d6491c7 git-browse-help.sh (Christian Couder 2007-12-02 06:07:55 +0100 154) case "$browser" in 81f42f11 git-web--browse.sh (Giuseppe Bilotta 2010-12-03 17:47:38 +0100 155) firefox|iceweasel|seamonkey|iceape) 5d6491c7 git-browse-help.sh (Christian Couder 2007-12-02 06:07:55 +0100 156)    # Check version because firefox < 2.0 do 5d6491c7 git-browse-help.sh (Christian Couder 2007-12-02 06:07:55 +0100 157)    vers=$(expr "$($browser_path -version)"  5d6491c7 git-browse-help.sh (Christian Couder 2007-12-02 06:07:55 +0100 158)    NEWTAB='-new-tab' 5d6491c7 git-browse-help.sh (Christian Couder 2007-12-02 06:07:55 +0100 159)    test "$vers" -lt 2 && NEWTAB='' a0685a4f git-web--browse.sh (Dmitry Potapov   2008-02-09 23:22:22 -0800 160)    "$browser_path" $NEWTAB "$@" & 

And you want to know the history of what is now line 155.

Then, use git log. Here, -L 155,155:git-web--browse.sh means "trace the evolution of lines 155 to 155 in the file named git-web--browse.sh".

$ git log --pretty=short -u -L 155,155:git-web--browse.sh commit 81f42f11496b9117273939c98d270af273c8a463 Author: Giuseppe Bilotta <[email protected]>      web--browse: support opera, seamonkey and elinks  diff --git a/git-web--browse.sh b/git-web--browse.sh --- a/git-web--browse.sh +++ b/git-web--browse.sh @@ -143,1 +143,1 @@ -firefox|iceweasel) +firefox|iceweasel|seamonkey|iceape)  commit a180055a47c6793eaaba6289f623cff32644215b Author: Giuseppe Bilotta <[email protected]>      web--browse: coding style  diff --git a/git-web--browse.sh b/git-web--browse.sh --- a/git-web--browse.sh +++ b/git-web--browse.sh @@ -142,1 +142,1 @@ -    firefox|iceweasel) +firefox|iceweasel)  commit 5884f1fe96b33d9666a78e660042b1e3e5f9f4d9 Author: Christian Couder <[email protected]>      Rename 'git-help--browse.sh' to 'git-web--browse.sh'.  diff --git a/git-web--browse.sh b/git-web--browse.sh --- /dev/null +++ b/git-web--browse.sh @@ -0,0 +127,1 @@ +    firefox|iceweasel) 
like image 150
Matt McClure Avatar answered Oct 06 '22 18:10

Matt McClure


You can get a set of commits by using pick-axe.

git log -S'the line from your file' -- path/to/your/file.txt 

This will give you all of the commits that affected that text in that file. If the file was renamed at some point, you can add --follow-parent.

If you would like to inspect the commits at each of these edits, you can pipe that result to git show:

git log ... | xargs -n 1 git show 
like image 44
Adam Dymitruk Avatar answered Oct 06 '22 18:10

Adam Dymitruk