Git 添加名称中带有空格的文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27393010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:44:36  来源:igfitidea点击:

Git add a folder with spaces in the name

gitdirectoryaddspace

提问by Jonathan Yeong

Problem

问题

When I want to add something to the staging area I normally type git add < folder-name >. However, I can't add folders with spaces in the name. My git addauto complete doesn't correctly escape the spaces.

当我想向暂存区添加一些东西时,我通常输入 git add <文件夹名称>。但是,我无法添加名称中带有空格的文件夹。我的git add自动完成没有正确转义空格。

For example

例如

I have a folder named: Folder A

我有一个文件夹名为:文件夹 A

I run the command git add F < tab-autocomplete >which becomes git add Folder A/. If I try and add this folder it will throw an error:

我运行的命令git add F < tab-autocomplete >变成了git add Folder A/. 如果我尝试添加此文件夹,它将引发错误:

fatal: pathspec 'Folder' did not match any files

fatal: pathspec 'Folder' did not match any files

This is because the correct syntax should be git add Folder\ A/.

这是因为正确的语法应该是git add Folder\ A/.

Summary

概括

I'm not sure how to fix this though and I can't find any resources with a permanent fix. This issue "How git deals with folder names with spaces" describes a fix. But it involves putting speech marks around the folder name which I don't really want to do. Is there a better solution?

我不知道如何解决这个问题,而且我找不到任何可以永久修复的资源。这个问题“ git 如何处理带空格的文件夹名称”描述了一个修复。但它涉及在文件夹名称周围放置语音标记,我真的不想这样做。有更好的解决方案吗?

I'm using git version 2.2.0 and zsh version 5.0.7. Thank you in advance!

我使用的是 git 2.2.0 版和 zsh 5.0.7 版。先感谢您!

回答by WinterChild

The solution is to wrap the folder name inside ' and ' (single quotes).
In your example, try the following:

解决方案是将文件夹名称括在 ' 和 '(单引号)内。
在您的示例中,请尝试以下操作:

git add 'Folder A'

I hope this helps :)

我希望这有帮助 :)

回答by VonC

You check if the setup mentioned in "git completion with zsh: filenames with spaces aren't being escaped properly" works:

您检查“使用 zsh 完成 git 完成:带空格的文件名没有正确转义”中提到的设置是否有效:

The shell backslash escapes the filenames as expected when I use tab completion to insert the file name.

当我使用制表符完成插入文件名时,shell 反斜杠会按预期对文件名进行转义。

% echo "testing" >> test<tab>

autocompletes to this after hitting tab three times.

点击标签三下后自动完成。

% echo "testing" >> test\ four\ -\ latest.txt

In other words, the proper completion shouldn't need quptes ("), but should escape spaces.

换句话说,正确的完成不应该需要 quptes ( "),但应该转义空格。