harithahv
harithahv

Reputation: 43

Can't add a file separated with space to git

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

Upvotes: 4

Views: 4572

Answers (6)

Daniel Gray
Daniel Gray

Reputation: 1

To add as a single file add a backslash before the space in the filename:

git add pathtofilename/filenamewith\ space.txt

Upvotes: 0

Joe Linoff
Joe Linoff

Reputation: 771

I know that this is very late but here is one way to do it using the standard xargs linux command:

git ls-files -o --exclude-standard | xargs -L 1 -I{} -d '\n' git add '{}'

You can test it by simply echoing the command as follows:

git ls-files -o --exclude-standard | xargs -L 1 -I{} -d '\n' echo "git add '{}'"

Upvotes: 0

another.anon.coward
another.anon.coward

Reputation: 11395

If you are using bash alternative to the solution provided by @AndrewF, you can make use of IFS bash internal variable to change the delimiter from space to newline, something on these lines:

(IFS=$'\n'
for FILE in $(git ls-files -o --exclude-standard); do  
git add $FILE  
git commit -m "Added $FILE"  
git push origin master  
done 
)

This is just for your information. The response of AndrewF is more informative covering debugging option & usage of while instead of for.
Hope this helps!

Upvotes: 4

AndrewF
AndrewF

Reputation: 2557

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 the git add command, it wouldn't work. So the loop is getting run with wrong arguments, as shown by this output with set -x:

ubuntu@up:~/test$ ls -1
a a
ubuntu@up:~/test$ set -x; for FILE in $(git ls-files -o --exclude-standard); do git add "$FILE"; git commit -m "Added $FILE"; done
+ set -x
++ git ls-files -o --exclude-standard
+ for FILE in '$(git ls-files -o --exclude-standard)'
+ git add a
...

The proper solution is to quote the git add $file and have git ls-files NULL separate the filenames by passing -z to git ls-files and use a while loop with a null delimiter:

git ls-files -o --exclude-standard -z | while read -r -d '' file; do
  git add "$file"
  git commit -m "Added $file"
  git push origin master
done

Upvotes: 10

Dan
Dan

Reputation: 10786

Replace git add $FILE with git add "$FILE". That way it will be interpreted as a single element.

Upvotes: 1

mipadi
mipadi

Reputation: 410582

Try putting the $FILE var in quotes:

git add "$FILE"

That'll quote the filename, thus allowing spaces in it.

Upvotes: 2

Related Questions