@tomossius 询问了一个示例,说明如何使用 git add 交互式命令使用命令行工具部分暂存文件。可能有一种更优雅的方式,但这就是我的做法。
Git manual reference - Interactive Staging
不过,我还是会介绍一个简单的案例。
命令是
git add -i stagepartialfile.cs
然后你会收到一个菜单提示
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>
从这里你可以选择 5 或 p 作为补丁。
What now> 5
staged unstaged path
1: unchanged +30/-30 stagepartialfile.cs
Patch update>>
Git 提示您选择要修补的文件。在这种情况下,我们输入 1 来选择我们指定的文件。
Patch update>> 1
staged unstaged path
* 1: unchanged +30/-30 stagepartialfile.cs
Patch update>>
用*表示这个文件被选中,我们可以简单地按回车开始修补过程。
此时,系统会提示您暂存每个单独的块。
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,?]?
按 ?我们可以得到命令列表
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
您可以在此处选择要暂存的块,方法是使用 y 或 n 或 s 将其拆分为更小的块。
完成此操作后,您将在 Visual Studio 中的暂存区域和未暂存区域中看到该文件。您暂存的更改将在该文件中,而您拒绝的更改将在未暂存区域中。