Git ls-files, how to avoid spaces in file paths?

All BUNCH files were checked in our git repository before we submitted the .gitignore file. I am currently trying to clear it:

git rm --cached `git ls-files -i --exclude-from=.gitignore` 

MANY file paths have spaces.

For instance:

 ...... Assets/Aoi Character Pack/Viewer/Resources/Aoi/Viewer BackGrounds/bg12.png.meta Assets/Aoi Character Pack/Viewer/Resources/Aoi/Viewer BackGrounds/bg13.png.meta Assets/Aoi Character Pack/Viewer/Resources/Aoi/Viewer BackGrounds/bg14.png.meta Assets/Aoi Character Pack/Viewer/Resources/Aoi/Viewer BackGrounds/bg15.png.meta Assets/Aoi Character Pack/Viewer/Resources/Aoi/Viewer BackGrounds/bg16.png.meta Assets/Aoi Character Pack/Viewer/Resources/Aoi/Viewer BackGrounds/bg17.png.meta Assets/Aoi Character Pack/Viewer/Resources/Aoi/Viewer BackGrounds/bg18.png.meta ....... 

There ALL ALL LOTTA SPACES in general Lotta files that I need to get rid of.

I am looking for an elegant way:

  • A: Avoid spaces with "\", for example

    Assets/Aoi\ Character Pack/Viewer/Resources/Aoi/Viewer\ BackGrounds/bg18.png.meta

-or -

  • B: git ls-files deflate my list of files neatly sandwiched between quotation marks.

    'Assets/Aoi Character Pack/Viewer/Resources/Aoi/Viewer BackGrounds/bg18.png.meta'

-Edit -

So far I have tried git ls-files -i --exclude-from=.gitignore | sed 's/\ /\\\ /g' git ls-files -i --exclude-from=.gitignore | sed 's/\ /\\\ /g'

While it happily outputs file paths that look the way I expected, with spaces running through ..... When I try to execute

 git rm --cached `git ls-files -i --exclude-from=.gitignore | sed 's/\ /\\\ /g'` 

I get - error: unknown switch `\ '

Where I expect something elusive to happen to the pipe.

+5
source share
2 answers

Canonical path:

 git ls-files -z | xargs -0 git rm 

another way:

 git ls-files | xargs -d '\n' git rm 
+3
source

Rachel Duncan answered me in the right direction and I found a solution that works! I also borrowed tips from https://superuser.com/questions/401614/inserting-string-from-xargs-into-another-string

 git ls-files -i --exclude-from=.gitignore | tr '\n' '\0' | xargs -0 -L1 -I '$' git rm --cached '$' 

The above script compiles a list of all of your versions of git, which are now included in the .gitignore exception rules, wraps quotes around each std line (file path), then runs the git rm --cached command with the modified line.

I like this way because the terminal gives confirmation for each of the files that it deletes from the original control.

Hooray!

+2
source

Source: https://habr.com/ru/post/1242734/


All Articles