Sometimes you want to commit only a few sections of a file rather than the whole file. In order to have more control over the contents of a commit we can implement the --interactive mode of the git add command.
Tracking the files with Git
Let's say that you just created a file called fruits.txt within the working directory and you want to save its contents into two different commits.
The file contains two words:
Bananas
Apples
By running the command git status we can see that the file is untracked:
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: countries.txt
modified: spanish.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
fruits.txt
Firstly make Git track the file; in other words make Git aware that the file exists by running the next command:
git add -N fruits.txt
Then run git status and check out the output; now the file fruits.txt appears under the Changes not staged for commit heading which means Git it aware the file exists:
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: countries.txt
new file: fruits.txt
modified: spanish.txt
Running the --interactive mode to customize the commit
We can customize the contents of a commit not just on a file basis but on a line-by-line basis.
The command git addcan be used to customize the contents inside the staging area; in other words we can customize the commit:
Run the next command:
git add --interactive
In the prompt enter the keyword patch:
What now> patch
In my case the output looks as shown below:
staged unstaged path
1: unchanged +1/-0 countries.txt
2: +0/-0 +2/-0 fruits.txt
3: unchanged +1/-0 spanish.txt
In the prompt enter the number corresponding to the file fruits.txt as shown below:
Patch update>> 2
The output looks as shown below in my case; note the asterisc next to the entry for the fruits.txt file; which means such a file was selected. Press enter in the prompt as a confirmation:
staged unstaged path
1: unchanged +1/-0 countries.txt
* 2: +0/-0 +2/-0 fruits.txt
3: unchanged +1/-0 spanish.txt
Patch update>>
Then the output displays the contents of the file; in this case two lines of text. Enter e in the command prompt to indicate that we want to customize which line of text will be included in the next commit and which one won't be included:
index 0000000..9584ef5
--- /dev/null
+++ b/fruits.txt
@@ -0,0 +1,2 @@
+Bananas
+Apples
(1/1) Stage addition [y,n,q,a,d,e,?]? e
The text editor opens and in order to indicate that one line won't ne included into the next commit let's add a # symbol at the beginning of the line:
# Manual hunk edit mode -- see bottom for a quick guide.
@@ -0,0 +1,2 @@
+Bananas
#Apples
# ---
Save the changes and exit the text editor. Now in the command prompt enter diff to see what's included in the staging area :
What now> diff
The output contains the files within the staging area and an overview of the additions and deletions made to the file. In the command prompt enter the number which represents the file to see its contents:
staged unstaged path
1: +1/-0 +1/-0 fruits.txt
Review diff>> 1
The output indicates that the words Bananas will be included into the next commit; note how the word Apples does not appear in the file anymore:
index 0000000..2db3da3
--- /dev/null
+++ b/fruits.txt
@@ -0,0 +1 @@
+Bananas
In the command promt enter quit to exit the --interactive mode:
What now> quit
Bye.
Now in order to see the contents of the file fruits.txt which ain't be included into the commit run the next command:
git diff fruits.txt
And the output looks as shown below:
diff --git a/fruits.txt b/fruits.txt
index 2db3da3..9584ef5 100644
--- a/fruits.txt
+++ b/fruits.txt
@@ -1 +1,2 @@
Bananas
+Apples
The word Apples is not in the staging area and won't be included into the next commit.
Commit the changes
Run the next command:
git commit -m "some comment on the commit"
And the new commit will be created. Run git log to see the listing of commits available:
commit a722eda865765382bc3e88a0e750d6f316f22c0a (HEAD -> main)
Author: Raymunco CH <[email protected]>
Date: Mon Jun 16 19:50:14 2025 -0600
commit for Bananas
more commits appear here in my case....
Commiting the remaining text line
The word Apples remains unstaged and uncommited therefore let's create a commit for it.
Run the command:
git add fruits.txt
Once the file is into the staging area; let's run the next command to see the contents added to the staging area:
git diff --staged
the output looks as follows:
--- a/fruits.txt
+++ b/fruits.txt
@@ -1 +1,2 @@
Bananas
+Apples
Once the word Apples is staged and ready to be commited then run the next command_
git commit -m "Commit for apples"
And that's it!! the contents of the file fruits.txt were splitted into two commits.
Happy Coding!!
Top comments (0)