Partially stage files with Visual Studio

GitTools hasn't got the best Gui, but better than nothing. In advanced mode (checkbox above the file list) you can stage or reset selected lines. https://marketplace.visualstudio.com/items?itemName=yysun.GitTools


@tomossius asked an example of how to partially stage a file using command line tools by using the git add interactive command. There may be a more elegant way but this is how I do it.

Git manual reference - Interactive Staging

I will run through a simple case nonetheless.

The command would be

git add -i stagepartialfile.cs

then you are prompted with a menu

           staged     unstaged path
  1:    unchanged      +30/-30 stagepartialfile.cs

*** Commands ***
  1: status       2: update       3: revert       4: add untracked
  5: patch        6: diff         7: quit         8: help
What now>

From here you would choose 5 or p for patch.

What now> 5
           staged     unstaged path
  1:    unchanged      +30/-30 stagepartialfile.cs
Patch update>>

Git prompts you to select the files you want to patch in. In this case we enter 1 to select the file that we have specified.

Patch update>> 1
           staged     unstaged path
* 1:    unchanged      +30/-30 stagepartialfile.cs
Patch update>>

With the * indicating that this file selected, we can simply hit enter to start the patching process.

At this point you will be prompted stage each individual chunk.

diff --git a/stagepartialfile.cs b/stagepartialfile.cs
index ea97bc6..d55218c 100644
--- a/stagepartialfile.cs
+++ b/stagepartialfile.cs
@@ -1,4 +1,5 @@
using System;
+using System.Configuration;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]? 

By pressing the ? we can get a listing of the commands

y - stage this hunk
n - do not stage this hunk
q - quit; do not stage this hunk or any of the remaining ones
a - stage this hunk and all later hunks in the file
d - do not stage this hunk or any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help

From here you can choose which chunks to stage by using y or n or s to split into smaller chunks.

After doing this you will see the file in Visual Studio in the staged area and in the unstaged area. The changes that you staged will be in that file and the ones that you said no to will be in the unstaged area.


2022

This has shipped with Visual Studio 17.3.

enter image description here

Visual Studio 2022 has recently introduced staging lines and chunks! See:

Line-staging (interactive staging)

Line-staging support, a.k.a. interactive staging is one of our most popular Git suggestion tickets. Line-staging can be helpful when you need to split changes across different commits. This preview includes few of the Line-staging features that we are still working on enhancing. The easiest way to enable this early version of line-staging support is to use CTRL+Q, type “preview” and open the preview features pane. Scroll to “Enable line-staging support” and toggle the checkbox.

Note: line-staging is still a preview feature

This functionality is still a preview feature, which means we are working hard to add more support in the coming releases. In the meantime, we’re depending on your feedback, the community, to build what you need

Once you switch on the line-staging preview flag and restart your Visual Studio, you can start staging chunks of your changes by clicking on files in the Git Changes window. Then hover over the sections of code you would like to stage and click Stage Change.

Tip: use line-staging with your preferred diff layout

Line-staging is supported on both inline and side-by-side diff modes: Image staging a chunk in line view

This early version of line-staging support has a number of known issues and limitations.

2019 and below

No, neither Visual Studio 2015, 2017, 2019 nor 2022 pre 17.3 support staging hunks (partial files). You'll need to use another client to stage these partial changes.

Staging hunks is a client feature, any client that supports it can be used to stage a hunk. The command line or a 3rd party client like Tower or SourceTree will do. Once staged, committing the staged changes can be done using Visual Studio or any other client that can commit changes (that would be pretty much every git client out there).

Once a hunk is staged, Visual Studio will show the file as "Staged" and also as "Unstaged". The staged file contains the hunk you staged. The unstaged file contains the hunks you haven't staged. When you commit the staged hunk(s) will be committed. You can repeat this cycle as many times as you want.