问题
Say I have a file in my git repository 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.
回答1:
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
回答2:
Even though it's correct to use git rm [FILE]
, alternatively, you could do git add -u
.
According to the git-add
documentation:
-u --update
Update the index just where it already has an entry matching . This removes as well as modifies index entries to match the working tree, but adds no new files.
If no is given when -u option is used, all tracked files in the entire working tree are updated (old versions of Git used to limit the update to the current directory and its subdirectories).
Upon which the index will be refreshed and files will be properly staged.
回答3:
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)'
回答4:
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
回答5:
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.
回答6:
You can use
git rm -r --cached -- "path/to/directory"
to stage a deleted directory.
回答7:
You can use this command
git add `git ls-files --deleted`
来源:https://stackoverflow.com/questions/12373733/staging-deleted-files