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

时间:2018-07-24 00:05:16

标签: git git-add

团队, 有什么方法可以确认我一个人添加所有更改吗?

示例:如果我更改了10个文件,则系统会提示我10次添加更改或否。我不想执行“ git add -A”或手动添加每个更改。有没有一种自动方法在添加git之前提示y / n?

2 个答案:

答案 0 :(得分:3)

对于仓库中已有文件的更改(与向仓库中添加新文件相对),您可能需要使用git add --patchgit 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")

让我们一一添加:

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

完成:

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$

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

例如:

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

让我们尝试add --patch

tymek@LAPTOP-B0OQU3LB:/mnt/c/git/sandbox$ git add --patch
No changes.

有关非暂存文件的交互式添加,请参阅git add --interactive。可以在here中找到该Doco。

答案 1 :(得分:1)

相关问题