问题
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).
回答1:
No, neither Visual Studio 2015 nor 2017 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.
回答2:
@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.
回答3:
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
回答4:
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.
来源:https://stackoverflow.com/questions/41225582/partially-stage-files-with-visual-studio