Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you do a `git blame` from the command line on one line of code that I am interested in?

Essentially what I am trying to do is that same thing we usually do using the web interface. I know how to do git blame using the web interface say bitbucket or github, but how do you do the same thing using git CLI ? So far I have got thus far:

  1. git grep <line of code that I am interested to see the blame>. Get a list of files then find the one that I am interested in.
  2. git blame <on file of interest>. Shows who did what and commit hash.
  3. Can't figure this part out ... I see the hash I am interested in, how do I see the full commit log on just that one commit hash ?
like image 565
Muhammad Lukman Low Avatar asked Mar 23 '15 06:03

Muhammad Lukman Low


People also ask

How do I blame a specific line in git?

Blaming only a limited line range Sometimes, You don't need to blame the entire file, you just need to blame in a limited line range. Git blame provides the option for that. -L will take the option for the start line and for the end line. The above command will blame from line 80 through line 100.

How do I git blame on GitHub?

On GitHub.com, navigate to the main page of the repository. Click to open the file whose line history you want to view. In the upper-right corner of the file view, click Blame to open the blame view.

Can you use git from command line?

To use Git on the command line, you will need to download, install, and configure Git on your computer. You can also install GitHub CLI to use GitHub from the command line.


1 Answers

I stumbled on this http://zsoltfabok.com/blog/2012/02/git-blame-line-history/ after I posted this, so I am going to post this here for reference. The steps are:

  1. Find the line number you are interested to find blame on and do git blame -L <starting line number>,<ending line number> <filename>. (i.e. line numbers are comma-separated, no space)
  2. git show <commit hash> will show you what you need to know.
like image 88
Muhammad Lukman Low Avatar answered Nov 15 '22 20:11

Muhammad Lukman Low