Search⌘ K
AI Features

Use git add Interactively

Explore how to use the git add interactive mode to stage changes selectively, down to individual lines. Learn to manage hunks for precise commits, split changes, and avoid including unwanted code in your repository history.

We'll cover the following...

The git add command comes with an interactive mode. In this mode, we can choose to stage only a part of a file, down to the level of lines.

Understand git add -i

First of all, let’s create two files, file1 and file2, in the new directory and commit them. Then, we add a few lines in both files after the initial commit according to the statements given below.

The following commands are already in the terminal below. Click the terminal to run them:

 $ mkdir folder
 $ cd folder
 $ git init
 $ echo 'This is file1' > file1
 $ echo 'This is file2' > file2
 $ git add .
 $ git commit -am 'files added'
 $ cat > file1 << END
 > Good change
 > This is file1
 > Experimental change
 > END
 $ cat > file2 << END
 > All good
 > This is file2
 > END
Terminal 1
Terminal
Loading...

In the terminal above, run the following command:

 $ git add -i

The following options appear: ...