Staging Deleted files

2019-01-29 14:52发布

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.

6条回答
姐就是有狂的资本
2楼-- · 2019-01-29 15:10

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.

查看更多
Explosion°爆炸
3楼-- · 2019-01-29 15:15

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

查看更多
别忘想泡老子
4楼-- · 2019-01-29 15:16

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

查看更多
孤傲高冷的网名
5楼-- · 2019-01-29 15:22

You can use

git rm -r --cached -- "path/to/directory"

to stage a deleted directory.

查看更多
We Are One
6楼-- · 2019-01-29 15:29

You could do git add -u.

This would help if you want to delete multiple files, without doing git rm for each of them.

查看更多
女痞
7楼-- · 2019-01-29 15:33

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)'
查看更多
登录 后发表回答