Stage lines of code in Visual Studio

Line-staging support, also known as interactive staging, is available in Visual Studio 2022 version 17.3 and later. Line-staging lets you stage chunks of changes in your files right from the code editor. Line staging can also be helpful when you want to split changes across different commits.

Screenshot of an example of line-staging support in Visual Studio 2022.

Starting with Visual Studio 2022 version 17.6, you can use the hover option to also unstage changes, line by line.

Screenshot of an example of how to unstage changes in Visual Studio 2022.

Color margin support

The first thing you might notice about line-staging support is how the editor visualizes Git changes on both the scroll bar and in the margin.

Screenshot of the color margins in the Visual Studio 2022 editor.

The colors and annotations make it easy to differentiate between saved and unsaved changes that you've added, deleted, or modified.

Screenshot of differentiated and annotated color margin examples in Visual Studio 2022.

Tip

You can change the colors that Visual Studio displays to help you manage changes. Go to Tools > Options > Environment > Fonts and Colors and then choose from the following options:

  • Track additions in documents under source control
  • Track deletions in documents under source control
  • Track modifications in documents under source control

Screenshot of the Options dialog box where you can change the colors Visual Studio uses for source control.

Peek Difference support

Color margins are interactive. When you select a color margin, Visual Studio opens the Peek Difference user interface (UI) where you can view your changes in the editor without switching context. The Peek Difference UI summarizes the number of added and removed lines and makes it easy to navigate between changes by using the Up and Down arrows.

Screenshot of the Peek Difference user interface in Visual Studio as it relates to source control.

You can also access Peek Difference when you right-click a line that includes changes. A context menu appears where you can select Peek Difference from it. Or, you can use the Alt+F8 keyboard shortcut.

Screenshot of the right-click context menu in Visual Studio where you can select Peek Difference.

Note

If you prefer an inline difference view, you can use the Settings control in the top-right corner of the Diff editor to switch to Inline mode.

Screenshot of the Diff options available from the gear icon, where you can select the 'Inline mode' option.

Tip

You can modify how Visual Studio interacts with code changes in the Peek Difference UI by using the Click to peek in margin option from Tools > Options > Text Editor > Advanced. Screenshot of the 'Click to peek in margin' setting. For example, you can change the default Single click setting to Double click, or you can select None to turn off the Peek Difference UI.

Stage chunks of code

You can stage any chunk of code by using the Peek Difference user interface (UI). To do so, hover over the change you want to stage and select Stage Change. Alternatively, use the global Stage button if you want to stage all the changes you made to a document.

Screenshot of the Stage Change option in Visual Studio 2022.

If you prefer a full-screen difference view, you can switch to the Diff editor. Here's how: select the Promote to Document button on the top-right corner of the Peek Difference UI. Or, you can use the Ctrl+Alt+Home keyboard shortcut. All the features that the Peek Difference UI provides are also available in the Diff editor.

Screenshot of the Stage Change difference view in Visual Studio 2022.

Commit staged changes

After you've prepared your commit by reviewing and staging the changes you want to include, you can create your commit by using the Git Changes window. Next, enter a commit message and then select Commit Staged.

Screenshot of the Git Changes dialog in Visual Studio 2022.