Partially stage files with Visual Studio Partially stage files with Visual Studio git git

Partially stage files with Visual Studio


No, neither Visual Studio 2015, 2017, nor 2019 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.


@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: helpWhat now>

From here you would choose 5 or p for patch.

What now> 5           staged     unstaged path  1:    unchanged      +30/-30 stagepartialfile.csPatch 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.csPatch 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.csindex 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 hunkn - do not stage this hunkq - quit; do not stage this hunk or any of the remaining onesa - stage this hunk and all later hunks in the filed - do not stage this hunk or any of the later hunks in the fileg - select a hunk to go to/ - search for a hunk matching the given regexj - leave this hunk undecided, see next undecided hunkJ - leave this hunk undecided, see next hunkk - leave this hunk undecided, see previous undecided hunkK - leave this hunk undecided, see previous hunks - split the current hunk into smaller hunkse - 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.


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