How to add and commit removals made with "rm" instead of "git rm"?

I deleted a bunch of files and directories from a Git repository using rm , the Finder, etc.

I'm looking for a Git command that'll record these to the index as marked for removal, as if I had called git rm on them.

I understand git add -u will do this, along with a bunch of other things. I'd like my command to exclusively handle removals.


Without spaces in filenames:

$ git rm `git ls-files -d`

More robust:

$ git ls-files -z -d | xargs -0 --no-run-if-empty git rm

Take a look what Jonio C Hamano wrote in "Re: [PATCH 1/2] Documentation: 'git add -A' can remove files" post at git mailing list, namely that this question looks like XY problem (you are asking about assumed solution Y to the problem, instead about the problem X itself). The solution to problem (if it is really "XY problem" situation) might be:

  • git commit -a , which would automatically pick up deletions, committing current state of tracked files in working directory

  • git add -A , which would add not ignored untracked files and remove no longer existing files, eg if you want to create commit from sideways update of working directory, eg unpacking a snapshot or result of rsync.

  • Nevertheless if what you ask is a problem (and not solution), then as you can see from other answers there are tools in place to do it.


    查尔斯贝利的回答让我意识到这一点,但我仍然欢迎更短的东西。

    $ git diff --name-only --diff-filter=D | xargs git rm
    
    链接地址: http://www.djcxy.com/p/45274.html

    上一篇: 我如何在git中混淆命令?

    下一篇: 如何添加和提交使用“rm”而不是“git rm”创建的删除?