If I found a bug in my application, sometimes I need to know which commits have affected to the bug source code line. I'm wondering which is the best approach to do it with Git.
            Asked
            
        
        
            Active
            
        
            Viewed 3.3k times
        
    5 Answers
58
            To see commits affecting line 40 of file foo:
git blame -L 40,+1 foo
The +1 means exactly one line. To see changes for lines 40-60, it's:
git blame -L 40,+21 foo
OR
git blame -L 40,60 foo
The second number can be an offset designated with a '+', or a line number. git blame docs
 
    
    
        ahaurat
        
- 3,947
- 4
- 27
- 22
- 
                    How can I get the entire line history, not only the last one? – Pedro Apr 14 '21 at 22:26
- 
                    1@Pedro you probably want `git log -L`. See this answer for more info: https://stackoverflow.com/a/19757493/2356383 – ahaurat Apr 14 '21 at 23:25
42
            
            
        I'd use the git blame command. That's pretty much exactly what it is for. The documentation should get you started.
 
    
    
        vcsjones
        
- 138,677
- 31
- 291
- 286
- 
                    1will this give me the entire line history or only the last one? What if the line has be modified more than once and i want to check the changes each time the line has been modified. – Yash Kalwani Sep 20 '19 at 07:17
9
            
            
        If you only need the last change:
git blame
Otherwise, you could try to automatically find the offending change with
git bisect
 
    
    
        Frank Schmitt
        
- 30,195
- 12
- 73
- 107
- 
                    1+1 for bisect. Good for trying to figure out which commit broke something without knowning exactly what is wrong. – vcsjones May 26 '11 at 19:24
 
     
    