54

Is there any way to just stage pieces of code in a file instead of the whole file?

Just wondering if this is possible.

Using Visual Studio 2015, TFS 2015 (Git).

poke
  • 369,085
  • 72
  • 557
  • 602
Valter
  • 2,859
  • 5
  • 30
  • 51
  • 2
    It's very easy in Visual Studio Code: https://stackoverflow.com/a/34736732/8534588 – Josh Withee Dec 13 '18 at 15:31
  • Still not possible natively in VS 2019, but can be done in Git GUI (`Stage Hunk for Commit`) and Visual Studio Code (`Stage Selected Ranges`). You can even have the project open in both VS 2019 (for main development) and in Git GUI or VS Code (for git). – AlainD Dec 01 '20 at 14:49

5 Answers5

45

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.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • 7
    Note that Visual Studio does use the same index as the command line Git (that actually wasn’t the case in earlier versions), so you *can* stage files partially using the command line (or other tools) and still commit those staged changes from Visual Studio. – poke Dec 19 '16 at 15:16
  • 1
    If anyone has any pointers on instructions or examples on how to partially stage a file using a command line and then commit that from Visual Studio, it would be greatly appreciated. – tomosius Mar 08 '17 at 04:38
  • 1
    I use SourceTree to just index the partial chunk I need, and do rest of the selection and the commit in Visual Studio (VS understand and split the file in two modifications). – Matthieu Charbonnier Aug 07 '17 at 14:40
  • Also not possible in Visual Studio 2019. Additional clients which offer this functionality include `Git GUI` and `Visual Studio Code`. – AlainD Dec 01 '20 at 14:51
  • VS 22 seems to support partial staging – Kaiyakha Jan 03 '22 at 09:04
  • 1
    VS 22 17.03: released! https://devblogs.microsoft.com/visualstudio/git-line-staging-released/ – VonC Aug 22 '22 at 11:32
27

@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.

craigdfrench
  • 972
  • 10
  • 20
8

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

Morty
  • 2,889
  • 2
  • 19
  • 28
0

One can use the source tree for partial staging in files. All the changes will be reflected in the source tree if you commit from visual studio using team explorer.

arpit rai
  • 21
  • 1
  • 7
0

Not related to staging, but now you can unstage the selected lines as well with Visual Studio 2022 17.6.4.

enter image description here

Reference: https://developercommunity.visualstudio.com/t/Unstage-individual-lines-and-hunks-in-a-/10145068

https://learn.microsoft.com/en-us/visualstudio/releases/2022/release-notes#summary-of-whats-new-in-this-release-of-visual-studio-2022-version-176

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197