如何在git上确认后一一添加所有更改

2 git git-add

团队,有没有一种方法可以通过确认一一添加我的所有更改?

例如:如果我更改了 10 个文件,那么应该会提示我 10 次是否添加更改。我不想执行“git add -A”或手动添加每个更改。在添加 git 之前是否有自动提示 y/n 的方法?

tym*_*tam 5

对于 repo 中已有文件的更改(而不是向 repo 添加新文件),您可能需要使用git add --patch( git add -p)。从DOCO

在索引和工作树之间交互选择补丁块并将它们添加到索引中。这使用户有机会在将修改后的内容添加到索引之前查看差异。这有效地运行 add --interactive,但绕过初始命令菜单并直接跳转到 patch 子命令。有关详细信息,请参阅“交互模式”。

让我们考虑更改了两个文件:

tymtam@xxx:/mnt/c/git/sandbox$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   1.txt
        modified:   2.txt

no changes added to commit (use "git add" and/or "git commit -a")
Run Code Online (Sandbox Code Playgroud)

让我们一一添加:

tymtam@xxx:/mnt/c/git/sandbox$ git add --patch
diff --git a/1.txt b/1.txt
index e69de29..9d60796 100644
--- a/1.txt
+++ b/1.txt
@@ -0,0 +1 @@
+11
\ No newline at end of file
Stage this hunk [y,n,q,a,d,/,e,?]? y

diff --git a/2.txt b/2.txt
index e1f9ded..8fdd954 100644
--- a/2.txt
+++ b/2.txt
@@ -1 +1 @@
-x echo y
+22
\ No newline at end of file
Stage this hunk [y,n,q,a,d,/,e,?]? y
Run Code Online (Sandbox Code Playgroud)

完毕:

tymtam@xxx:/mnt/c/git/sandbox$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   1.txt
        modified:   2.txt

tymtam@xxx:/mnt/c/git/sandbox$
Run Code Online (Sandbox Code Playgroud)

让我重申一下,这不会与新文件交互。

例如:

tymek@LAPTOP-B0OQU3LB:/mnt/c/git/sandbox$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   1.txt
        modified:   2.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        3.txt
Run Code Online (Sandbox Code Playgroud)

咱们试试吧 add --patch

tymek@LAPTOP-B0OQU3LB:/mnt/c/git/sandbox$ git add --patch
No changes.
Run Code Online (Sandbox Code Playgroud)

有关未暂存文件的交互式添加,请参阅git add --interactive。可以在这里找到 doco 。