带有空格的文件名如何使用git rm删除?

32
我尝试使用Xcode中的命令行进行项目合并,但我认为需要删除一个文件。这是一个在我正在合并的分支中不存在,但在我要合并的另一个分支中存在的文件。问题是它的名字中有一个空格。
TestService/TestService copy-Info.plist

我该如何删除那个文件?谢谢!


3
你尝试过使用单引号或双引号引用它吗? - Adrian Cornish
4个回答

58

你可以使用rm删除这样的文件,方法是对名称加引号:

git rm "TestService/TestService copy-Info.plist"
或者
git rm 'TestService/TestService copy-Info.plist'
或者
git rm TestService/TestService\ copy-Info.plist

根据您的shell和其他文件的名称,可能可以使用制表符完成此操作。输入

$ git rm TeTab

很可能会自动完成目录名称:

$ git rm TestingService/

然后输入文件名的一部分并再次按下Tab键:

$ git rm TestService/TeTab

将自动完成文件名,包括插入的\以转义空格字符:

$ git rm TestService/TestService\ copy-Info.plist

但是,制表符通常仅基于所有可用文件扩展唯一前缀,因此这可能或可能不起作用。


似乎在 [git 版本 2.15.1] 上使用命令 git rm --cached [文件路径] 无法正常工作。 - Danyun Liu
@DanyunLiu,我刚看到你的评论。它为什么不起作用? - Keith Thompson

5

您可以引用文件名:

git rm "TestService/TestService copy-Info.plist"

或者转义空格:

git rm TestService/TestService\ copy-Info.plist

2

如果你使用Ubuntu,有时需要使用git rm --cached -r "TestService/TestService copy-Info.plist"


2

您尝试给文件名添加引号了吗?"TestService/TestService copy-Info.plist" 我不能100%确定它在Git中的工作原理。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接