Git
version control
commit
deleted files
duplicate question

How to commit deleted files in Git

Codemia Knowledge Hub ·

Deleting a file from your working directory does not stage the deletion. Stage it with git rm <file>, or stage every deleted file at once with git rm $(git ls-files --deleted), then commit. Git sees the file is gone, but the deletion only becomes part of the next commit once it is staged.

1. Find the deleted files

bash
   git status

Deleted files show up under changes not staged for commit:

 
1   Changes not staged for commit:
2     (use "git add/rm <file>..." to update what will be committed)
3     (use "git restore <file>..." to discard changes in working directory)
4
5    deleted:    path/to/deleted-file.txt

2. Stage the deletions

git rm removes a file from the working directory and stages its deletion. For a file you already deleted, it just stages the removal:

bash
   git rm path/to/deleted-file.txt

To stage all deleted files without naming each one:

bash
   git rm $(git ls-files --deleted)

git ls-files --deleted lists the same files that git status reports as deleted.

3. Commit

bash
   git commit -m "Remove unnecessary files"

This commits the staged deletions to the repository. The -m flag supplies the commit message inline, and the message should describe what was removed and why.

Restoring a file you deleted by mistake

If the deletion is not committed yet, bring the file back from the last commit:

bash
  git restore path/to/file

A .gitignore entry does not help here: it has no effect on files Git already tracks, so it neither hides nor prevents staging their deletion.

Confirming deletions in history

bash
  git log --summary

To confirm the deletions landed, review the history. --summary adds a summary of changes to each commit, including the files it deleted.

Command reference

CommandDescription
git statusDisplay the state of the working directory and staging area.
git rm <file>Stage the deletion of a specific file.
git rm $(git ls-files --deleted)Stage all deleted files at once.
git commit -m "message"Commit changes with a message.
git restore <file>Restore a deleted file if not yet committed.
git log --summaryReview commit history, including deletions.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Interview Questions practice on Codemia

Over 11,000 interview practice questions for 300+ companies, searchable by company and role.

Browse interview questions