Jonathan Yeong
Jonathan Yeong

Reputation: 563

Git add a folder with spaces in the name

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 add auto complete doesn't correctly escape the spaces.

For example

I have a folder named: Folder 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:

fatal: pathspec 'Folder' did not match any files

This is because the correct syntax should be 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?

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

Upvotes: 41

Views: 61648

Answers (2)

WinterChild
WinterChild

Reputation: 1059

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 :)

Upvotes: 79

VonC
VonC

Reputation: 1329082

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

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

% 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.

Upvotes: 0

Related Questions