user9182249
user9182249

Reputation:

how to add all changes one by one with confirmation on git

team, is there a way I can add all my changes one by one with confirmation?

example: if I changed 10 files, then I should be prompted 10 times to add change or no. I don't want to do "git add -A" or add every change manually. is there an automatic way which prompts y/n before adding on git?

Upvotes: 3

Views: 4351

Answers (2)

tmaj
tmaj

Reputation: 35037

For changes in files already in the repo (as opposed to adding new files to repo) you may want to use git add --patch (git add -p). From 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.

Let's consider two files were changed:

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")

Let's add them one by one:

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

Done:

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$

Let me reiterate that this will not interact with new files.

For example:

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

Let's try add --patch

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

For interactive adding of unstaged files please refer to git add --interactive. The doco can be found here.

Upvotes: 6

Pankaj Gadge
Pankaj Gadge

Reputation: 2814

Take a look at interactive staging

git add -i

https://git-scm.com/book/en/v2/Git-Tools-Interactive-Staging

Upvotes: 1

Related Questions