git ignore for directories with spaces on Mac OS X

断了今生、忘了曾经 提交于 2019-12-22 04:45:24

问题


I'm trying to add some patterns to my .gitignore file to ignore *.mode1v3 and *.pbxuser files generated by Xcode. However, my app name has a space in it, so the files I want to ignore are in the Foo Bar.xcodeproj/ directory. Adding variants of these patterns don't seem to work:

*.mode1v3
Foo Bar.xcodeproj/
Foo Bar.xcodeproj/*.mode1v3
Foo Bar.xcodeproj/username.mode1v3

What should the .gitignore patterns be?


回答1:


AFAIK spaces aren't treated specially; neither Pro Git nor gitignore(5) nor fnmatch(3) mentions them. Anyway the first pattern *.mode1v3 is totally sufficient; patterns without slashes are applied to all subdirectories. If you want additional ignore patters for a specific subdirectory, just place a dedicated .gitignore in that directory.




回答2:


Have you tried escaping any spaces in the folder or file names with backslashes?

*.mode1v3
Foo\ Bar.xcodeproj/
Foo\ Bar.xcodeproj/*.mode1v3
Foo\ Bar.xcodeproj/username.mode1v3

Also, are these files already being tracked by git? From man gitignore:

A gitignore file specifies intentionally untracked files that git should ignore.
Note that all the gitignore files really concern only files that are not already
tracked by git; in order to ignore uncommitted changes in already tracked files,
please refer to the git update-index --assume-unchanged documentation.

Additionally, here are some of the patterns discussed in man gitignore:

o   If the pattern ends with a slash, it is removed for the purpose of the
    following description, but it would only find a match with a directory. In
    other words, foo/ will match a directory foo and paths underneath it, but will
    not match a regular file or a symbolic link foo (this is consistent with the
    way how pathspec works in general in git).

o   If the pattern does not contain a slash /, git treats it as a shell glob
    pattern and checks for a match against the pathname relative to the location of
    the .gitignore file (relative to the toplevel of the work tree if not from a
    .gitignore file).

o   Otherwise, git treats the pattern as a shell glob suitable for consumption by
    fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will not match
    a / in the pathname. For example, "Documentation/*.html" matches
    "Documentation/git.html" but not "Documentation/ppc/ppc.html" or
    "tools/perf/Documentation/perf.html".

o   A leading slash matches the beginning of the pathname. For example, "/*.c"
    matches "cat-file.c" but not "mozilla-sha1/sha1.c".


来源:https://stackoverflow.com/questions/3675641/git-ignore-for-directories-with-spaces-on-mac-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!