Stage files already deleted from disk in Git

Record tracked deletions with git add -u, distinguish them from other changes, and recover a staged deletion from the correct source.

Best practices
Application troubleshooting illustration

Use git add -u to stage deletions of tracked files that you already removed from disk. It also stages modifications to tracked files, so inspect the staged diff before committing. It does not add new untracked files.

Stage and review the changes

git status --short
git add -u
git diff --cached --name-status
git diff --cached
git commit -m "Remove obsolete files"

Without a path restriction, modern Git updates tracked files across the working tree. To restrict the scope, use git add -u -- path/to/directory/. git add -A includes new files as well as modifications and deletions; it is useful when that broader set is intentional.

If you need only a few deletions, stage the explicit paths instead of accepting unrelated modified files into the commit.

Remove files that are still present

git rm -- file1.txt dir/file2.txt
git rm -- '*.log'

git rm removes tracked paths from the working tree and stages their removal, subject to its checks for local changes. Quote the pattern so Git interprets it. It does not remove arbitrary untracked files.

To stop tracking a file while keeping its current working copy:

git rm --cached -- config/local.yml

Add an appropriate ignore rule if the file should remain local. This does not erase earlier committed versions. If a credential was committed, rotate it and address historical exposure separately.

Clean untracked files separately

git clean -n
# After reviewing the preview:
git clean -f
# Preview directories too before using the directory-removal form:
git clean -nd

git clean -fd includes untracked directories. Ordinary clean excludes ignored files; -x changes that scope substantially. Git generally cannot recover content it never stored, so preview the exact options you intend to use and preserve needed files first.

Restore an accidental deletion

For an unstaged deletion, git restore -- path/to/file copies the index version into the working tree. After you have staged the deletion, that index version is gone. To restore both copies from the current commit:

git restore --source=HEAD --staged --worktree -- path/to/file

This overwrites local changes at the selected path. After committing the deletion, choose an earlier commit that contains the file, restore from it, and review a new commit. Restoring from the new HEAD cannot bring back a file that is absent from that snapshot.

Deleting a file in a new commit does not remove its older blobs from reachable history. Shrinking a repository by removing historical large files is a separate history-rewrite operation that requires coordination.

See git-add, git-rm, git-clean, and git-restore.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting