You have files indexed before commit but you want to remove one of them from index so that you don't commit it accidentally. Changes to the file remain intact. For that, we use git rm --cached but it doesn't apply to "untracked" files. For more information, click git rm link.


Check the current status


inanzzz@inanzzz:~/project$ git status
On branch develop
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

new file: file_five.txt
modified: file_three.txt

Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

deleted: file_one.txt
modified: file_two.txt

Untracked files:
(use "git add <file>..." to include in what will be committed)

file_four.txt

Remove file from index


inanzzz@inanzzz:~/project$ git rm --cached file_three.txt
rm 'file_three.txt'

Check the current status


As we can see, "file_three.txt" is marked as "deleted" and also put into "untracked" section so it's updates won't be committed.


inanzzz@inanzzz:~/project$ git status
On branch develop
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

new file: file_five.txt
deleted: file_three.txt

Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

deleted: file_one.txt
modified: file_two.txt

Untracked files:
(use "git add <file>..." to include in what will be committed)

file_four.txt
file_three.txt

Commit


inanzzz@inanzzz:~/project$ git commit -m 'Commit'
[develop 4cb186c] Commit
2 files changed, 1 insertion(+), 2 deletions(-)
create mode 100644 file_five.txt
delete mode 100644 file_three.txt

Check the current status


As we can see, "file_three.txt" is still in "untracked" section so it hasn't actually been committed.


inanzzz@inanzzz:~/project$ git status
On branch develop
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

deleted: file_one.txt
modified: file_two.txt

Untracked files:
(use "git add <file>..." to include in what will be committed)

file_four.txt
file_three.txt

no changes added to commit (use "git add" and/or "git commit -a")