使用Visual Studio部分暂存文件

Val*_*ter 27 git visual-studio-2015 tfs-2015

有没有办法只在文件而不是整个文件中放置代码片段?

只是想知道这是否可行.

使用Visual Studio 2015,TFS 2015(Git).

jes*_*ing 22

不,Visual Studio 2015和2017都不支持暂存(部分文件).您需要使用其他客户端来暂存和提交这些部分更改.

这是一个客户端功能,因此要么使用命令行,要么使用Tower或SourceTree等第三方客户端.

  • 请注意,Visual Studio确实使用与命令行Git相同的索引(在早期版本中实际上并非如此),因此您可以*使用命令行(或其他工具)部分地暂存文件,并仍然提交这些分阶段的更改来自Visual Studio. (7认同)
  • 这个 staging hunk 功能就是我还没有完全离开 SourceTree 的原因。 (2认同)

cra*_*nch 18

@tomossius通过使用git add interactive命令询问了如何使用命令行工具部分暂存文件的示例.可能有一种更优雅的方式,但这就是我的方式.

Git手动参考 - 交互式分段

尽管如此,我还是会遇到一个简单的案例.

命令是

git add -i stagepartialfile.cs
Run Code Online (Sandbox Code Playgroud)

然后会出现菜单提示

           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>
Run Code Online (Sandbox Code Playgroud)

从这里你可以选择5或p作为补丁.

What now> 5
           staged     unstaged path
  1:    unchanged      +30/-30 stagepartialfile.cs
Patch update>>
Run Code Online (Sandbox Code Playgroud)

Git会提示您选择要修补的文件.在这种情况下,我们输入1以选择我们指定的文件.

Patch update>> 1
           staged     unstaged path
* 1:    unchanged      +30/-30 stagepartialfile.cs
Patch update>>
Run Code Online (Sandbox Code Playgroud)

使用*表示选中此文件,我们只需按Enter键即可开始修补过程.

此时,您将被提示每个单独的块.

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,?]? 
Run Code Online (Sandbox Code Playgroud)

按下?我们可以获得命令列表

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
Run Code Online (Sandbox Code Playgroud)

从这里,您可以使用y或n或s来选择要分段的块,以便分割成更小的块.

执行此操作后,您将在舞台区域和未分区区域中的Visual Studio中看到该文件.您暂存的更改将在该文件中,而您拒绝的更改将在未暂存区域中.

  • 我认为“更优雅”的路线只是“git add -p”,它只是削减了菜单。 (9认同)

mor*_*rty 7

GitTools没有最好的Gui,但总比没有好.在高级模式(文件列表上方的复选框)中,您可以暂存或重置选定的行.https://marketplace.visualstudio.com/items?itemName=yysun.GitTools