Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 503
Jonathan Yeong Avatar asked Dec 10 '14 03:12

Jonathan Yeong


People also ask

Can you put spaces in folder names?

Leading (before the filename) and trailing (after the filename) spaces in file or folder names also aren't allowed. If you're using Office 2010, you can't use "&" in file and folder names. These names aren't allowed for files or folders: . lock, CON, PRN, AUX, NUL, COM0 - COM9, LPT0 - LPT9, _vti_, desktop.

How do I add an entire folder in git?

The easiest way to add all files to your Git repository is to use the “git add” command followed by the “-A” option for “all”. In this case, the new (or untracked), deleted and modified files will be added to your Git staging area. We also say that they will be staged.

Can you git add a folder?

GitHub does not allow you to add blank folders to your Git repository. A folder must contain a file before you can add it to a repository. If you want to create a new folder, create it first and then add a placeholder file into that folder. Then, add the new folder and file to your Git repo.

How do I add files to a git space?

Surround the folder or file with a space in it with quotes! On Mac and Linux, you can add single quotes, but on Windows you'll have to use double quotes. …and everything worked as expected! This works for all the git commands: so add and diff , for instance, are included.


1 Answers

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

like image 108
WinterChild Avatar answered Sep 22 '22 17:09

WinterChild