Thursday, May 29, 2014

Spaces in Filenames Cause git add-commit Error

Using my handy git add-commit command failed with a pathspec error

$ git add-commit 'added frontend'
fatal: pathspec 'bower_components/modernizr/media/Modernizr' did not match any files


Looks like the error is caused by files with spaces in the filename:

$ git untracked|grep Modernizr
bower_components/modernizr/media/Modernizr 2 Logo.ai
bower_components/modernizr/media/Modernizr 2 Logo.eps
bower_components/modernizr/media/Modernizr 2 Logo.pdf
bower_components/modernizr/media/Modernizr 2 Logo.png
bower_components/modernizr/media/Modernizr 2 Logo.svg


Temporary fix

Run the following command to generate git add commands that wrap the filename with a quote character:

$ (IFS=$'\n';files=(`git untracked|grep Modernizr`);echo "RUN THE FOLLOWING COMMANDS:";for file in "${files[@]}"; do echo "git add '"$file"'"; done)

RUN THE FOLLOWING COMMANDS:
git add 'bower_components/modernizr/media/Modernizr 2 Logo.ai'
git add 'bower_components/modernizr/media/Modernizr 2 Logo.eps'
git add 'bower_components/modernizr/media/Modernizr 2 Logo.pdf'
git add 'bower_components/modernizr/media/Modernizr 2 Logo.png'
git add 'bower_components/modernizr/media/Modernizr 2 Logo.svg'


Try again...


$ git add-commit


Success.

Enhance the add-commit command to handle spaces in filenames

Changed


$ git config --global alias.add-untracked '!git add $(git untracked)'


To


$ git config --global alias.add-untracked '!f() { files=(`git untracked`);for file in "${files[@]}"; do cmd="git add \""$file"\""; eval $cmd; done; }; f'



Note

In my git add --update :/ post, I show the "best" git commands to add, commit and push your code changes to your git repo.


References

http://lexsheehan.blogspot.com/2014/05/git-add-update.html
http://lexsheehan.blogspot.com/2014/05/process-array-of-values-in-bash-shell.html

No comments:

Post a Comment