Stage individual lines or chunks of code in Visual Studio with Git from a file with many changes

The ability to stage individual lines or chunks of code from a file with many changes in Visual Studio with Git is possible since 17.2. This is really helpful if we need to split changes across multiple commits.

To enable make sure the below option is checked, although it should be enabled by default.

To use line/chunk staging just make multiple changes in a file and open the ‘Compare with Unmodified…’ window by right clicking on the file in the Git Changes window to open a view similar to below.

Next either mouse over a change/chunk to show the ‘Stage Change’ button or click a specific line to show the ‘Stage Line’ button…

If I stage the whole change (two lines in total) by clicking ‘Stage Change’ for bug1 my Git Changes window will look like this…

We can see we now have the ability to check in changes related to bug1 and bug2 independently.

Leave a Reply

Your email address will not be published.