git ignore 的使用

.gitignore NuGet exclude packages/ include packages/repositories.config

回答1

I faced the same issue.

None of the above solutions worked for me. And I think it's a poor solution to maintain multiple .ignore files.

This is how I solved it.

**/packages/*
!**/packages/repositories.config

Combining two asterisks will match any string of folders. I thought leaving out asterisks would have the same effect, but apparently I (we) were wrong, as it doesn't seem to work.

The official .gitignore template for Visual Studio recommends the following solutions:

# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/packages/*
# except build/, which is used as an MSBuild target.
!**/packages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/packages/repositories.config

EDIT: You can use https://www.gitignore.io to generate .ignore file for your favorite project :-)

 

This is an example of a .gitignore file, which is used by Git to determine which files and directories should be ignored when tracking changes in a repository. In this example, the file contains a comment and a few ignore patterns specific to NuGet package management in Visual Studio.

  • The first pattern *.nupkg is used to ignore NuGet package files that typically have this extension. This is a common file extension for software packages in the .NET ecosystem, which are often managed through the NuGet package manager.

  • The second pattern **/packages/* ignores the packages directory and its contents, which is typically where NuGet packages are installed by default. This is because when using package restore, it is not necessary to commit the packages to source control, since they can be downloaded automatically when needed.

  • The third pattern !**/packages/build/ is a negative pattern that "unignores" the build subdirectory of the packages directory. This is because this directory is used as an MSBuild target, and should not be ignored.

  • The fourth pattern #!**/packages/repositories.config is commented out using the #! syntax. This is because it is not always necessary to ignore the repositories.config file, which is used to specify package sources for NuGet. If this file is not ignored, it can be useful to track changes to the package sources used by a project. However, this is commented out because it is generally regenerated automatically when needed.

 

Difference between pattern packages and **/packages/*

In the context of a .gitignore file, packages matches any folder or file named exactly "packages" in the root of the repository or in any subdirectory. It would ignore, for example, a folder named "packages" in the root, but not a folder named "sub/packages".

On the other hand, **/packages/* is a glob pattern that matches any folder or file that is under a folder named "packages" in the repository. The ** prefix matches any number of subdirectories, so it would match, for example, a file at src/foo/packages/bar.txt. It would not match a file at packages/bar.txt because it is not inside a folder named "packages".

The difference is that the first pattern only matches files or folders with the exact name "packages" in the root of the repository or in a subdirectory. The second pattern matches any file or folder that is inside a folder named "packages", regardless of its location in the repository.

 

 

 

http://www.barretlee.com/blog/2015/09/06/set-gitignore-after-add-file/

 

需要注意的 **

  • 如果一个 pattern 以 ** 开头,如 **/foo,最后会匹配所有文件夹下的 foo 文件(夹);
  • 如果一个 pattern 以 /** 开头,如 abc/**,则表示匹配 abc 目录下的所有内容;
  • 如果一个 pattern 中间包含 **,如 a/**/b,则会匹配 a/ba/x/ba/x/y/b以及所有类似的内容。

需要找个时间验证下

 

 

忽略指定文件夹下的指定文件

**/Properties/launchSettings.json   Properties即使是子文件夹也能生效

 

如果ignore不生效的话,需要注意,有可能是文字后面的空格导致的。

bin

obj

这些忽略,后面一定不要多出空格

 

https://stackoverflow.com/questions/5533050/gitignore-exclude-folder-but-include-specific-subfolder

 

posted @ 2015-09-12 18:05  ChuckLu  阅读(331)  评论(0编辑  收藏  举报