Easy way to clean up un-staged deleted files in Git

If I understand your question correctly, you want to commit your deletes…that is, you want to perform the equivalent of git rm for all of the files that show up as deleted. git clean won’t do this.

You can run git add -u:

Only match against already tracked files in the index
rather than the working tree. That means that it will never stage new
files, but that it will stage modified new contents of tracked files
and that it will remove files from the index if the corresponding
files in the working tree have been removed.

This will pick up all changes to tracked files, including deletes. So if you start with this:

# 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:    file2
#   deleted:    file3
#   deleted:    file4
#   deleted:    file5

Running git add -u will get you to this:

# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   deleted:    file2
#   deleted:    file3
#   deleted:    file4
#   deleted:    file5

And a commit at this point will do the right thing.

Leave a Comment