92

I wondered if any of you knew of a tool that would allow me to select a line in my code and then view a list view of the history of that line on a commit-by-commit basis.

Does anyone know of such a tool?

Amin Shojaei
  • 5,451
  • 2
  • 38
  • 46
David.LPower
  • 1,083
  • 2
  • 10
  • 14

8 Answers8

99

Maybe annotations in IntelliJ IDEA is that you are looking for:

the left gutter with enabled annotations

Showing and hiding annotations

  1. Open the desired file in the editor.
  2. To show annotations, right-click the left gutter, and select Annotate:
    context menu
  3. To hide annotations, right-click the annotations gutter, and choose Close Annotations.
Community
  • 1
  • 1
Ilya Serbis
  • 21,149
  • 6
  • 87
  • 74
  • 1
    Watch this video to learn more on how you can benefit from using annotations: https://youtu.be/R8QW8s4Ibio – naXa stands with Ukraine Oct 03 '18 at 09:04
  • 5
    Would it be useful if I want to see the history for my selection? It just shows the last commit detail on that line, but what if I want to see the entire history of that line of code ? – Amit Jan 08 '19 at 16:16
59

I know only the IntelliJ IDEA "Viewing Changes History for Selection" feature.

You could also try to use several git blame commands to iterate over history of a fragment.

José Andias
  • 1,894
  • 2
  • 29
  • 31
kan
  • 28,279
  • 7
  • 71
  • 101
  • 1
    I use PHPStorm so I was able to use that 'show history for selection' Thank you for your help Kan. – David.LPower Mar 27 '13 at 14:50
  • 1
    Is this feature available only in license version of intellij? I am not able to see the detailed history for my selection. – Amit Jan 08 '19 at 16:13
53

If you would like to view inline such information then you may add GitToolBox plugin. Live example on YT

Example view: enter image description here

How to setup: enter image description here enter image description here

Gelldur
  • 11,187
  • 7
  • 57
  • 68
13

git blame (docs)

git-blame shows what revision and author last modified each line of a file.

Usage examples

When you are interested in finding the origin for lines 40-50 for file foo, you can use the -L option like so (they mean the same thing — both ask for 11 lines starting at line 40):

git blame -L 40,50 foo.txt
git blame -L 40,+11 foo.txt

You can specify a revision for git blame to look back starting from (instead of the default of HEAD) if you want to find out who edited that lines before a specific commit (fe25b6d in this example; fe25b6d^ is the parent of fe25b6d):

git blame -L 40,+11 fe25b6d^ -- foo.txt
Community
  • 1
  • 1
naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
8

git log (docs)

git-log shows commit logs.

Usage example

You can specify -L option to trace the evolution of the line range given by ",". You can specify this option more than once.

git log -L 40,50:foo.txt
Community
  • 1
  • 1
naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
7

If you are using IntelliJ then, its annotation feature provides an option to do the annotation on previous revision. Using this option you can go back to the history of that line.

Find below screen-shot which shows, This option and its available in the community edition as well.

enter image description here

Amit
  • 30,756
  • 6
  • 57
  • 88
5

In IntelliJ, you can use show history for selection in the git submenu after selecting a line/ multiple lines.

enter image description here

Vishrant
  • 15,456
  • 11
  • 71
  • 120
4

As suggested in one of the comments in Can Git show history for selected lines?

git show $(git blame example.js -L 250,260 | awk '{print $1}')

more info: Every line of code is always documented.

Community
  • 1
  • 1
Snger
  • 1,374
  • 19
  • 23