gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:11:27 25 4
gpt4 key购买 nike

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

示例:如果我更改了 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com