Git add/delete a file or folder with spaces in the name
Summary
Learn how to handle files or directories with spaces in their names when using git
, focusing on using single quotes or backslashes to properly escape spaces in git add
and git rm
commands.
Introduction #
When dealing with files or directories that contain whitespace (space characters) in git
, you need to take special care with quoting or escaping the names to ensure they are interpreted correctly. Although this article and examples deal with Git, the rules for quoting / escaping come from the Bash shell.
Here’s how you can add
and/or remove (rm
) such files with Git in the bash
shell:
File name with space #
To git add
or git rm
files with spaces to the Git staging index and the working directory, you can wrap the file name, including the path, inside single quotes ('
):
git add 'path/to/file with spaces.txt'
git rm 'path/to/file with spaces.txt'
Alternatively you can escape using backslash (\
):
git add path/to/file\ with\ spaces.txt
git rm path/to/file\ with\ spaces.txt
Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.
Depending on your shell, you can try to use tab completion (start typing the file name, and use tab
key to let your shell handle the escaping automatically.
Folder name with space #
To git add
or git rm
directories with spaces to the Git staging index and the working directory, you can:
git add 'long path/to a/file with spaces.txt'
git rm 'long path/to a/file with spaces.txt'
Alternatively you can escape using backslash (\
):
git add long\ path/to\ a/file\ with\ spaces.txt
git rm long\ path/to\ a/file\ with\ spaces.txt
Best Practices #
Avoid Spaces in Filenames. If possible, avoid spaces in filenames for simplicity. Use underscores (_
) and/or hyphens (-
) instead.
Check out our file naming convention for more best practices.
Further readings #
Sources and recommended, further resources on the topic:
License
License: Git add/delete a file or folder with spaces in the name by Jonas Jared Jacek is licensed under CC BY-SA 4.0.
This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. To give credit, provide a link back to the original source, the author, and the license e.g. like this:
<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/instructions/git-add-delete-file-with-space-in-name">Git add/delete a file or folder with spaces in the name</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-SA 4.0</a>.</p>
For more information see the DITig legal page.