How to do it...

First, we need some changes to be added; we do this by resetting the latest commit:

$ git reset 'HEAD^'
Unstaged changes after reset:
M    liberty.txt

Now we have a modified file. To start the interactive add, we can either run the git add -i or git add -p filename. The -i option brings up an interface where all the different files in the modified state can be added interactively one at a time. The add -p/--patch option is simpler and just gives you the option to add parts of the file specified:

$ git add -p liberty.txt
diff --git a/liberty.txt b/liberty.txt
index 8350a2c..9638930 100644
--- a/liberty.txt
+++ b/liberty.txt
@@ -8,6 +8,13 @@
      WW) ,WWW)
+          9---W)
+      ,,--WPL=YXW===
+     (P),CY:,I/X'F9P
+ WUT===---/===9) ...

Get Git Version Control Cookbook now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.