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
Deleted files show up under changes not staged for commit:
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:
To stage all deleted files without naming each one:
git ls-files --deleted lists the same files that git status reports as deleted.
3. Commit
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:
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
To confirm the deletions landed, review the history. --summary adds a summary of changes to each commit, including the files it deleted.
Command reference
| Command | Description |
git status | Display 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 --summary | Review commit history, including deletions. |
Related reading
- How do I commit case-sensitive only filename changes in Git?
- How do I commit changes in a git submodule?
- How do I commit only some files?
- How do I concatenate or merge arrays in Swift?
- How do I configure AWS CodeBuild to build only when push to master branch is made?
- How do I configure git to ignore some files locally?
- How do I convert a bare git repository into a normal one in-place?
- How do I copy a version of a single file from one Git branch to another?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseInterview Questions practice on Codemia
Over 11,000 interview practice questions for 300+ companies, searchable by company and role.