从 git 中删除名为“~”的文件并取消暂存

delete file named "~" and unstage from git

我不确定这是怎么发生的(也许我输入了命令行 vi ~ /extra/space.txt?)但是现在有一个名为 ~ 的文件正在准备提交。我不想将它添加到 git,在考虑可能的副作用之前,我犹豫要不要尝试 rm ~

$ git status
On branch master
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:
    modified:   apples.js
    modified:   app/berries.js
    modified:   app/views/pears.js
    new file:   app/views/lemons.js
    modified:   public/stylesheets/oranges.css
    new file:   ~

使用反斜杠进行转义

rm \~

或者,您可以使用单引号来避免 shell 解释(而不是转义 shell 解释)

rm '~'

由于暂存提交,您还需要在合并之前清除它,方法是再次 add 删除已删除的文件(因此它的缺失会合并到暂存提交中)或 reset-查看删除的文件(这可能是因为您使用了 git add -a

git add \~

虽然你的担心是实际的,但也别担心,rm 不会删除目录,只会删除文件,而不会提供一些标志(-d-r-R..)