Tag: blame

How to find when a line was last changed in Visual Studio – 080

Previously, I mentioned how to use Blame on GitHub.com to find the commit that changed a specific line. Today we’re going to rinse and repeat this scenario within Visual Studio.

I told think anyone ever realized how scared I was of the world (if not plain neurotic) as a small child. I remember once in first grade (6 years old) being asked by the teacher for the answer to a multiple choice question. I thought it was ‘D’ but the answer was ‘C’. When the teacher said no that was wrong, I broke down sobbing from fear and embarrassment. I didn’t know what was going to happen to me for getting it wrong. I thought perhaps I’d get into serious trouble when I got home or lose my recess or something. Unfortunately, the teacher completely misread why I was crying (she thought I was acting out for attention) and scolded me, etc., which was my first foray into the self-fulfilling prophecy*.

I share this story because the first time I saw “Blame” on GitHub, I felt the same emotion as I did as a little kid. Why would I ever want to put myself into a position of having people call me out publicly via my code and blame me? But as I said in my previous post, Blame doesn’t mean “to imply blame” but rather “look up the commit that changed this line of code.”

I was happy to see this command referred to as Annotate in Visual Studio 2015, but a bit saddened to see now called Blame (Annotate) in Visual Studio 2017. I get it. I get it. When in Rome… but I often muse what would be the equivalent of commercial airline pilots who have to reskill like we do in the software industry. Would they start lowering the landing gear prior to take off? Would they refer to Air Traffic Controller as Air Traffic View or Air Traffic Model? (see what I did there? #PointsMe!)

In today’s scenario, I want to know two things:

  1. When was a specific line of code changed? as in, which commit changed a given line of code?
  2. What changed in that line?

1.  open the file that contains the line in question (either via Double-Click in Solution Explorer or File – Open – File provided you are connected to the repo in Team Explorer)

2. anywhere in the file, right-click and select Source Control – Blame (Annotate)

Source Control - Blame (Annotate) in context menu for file

The Blame / Annotate window will appear on the left hand side. This window shows for each line what was the last commit that modified it.

Annotated window shown for a given file

In the above example, lines 1-2 were last modified by commit ID 44af5057 (most likely my initial commit). But line 3, the one I’m interested in, was last modified in commit fd80ad89.

As any long time Visual Studio user will do… let’s click the fd80ad89 and see what happens!

Commit details for the last commit that modified line 3

Seeing the Commit Details appear in Team Explorer answers the part one of my scenario – the when. Now it is time to figure out the what – what was changed in the line…

TE - Commit Details - Changes - main.js - Compare with Previous

By clicking on the Compare with Previous… command in the context menu for the main.js file listed in the Changes list for that commit, you’ll see the file diff view appear.

"Tray" shown added to list of const

And now we  have “the what” for what has changed in the file. The “Tray” const was added to the list, giving it a type of Electron.Tray, so I could put the app in the Taskbar tray (or Mac tray thingy at top of screen).

*Sometimes I wonder if I’m the reincarnation of Rod Serling (technically, he died before I was born) or a zen master looking for a challenge. But then again, I’ve always hated smoking, so I’m not the former. And during the Visual Studio 2017 Launch Event when they were showing my Happy Birthday Visual Studio video, I was at home yelling at the Yoga DVD Instructor over the “share  your breath with the community” breathing stuff, so it can’t be the later either.

How to use Blame on GitHub to find the commit that changed that line or block of code – 059

First time I saw the Blame button, I was scared to put any of my code up on GitHub. I thought, “Why would I want people to publicly blame me?” Fortunately, the Blame functionality has absolutely nothing to do with its connotation.

The Blame button is more of a “Show the commit when this line or block of code was changed.” That’s it. With no blame implied! 🙂

For example, you can look at the 3rd line of the main.js file for my simple electron app.

Blame button for main.js

The timeline of events is as follows:

  1. at some time 7 months ago, I made a commit “added close button” that changed the 3rd line of code. I added “ipcMain” to the constants.
  2. shortly thereafter, about 7 months ago, I made a commit “appears in tray” that changed the 3rd line of code. I added “Tray” to the constants.

The blame button allows you to select a given line of code, e.g. the 3rd line in my example, and work your way backwards up the commit events…

In the image below, you’ll see the first line was added in my initial commit. Then I’ve made some modifications to the 3rd line. But the 4th line (some whitespace) has remained the same since the initial comment. But the 5th line was added alongside the changes to the 3rd line, and so on and so forth…

"appears in tray" commit for 3rd line of code

You have two options to proceed. You can either choose to click the “appears in tray” link to see the diff in the usual commit diff view,

diff showing Tray being added to list of consts

or you can click the box-looking button to the right of the 7 months ago commit date.

View blame prior to this change button

Clicking on the View blame prior to this change now shows you the file before the “added close button” commit was added. Why this commit? Because this “added close button” commit was the last time line #3 was changed after the “appears in tray” commit. You’re just walking back up the log for a given line of code.


A few things to notice:

  • the “appears in tray” commit has changed to “added close button” commit
  • line 3 only contains consts for app, BrowserWindow, and ipcMain
  • line 5 is different, because we’re looking at the history of the file when “added close button” was committed

Let’s go one more click up the chain.

View blame prior to this change for "added close button" 

Clicking on the View blame prior to this change for the “added close button” now shows the original file at the time of its initial commit.

Initial commit to the main.js file

You’ve now just walked all the way up the chain of changes to a given line of code.

Perhaps Pedigree is a better term than Blame? E.g. what’s the pedigree of this line of code? I only offer the word pedigree so the button icon could be a