Git add interactive

git add comes with an interaction mode.

$ git add -i
         staged     unstaged path
    1:    unchanged        +2/-2 index.html

  *** Commands ***
    1: status       2: update       3: revert       4: add untracked
    5: patch        6: diff         7: quit         8: help

  What now> 2
         staged     unstaged path
    1:    unchanged        +2/-2 index.html
  Update>> 1
         staged     unstaged path
  * 1:    unchanged        +2/-2 index.html
  Update>>
  updated one path

  What now> 1
         staged     unstaged path
    1:        +2/-2      nothing index.html

  *** Commands ***
    1: status       2: update       3: revert       4: add untracked
    5: patch        6: diff         7: quit         8: help


You can use the command to add file by choosing their index number.

You can also use `patch` to add line by line inside a file.


  Stage this hunk [y,n,q,a,d,/,s,e,?]? ?
  y - stage this hunk
  n - do not stage this hunk
  q - quit; do not stage this hunk nor any of the remaining ones
  a - stage this hunk and all later hunks in the file
  d - do not stage this hunk nor any of the later hunks in the file
  g - select a hunk to go to
  / - search for a hunk matching the given regex
  j - leave this hunk undecided, see next undecided hunk
  J - leave this hunk undecided, see next hunk
  k - leave this hunk undecided, see previous undecided hunk
  K - leave this hunk undecided, see previous hunk
  s - split the current hunk into smaller hunks
  e - manually edit the current hunk
  ? - print help

For example:

What now> s
         staged     unstaged path
    1:    unchanged        +2/-2 index.html

  *** Commands ***
    1: status       2: update       3: revert       4: add untracked
    5: patch        6: diff         7: quit         8: help
  What now> p
         staged     unstaged path
    1:    unchanged        +2/-2 index.html
  Patch update>> 1
         staged     unstaged path
  * 1:    unchanged        +2/-2 index.html
  Patch update>>
  diff --git a/index.html b/index.html
  index 5ba3e85..df49622 100644
  --- a/index.html
  +++ b/index.html
  @@ -1,5 +1,5 @@
  -This is HTML Sample.
  +This is HTML Sample for my Git course.

   Title: This is a sample.

  -Footer: Copyright.
  \\\\ No newline at end of file
  +Footer: Copyright 2016.
  \\\\ No newline at end of file
  Stage this hunk [y,n,q,a,d,/,s,e,?]? ?
  y - stage this hunk
  n - do not stage this hunk
  q - quit; do not stage this hunk nor any of the remaining ones
  a - stage this hunk and all later hunks in the file
  d - do not stage this hunk nor any of the later hunks in the file
  g - select a hunk to go to
  / - search for a hunk matching the given regex
  j - leave this hunk undecided, see next undecided hunk
  J - leave this hunk undecided, see next hunk
  k - leave this hunk undecided, see previous undecided hunk
  K - leave this hunk undecided, see previous hunk
  s - split the current hunk into smaller hunks
  e - manually edit the current hunk
  ? - print help
  @@ -1,5 +1,5 @@
  -This is HTML Sample.
  +This is HTML Sample for my Git course.

   Title: This is a sample.

  -Footer: Copyright.
  \\\\ No newline at end of file
  +Footer: Copyright 2016.
  \\\\ No newline at end of file
  Stage this hunk [y,n,q,a,d,/,s,e,?]? s
  Split into 2 hunks.
  @@ -1,4 +1,4 @@
  -This is HTML Sample.
  +This is HTML Sample for my Git course.

   Title: This is a sample.

  Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]? y
  @@ -2,4 +2,6 @@

   Title: This is a sample.

  -Footer: Copyright.
  \\\\ No newline at end of file
  +Footer: Copyright 2016.
  \\\\ No newline at end of file
  Stage this hunk [y,n,q,a,d,/,K,g,e,?]? d

  *** Commands ***
    1: status       2: update       3: revert       4: add untracked
    5: patch        6: diff         7: quit         8: help
  What now> s
         staged     unstaged path
    1:        +1/-1        +1/-1 index.html

  *** Commands ***
    1: status       2: update       3: revert       4: add untracked
    5: patch        6: diff         7: quit         8: help
  What now> q
  Bye.
  $ git st
  On branch index_b
  Changes to be committed:
    (use “git reset HEAD <file>...” to unstage)

      modified:   index.html

  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:   index.html

  $ git diff --cached
  diff --git a/index.html b/index.html
  index 5ba3e85..68b050a 100644
  --- a/index.html
  +++ b/index.html
  @@ -1,4 +1,4 @@
  -This is HTML Sample.
  +This is HTML Sample for my Git course.

   Title: This is a sample.

  $ git diff
  diff --git a/index.html b/index.html
  index 68b050a..df49622 100644
  --- a/index.html
  +++ b/index.html
  @@ -2,4 +2,4 @@ This is HTML Sample for my Git course.

   Title: This is a sample.

  -Footer: Copyright.
  \\\\ No newline at end of file
  +Footer: Copyright 2016.
  \\\\ No newline at end of file
  `