Stage lines of code in Visual Studio
Applies to: Visual Studio Visual Studio for Mac Visual Studio Code
Line-staging support, also known as interactive staging, is now available in Visual Studio 2022 version 17.3. 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.
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.
The colors and annotations make it easy to differentiate between saved and unsaved changes that you've added, deleted, or modified.
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
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.
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.
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.
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. 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.
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.
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.
To continue your journey, visit the Push to remote page.
Submit and view feedback for