Say I have a file in my git repostiory called foo
.
Suppose it has been deleted with rm
(not git rm
). Then git status will show:
Changes not staged for commit:
deleted: foo
How do I stage this individual file deletion?
If I try:
git add foo
It says:
'foo' did not match any files.
Use git rm foo
to stage the file for deletion. (This will also delete the file from the file system, if it hadn't been previously deleted. It can, of course, be restored from git, since it was previously checked in.)
To stage the file for deletion without deleting it from the file system, use git rm --cached foo
You could do git add -u
.
This would help if you want to delete multiple files, without doing git rm
for each of them.
To stage all manually deleted files you can use:
git rm $(git ls-files --deleted)
To add an alias to this command as git rm-deleted
, run:
git config --global alias.rm-deleted '!git rm $(git ls-files --deleted)'
to Add all ready deleted files
git status -s | grep -E '^ D' | cut -d ' ' -f3 | xargs git add --all
thank check to make sure
git status
you should be good to go
Since Git 2.0.0, git add
will also stage file deletions.
Git 2.0.0 Docs - git-add
< pathspec >…
Files to add content from. Fileglobs (e.g. *.c) can be given to add all > matching files. Also a leading directory name (e.g. dir to add dir/file1 and dir/file2) can be given to update the index to match the current state of the directory as a whole (e.g. specifying dir will record not just a file dir/file1 modified in the working tree, a file dir/file2 added to the working tree, but also a file dir/file3 removed from the working tree. Note that older versions of Git used to ignore removed files; use --no-all option if you want to add modified or new files but ignore removed ones.
You can use
git rm -r --cached -- "path/to/directory"
to stage a deleted directory.