gitvisual-studio-2015tfs-2015

Partially stage files with Visual Studio


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


Solution

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