I have been writing a script to add untracked files using git add .
The loop I use in my script is
for FILE in $(git ls-files -o --exclude-standard); do
git add $FILE
git commit -m "Added $FILE"
git push origin master
done
The script runs fine till it faces a filename which has space in it. for Eg., I cant add the file Hello 22.mp4.(Note that there is a SPACE between Hello and 22). The above loop would take the file as 2 separate files, Hello and 22.mp4 and exit with error.
Does someone know how to add it as a single file?
Thanks
What’s happening is the shell is expanding the
$(...)into a bunch of words, and it’s obviously interpreting a file with spaces embedded as multiple files obviously. Even with the prior suggestions of quoting thegit addcommand, it wouldn’t work. So the loop is getting run with wrong arguments, as shown by this output withset -x:The proper solution is to quote the
git add $fileand havegit ls-filesNULL separate the filenames by passing-ztogit ls-filesand use a while loop with a null delimiter: