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

标签 git git-add

团队, 有没有一种方法可以在确认的情况下逐一添加我的所有更改?

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

最佳答案

对于仓库中已有文件的更改(而不是将新文件添加到仓库),您可能需要使用 git add --patch (git add -p ).来自 doco :

Interactively choose hunks of patch between the index and the work tree and add them to the index. This gives the user a chance to review the difference before adding modified contents to the index. This effectively runs add --interactive, but bypasses the initial command menu and directly jumps to the patch subcommand. See “Interactive mode” for details.

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

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

让我们尝试添加--patch

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

对于未暂存文件的交互式添加,请引用 git add --interactive。可以找到 doco here .

关于git - 如何在 git 上确认并一一添加所有更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51488714/

相关文章:

linux - Git 只提交几个文件

git - 发出 pull 请求后继续在 Git 分支上工作

git - 添加到 git staging 意味着什么?

git - 即使在 git add 之后,更改也不会提交

git - 文件从存储库中消失,但提交历史记录中没有删除

git - 如何将非版本控制的分支导入git?

git - 是否可以使用 “minimal”算法进行交互式添加?

git - 我可以让 "git add -p"在添加 hunk 后立即退出吗?

git - 如何贡献给别人的存储库?

Git 添加到阶段单独的行 - 就像 --patch 但更精确?