Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all the versions of a given line number in the GIT history [duplicate]

Tags:

Is it possible in Git to list all the previous versions of a given line in a file by the line number?

The reason why I would find it useful is to be able to easier troubleshoot a problem based on logged stack trace report.

i.e. I have a undefined method exception logged at a line 100 of a given file. The file is included in plenty of commits which caused the given line might have 'traveled' up and down the file even without any changes made to it.

How would I print out the content of line 100 of a given file across last x commits?

like image 448
mrt Avatar asked Jul 22 '13 10:07

mrt


People also ask

Does GitHub save past versions?

Key Points. The GitHub website will show a list of changes, and show the differences between commits. We can download or copy from old versions of files.

How do I see my git history in GitHub?

Click History. On the History tab, click the commit you'd like to review. If there are multiple files in the commit, click on an individual file to see the changes made to that file in that commit.

How can I see the diff of a commit?

To see the diff for a particular COMMIT hash, where COMMIT is the hash of the commit: git diff COMMIT~ COMMIT will show you the difference between that COMMIT 's ancestor and the COMMIT .


1 Answers

This will call git blame for every meaningful revision to show line $LINE of file $FILE:

git log --format=format:%H $FILE | xargs -L 1 git blame $FILE -L $LINE,$LINE 

As usual, the blame shows the revision number in the beginning of each line. You can append

| sort | uniq -c 

to get aggregated results, something like a list of commits that changed this line. (Not quite, if code only has been moved around, this might show the same commit ID twice for different contents of the line. For a more detailed analysis you'd have to do a lagged comparison of the git blame results for adjacent commits. Anyone?)

like image 89
krlmlr Avatar answered Sep 21 '22 11:09

krlmlr