37

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!

Community
  • 1
  • 1
Jonathan Yeong
  • 523
  • 1
  • 4
  • 8

2 Answers2

71

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

WinterChild
  • 969
  • 10
  • 13
  • 14
    On windows, it must be double quotes, like git add "Folder A" – derek May 09 '18 at 18:10
  • I mean, obviously "on Windows" is too broad. If you're using [MinGW-w64 with git-windows](https://stackoverflow.com/a/43852861/1028230), the `'` is fine, for example. But it's a good point. ;^) – ruffin Jan 30 '23 at 21:08
  • This works fine in Mac terminal. Thanks – kiran kumar Apr 06 '23 at 06:03
0

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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    When I run the command `echo "testing" >> test` It will correctly auto complete. However, it doesn't autocomplete when I try `git add ` as stated above. Could I clarify my question further? As a work around I have started using `git add ""` which escapes the spaces. But as you have said @VonC: `proper completion shouldn't need quptes ("), but should escape spaces.` – Jonathan Yeong Dec 15 '14 at 03:48