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?
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.
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.
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 .
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?)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With