Find code changes and other history with CodeLens - Visual Studio (Windows) (2023)

  • Article

Applies to: Find code changes and other history with CodeLens - Visual Studio (Windows) (1)Visual Studio Find code changes and other history with CodeLens - Visual Studio (Windows) (2)Visual Studio for Mac Find code changes and other history with CodeLens - Visual Studio (Windows) (3)Visual Studio Code

With CodeLens, you can stay focused on your work while you find out what happened to your code–all without leaving the editor. You can find references to a piece of code, changes to your code, linked bugs, Azure DevOps work items, code reviews, and unit tests.

Note

CodeLens is available in the Community edition of Visual Studio; however, the source control indicators are not available in this edition.

CodeLens can show you both where and how the individual parts of your code are used in your solution.

Find code changes and other history with CodeLens - Visual Studio (Windows) (4)

To choose the indicators that you want to see, or to turn CodeLens off and on, go to Tools > Options > Text Editor > All Languages > CodeLens.

Find references to your code

You can find references in C# or Visual Basic code. Here's how.

  1. In the code editor, select the references indicator or press Alt+2.

    Note

    If the indicator shows 0 references, you don't have any references from your C# or Visual Basic code. However, there might be references in other items such as .xaml and .aspx files.

  2. To view the referencing code, mouse over the reference in the list.

  3. To open the file that contains the reference, double-click the reference.

Code maps

To see relationships between the code and its references, create a code map. In the code map shortcut menu, select Show All References.

Find changes in your code

Inspect your code's history to find out what happened to your code. Or, review changes before they're merged into your code so you can better understand how changes in other branches might affect your code.

You need:

  • Visual Studio Enterprise or Visual Studio Professional

  • Git or Azure DevOps Server (formerly Team Foundation Server) or Team Foundation Version Control

For C# or Visual Basic code that's stored with Team Foundation Version Control (TFVC) or Git, you get CodeLens details at the class and method levels (code element-level indicators).

For file types other than .cs or .vb, you get CodeLens details for the entire file in one place (file-level indicators).

Code element-level indicators

Code element-level indicators let you see who changed your code and what changes they made. Code element-level indicators are available for C# and Visual Basic code.

You can also view change history when you use Team Foundation Version Control (TFVC) in Azure DevOps Services.

The default time period is the last 12 months.

To see a detailed history of all the changes, including those changes from more than a year ago, select Show all file changes, which opens a History window.

From the History window, you can view all your files that are in a Git repository. You can then select the code element-level changes indicator for more details.

File-level indicators

You can also view the file-level indicators for an entire file.

Note

File-level indicators are not available for C# and Visual Basic files.

To get more details about a change, right-click the item. Depending on whether you're using TFVC or Git, there are options to compare the versions of the file, view details and track the changeset, get the selected version of the file, and email the author of that change. Some of these details appear in Team Explorer if you have it enabled; otherwise, these details appear in Git Changes.

You can also see who changed your code over time, which can help you find patterns in your team's changes and assess their impact.

Find changes in your current branch

Your team might have multiple branches, for example a main branch and a child development branch, to reduce the risk of breaking stable code. You can find out how many people changed your code and how many changes were made in the main branch by pressing Alt+6.

Find when your code was branched

To find when your code was branched, navigate to your code in the child branch. Then, select the changes indicator (or press Alt+6).

Find incoming changes from other branches

You can view incoming changes. And, you don't need to leave your current branch to review a change.

Find when changes got merged

You can see when changes got merged, so you can determine which changes are included in your branch.

Compare an incoming change with your local version

To compare an incoming change with your local version, double-click the changeset or press Shift+F10.

Branch icons

The icon in the Branch column tells you how the branch is related to the branch you're working in.

IconThe change came from:
Find code changes and other history with CodeLens - Visual Studio (Windows) (5)The current branch
Find code changes and other history with CodeLens - Visual Studio (Windows) (6)The parent branch
Find code changes and other history with CodeLens - Visual Studio (Windows) (7)A child branch
Find code changes and other history with CodeLens - Visual Studio (Windows) (8)A peer branch
Find code changes and other history with CodeLens - Visual Studio (Windows) (9)A branch further away than a parent, child, or peer
Find code changes and other history with CodeLens - Visual Studio (Windows) (10)A merge from the parent branch to a child branch
Find code changes and other history with CodeLens - Visual Studio (Windows) (11)A merge from a child branch to the parent branch
Find code changes and other history with CodeLens - Visual Studio (Windows) (12)A merge from an unrelated branch (baseless merge)

Linked work items

Find linked work items by selecting the work items indicator or by pressing Alt+8.

Linked code reviews

Find linked code reviews by selecting the reviews indicator. To use the keyboard, hold down the Alt key and then press Left arrow or Right arrow to navigate the indicator options.

Linked bugs

Find linked bugs by selecting the bugs indicator or by pressing Alt+7.

Find the author of an item by selecting the authors indicator or by pressing Alt+5.

You can also open the shortcut menu for an item to see the contact options. (If you have Teams installed, you have additional communication options available to you.)

Associated unit tests

You can discover unit tests that exist for your C# or Visual Basic code without opening Test Explorer.

  1. Go to application code that has associated unit test code.

  2. If you haven't already, build your application to load the CodeLens test indicators.

  3. Review the tests for the code by pressing Alt+3.

  4. If you see a warning icon Find code changes and other history with CodeLens - Visual Studio (Windows) (13), the tests haven't run yet, so run them.

  5. To review a test's definition, double-click the test item in the CodeLens indicator window to open the code file in the editor.

  6. To review the test's results, choose the test status indicator (Find code changes and other history with CodeLens - Visual Studio (Windows) (14) or Find code changes and other history with CodeLens - Visual Studio (Windows) (15)) or press Alt+1.

  7. To see how many people changed this test, who changed this test, or how many changes were made to this test, find your code's history and linked items.

Keyboard shortcuts

To use the keyboard to select indicators, press and hold the Alt key to display the related number keys, then press the number that corresponds to the indicator you want to select.

Note

To select the reviews indicator, hold down Alt while using the left and right arrow keys to navigate.

Q & A

Q: How do I turn CodeLens off or on or choose which indicators to see?

A: You can turn indicators off or on, except for the references indicator. Go to Tools > Options > Text Editor > All Languages > CodeLens.

When the indicators are turned on, you can also open the CodeLens options from the indicators. And, you can turn CodeLens file-level indicators on and off by using the chevron icons at the bottom of the editor window.

Q: Where is CodeLens?

A: CodeLens appears in C# and Visual Basic code at the method, class, indexer, and property level. CodeLens appears at the file level for all other types of files.

  • Make sure CodeLens is turned on. Go to Tools > Options > Text Editor > All Languages > CodeLens.

  • Azure DevOps-related indicators appear only when work items are linked to the code and when you have permissions to open linked work items. Confirm that you have team member permissions.

  • Unit test indicators don't appear when application code doesn't have unit tests. Test status indicators appear automatically in test projects. If you know that your application code has unit tests, but the test indicators don't appear, try building the solution (Ctrl+Shift+B).

Tip

CodeLens is available in Visual Studio Community edition, however, the source control indicators are not available in this edition.

Q: Why don't I see the work item details for a commit?

A: This might happen because CodeLens can't find the work items in Azure Boards. Check that you're connected to the project that has those work items, and that you have permissions to see those work items. Work item details might also not show if the commit description has incorrect information about the work item IDs in Azure Boards.

Q: How do I change the font and color for CodeLens?

A: Go to Tools > Options > Environment > Fonts and Colors.

Find code changes and other history with CodeLens - Visual Studio (Windows) (16)

To use the keyboard:

  1. Press Alt+T+O to open the Options dialog box.

  2. Press Up Arrow or Down Arrow to go to the Environment node, then press Left Arrow to expand the node.

  3. Press Down Arrow to go to Fonts and Colors.

  4. Press Tab to go to the Show settings for list, and then press Down Arrow to select CodeLens.

Q: Can I move the CodeLens heads-up display?

A: Yes, choose Find code changes and other history with CodeLens - Visual Studio (Windows) (17) to dock CodeLens as a window.

Q: How do I refresh the indicators?

A: This depends on the indicator:

  • References: This indicator updates automatically when the code changes. If the References indicator is docked as a separate window, refresh the indicator by selecting Refresh.

  • Team: Refresh these indicators by selecting Refresh CodeLens Team Indicators from the right-click menu.

  • Test: Find unit tests for your code to refresh the Test indicator.

Q: What's "Local Version"?

A: The Local Version arrow points at the most recent changeset in your local version of a file. When the server has more recent changesets, they appear above or below the Local Version arrow, depending on the order used to sort the changesets.

Q: My CodeLens test indicators no longer appear in my file when I first open my solution. How can I load them?

A: Rebuild your project to get CodeLens test indicators to load in your file. To improve performance, Visual Studio no longer fetches source information for test indicators when code files are loaded. Test indicators are loaded after a build, or when you navigate to a test by double-clicking on it in Test Explorer.

CodeLens lets you stay focused on your work while you find out what happened to your code–without leaving the editor. You can find references to a piece of code, changes to your code, linked bugs, work items, code reviews, and unit tests.

Note

CodeLens is available in Visual Studio Community edition, however, the source control indicators are not available in this edition.

See where and how the individual parts of your code are used in your solution:

Find code changes and other history with CodeLens - Visual Studio (Windows) (18)

Contact your team about changes to your code without leaving the editor:

Find code changes and other history with CodeLens - Visual Studio (Windows) (19)

To choose the indicators that you want to see, or to turn CodeLens off and on, go to Tools > Options > Text Editor > All Languages > CodeLens.

Find references to your code in Visual Studio 2019

You can find references in C# or Visual Basic code.

  1. Choose the references indicator or press Alt+2.

    Find code changes and other history with CodeLens - Visual Studio (Windows) (20)

    Note

    If the indicator shows 0 references, you have no references from C# or Visual Basic code. However, there may be references in other items such as .xaml and .aspx files.

  2. To view the referencing code, mouse over the reference in the list.

    Find code changes and other history with CodeLens - Visual Studio (Windows) (21)

  3. To open the file that contains the reference, double-click the reference.

Code maps

To see relationships between the code and its references, create a code map. In the code map shortcut menu, select Show All References.

Find code changes and other history with CodeLens - Visual Studio (Windows) (22)

Find changes in your code in Visual Studio 2019

Inspect your code's history to find out what happened to your code. Or, review changes before they're merged into your code so you can better understand how changes in other branches might affect your code.

You need:

  • Visual Studio Enterprise or Visual Studio Professional

  • Git or Azure DevOps Server (formerly Team Foundation Server) or Team Foundation Version Control

For C# or Visual Basic code that's stored with Team Foundation Version Control (TFVC) or Git, you get CodeLens details at the class and method levels (code element-level indicators).

Find code changes and other history with CodeLens - Visual Studio (Windows) (23)

For file types other than .cs or .vb, you get CodeLens details for the entire file in one place at the bottom of the window (file-level indicators).

Find code changes and other history with CodeLens - Visual Studio (Windows) (24)

Code element-level indicators

Code element-level indicators let you see who changed your code and what changes they made. Code element-level indicators are available for C# and Visual Basic code.

This is what you see when you use Team Foundation Version Control (TFVC) or Azure DevOps Services:

Find code changes and other history with CodeLens - Visual Studio (Windows) (25)

The default time period is the last 12 months.

To see a detailed history of all the changes, including those changes from more than a year ago, choose Show all file changes:

Find code changes and other history with CodeLens - Visual Studio (Windows) (26)

The History window opens:

Find code changes and other history with CodeLens - Visual Studio (Windows) (27)

When your files are in a Git repository and you choose the code element-level changes indicator, this is what you see:

Find code changes and other history with CodeLens - Visual Studio (Windows) (28)

File-level indicators

Find changes for an entire file in the file-level indicators at the bottom of the window:

Find code changes and other history with CodeLens - Visual Studio (Windows) (29)

Note

File-level indicators are not available for C# and Visual Basic files.

To get more details about a change, right-click that item. Depending on whether you're using TFVC or Git, there are options to compare the versions of the file, view details and track the changeset, get the selected version of the file, and email the author of that change. Some of these details appear in Team Explorer in Visual Studio 2019 version 16.7 and earlier. In Visual Studio 2019 version 16.8 and later, these details appear in Git Changes.

You can also see who changed your code over time, which can help you find patterns in your team's changes and assess their impact.

Find code changes and other history with CodeLens - Visual Studio (Windows) (30)

Find changes in your current branch

Your team might have multiple branches, for example a main branch and a child development branch, to reduce the risk of breaking stable code.

Find code changes and other history with CodeLens - Visual Studio (Windows) (31)

You can find out how many people changed your code and how many changes were made in the main branch by pressing Alt+6:

Find code changes and other history with CodeLens - Visual Studio (Windows) (32)

Find when your code was branched

To find when your code was branched, navigate to your code in the child branch. Then, select the changes indicator (or press Alt+6):

Find code changes and other history with CodeLens - Visual Studio (Windows) (33)

Find incoming changes from other branches

Find code changes and other history with CodeLens - Visual Studio (Windows) (34)

You can view incoming changes. In the following screenshot, a bug fix was made in the "Dev" branch:

Find code changes and other history with CodeLens - Visual Studio (Windows) (35)

You can review the change without leaving your current branch ("Main"):

Find code changes and other history with CodeLens - Visual Studio (Windows) (36)

Find when changes got merged

You can see when changes got merged, so you can determine which changes are included in your branch:

Find code changes and other history with CodeLens - Visual Studio (Windows) (37)

For example, your code in the Main branch now has the bug fix from the "Dev" branch:

Find code changes and other history with CodeLens - Visual Studio (Windows) (38)

Compare an incoming change with your local version

Compare an incoming change with your local version by pressing Shift+F10, or by double-clicking the changeset.

Find code changes and other history with CodeLens - Visual Studio (Windows) (39)

Branch icons

The icon in the Branch column tells you how the branch is related to the branch you're working in.

IconThe change came from:
Find code changes and other history with CodeLens - Visual Studio (Windows) (40)The current branch
Find code changes and other history with CodeLens - Visual Studio (Windows) (41)The parent branch
Find code changes and other history with CodeLens - Visual Studio (Windows) (42)A child branch
Find code changes and other history with CodeLens - Visual Studio (Windows) (43)A peer branch
Find code changes and other history with CodeLens - Visual Studio (Windows) (44)A branch further away than a parent, child, or peer
Find code changes and other history with CodeLens - Visual Studio (Windows) (45)A merge from the parent branch to a child branch
Find code changes and other history with CodeLens - Visual Studio (Windows) (46)A merge from a child branch to the parent branch
Find code changes and other history with CodeLens - Visual Studio (Windows) (47)A merge from an unrelated branch (baseless merge)

Linked work items in Visual Studio 2019

Find linked work items by selecting the work items indicator or by pressing Alt+8.

Find code changes and other history with CodeLens - Visual Studio (Windows) (48)

Linked code reviews in Visual Studio 2019

Find linked code reviews by selecting the reviews indicator. To use the keyboard, hold down the Alt key and then press Left arrow or Right arrow to navigate the indicator options.

Find code changes and other history with CodeLens - Visual Studio (Windows) (49)

Linked bugs in Visual Studio 2019

Find linked bugs by selecting the bugs indicator or by pressing Alt+7.

Find code changes and other history with CodeLens - Visual Studio (Windows) (50)

Find the author of an item by selecting the authors indicator or by pressing Alt+5.

Find code changes and other history with CodeLens - Visual Studio (Windows) (51)

Open the shortcut menu for an item to see the contact options. If you have Teams installed, you might see options similar to those in the following screenshot:

Find code changes and other history with CodeLens - Visual Studio (Windows) (52)

Associated unit tests in Visual Studio 2019

You can discover unit tests that exist for your C# or Visual Basic code without opening Test Explorer.

  1. Go to application code that has associated unit test code.

  2. If you haven't already, build your application to load the CodeLens test indicators.

  3. Review the tests for the code by pressing Alt+3.

    Find code changes and other history with CodeLens - Visual Studio (Windows) (53)

  4. If you see a warning icon Find code changes and other history with CodeLens - Visual Studio (Windows) (54), the tests haven't run yet, so run them.

    Find code changes and other history with CodeLens - Visual Studio (Windows) (55)

  5. To review a test's definition, double-click the test item in the CodeLens indicator window to open the code file in the editor.

    Find code changes and other history with CodeLens - Visual Studio (Windows) (56)

  6. To review the test's results, choose the test status indicator (Find code changes and other history with CodeLens - Visual Studio (Windows) (57) or Find code changes and other history with CodeLens - Visual Studio (Windows) (58)) or press Alt+1.

    Find code changes and other history with CodeLens - Visual Studio (Windows) (59)

  7. To see how many people changed this test, who changed this test, or how many changes were made to this test, find your code's history and linked items.

Keyboard shortcuts in Visual Studio 2019

To use the keyboard to select indicators, press and hold the Alt key to display the related number keys, then press the number that corresponds to the indicator you want to select.

Find code changes and other history with CodeLens - Visual Studio (Windows) (60)

Note

To select the reviews indicator, hold down Alt while using the left and right arrow keys to navigate.

Q & A for CodeLens in Visual Studio 2019

Q: How do I turn CodeLens off or on or choose which indicators to see?

A: You can turn indicators off or on, except for the references indicator. Go to Tools > Options > Text Editor > All Languages > CodeLens.

When the indicators are turned on, you can also open the CodeLens options from the indicators.

Find code changes and other history with CodeLens - Visual Studio (Windows) (61)

Turn CodeLens file-level indicators on and off using the chevron icons at the bottom of the editor window.

Find code changes and other history with CodeLens - Visual Studio (Windows) (62)

Q: Where is CodeLens?

A: CodeLens appears in C# and Visual Basic code at the method, class, indexer, and property level. CodeLens appears at the file level for all other types of files.

  • Make sure CodeLens is turned on. Go to Tools > Options > Text Editor > All Languages > CodeLens.

  • DevOps-related indicators appear only when work items are linked to the code and when you have permissions to open linked work items. Confirm that you have team member permissions.

  • Unit test indicators don't appear when application code doesn't have unit tests. Test status indicators appear automatically in test projects. If you know that your application code has unit tests, but the test indicators don't appear, try building the solution (Ctrl+Shift+B).

Tip

CodeLens is available in Visual Studio Community edition, however, the source control indicators are not available in this edition.

Q: Why don't I see the work item details for a commit?

A: This might happen because CodeLens can't find the work items in Azure Boards. Check that you're connected to the project that has those work items, and that you have permissions to see those work items. Work item details might also not show if the commit description has incorrect information about the work item IDs in Azure Boards.

Q: How do I change the font and color for CodeLens?

A: Go to Tools > Options > Environment > Fonts and Colors.

Find code changes and other history with CodeLens - Visual Studio (Windows) (63)

To use the keyboard:

  1. Press Alt+T+O to open the Options dialog box.

  2. Press Up Arrow or Down Arrow to go to the Environment node, then press Left Arrow to expand the node.

  3. Press Down Arrow to go to Fonts and Colors.

  4. Press Tab to go to the Show settings for list, and then press Down Arrow to select CodeLens.

Q: Can I move the CodeLens heads-up display?

A: Yes, choose Find code changes and other history with CodeLens - Visual Studio (Windows) (64) to dock CodeLens as a window.

Find code changes and other history with CodeLens - Visual Studio (Windows) (65)

Find code changes and other history with CodeLens - Visual Studio (Windows) (66)

Q: How do I refresh the indicators?

A: This depends on the indicator:

  • References: This indicator updates automatically when the code changes. If the References indicator is docked as a separate window, refresh the indicator by selecting Refresh:

    Find code changes and other history with CodeLens - Visual Studio (Windows) (67)

  • Team: Refresh these indicators by selecting Refresh CodeLens Team Indicators from the right-click menu:

    Find code changes and other history with CodeLens - Visual Studio (Windows) (68)

  • Test: Find unit tests for your code to refresh the Test indicator.

Q: What's "Local Version"?

A: The Local Version arrow points at the most recent changeset in your local version of a file. When the server has more recent changesets, they appear above or below the Local Version arrow, depending on the order used to sort the changesets.

Q: My CodeLens test indicators no longer appear in my file when I first open my solution. How can I load them?

A: Rebuild your project to get CodeLens test indicators to load in your file. To improve performance, Visual Studio no longer fetches source information for test indicators when code files are loaded. Test indicators are loaded after a build, or when you navigate to a test by double-clicking on it in Test Explorer.

Next steps

Learn more about how to use Git and Azure DevOps in the Visual Studio version control docs.

See also

  • Features of the code editor
Top Articles
Latest Posts
Article information

Author: Rob Wisoky

Last Updated: 15/11/2023

Views: 6596

Rating: 4.8 / 5 (68 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Rob Wisoky

Birthday: 1994-09-30

Address: 5789 Michel Vista, West Domenic, OR 80464-9452

Phone: +97313824072371

Job: Education Orchestrator

Hobby: Lockpicking, Crocheting, Baton twirling, Video gaming, Jogging, Whittling, Model building

Introduction: My name is Rob Wisoky, I am a smiling, helpful, encouraging, zealous, energetic, faithful, fantastic person who loves writing and wants to share my knowledge and understanding with you.