经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
Git忽略提交规则
来源:cnblogs  作者:lingfeng95  时间:2019/7/29 11:23:10  对本文有异议

现在开源项目越来越多,Git使用越来越方便,用Git的人也越来越多。创建项目的时候,喜欢把日志,临时文件,项目编译的中间文件,引用的类库等等,这时就要设置响应的规则,来忽略这些文件。例如创建一个C#项目,项目下面会有.vs,bin,obj等,这些都是不需要提交的需要忽略的,如何忽略呢?其实很简单,增加.gitignore文件就可以了,下面将详细说明C#项目的忽视规则。

创建Git项目

创建git项目,创建的时候有个.gitignore的选项,根据自己的需要选择需要忽视的文件,例如:java,python等,C#的项目选择VisualStudio,因为创建的都是VS项目,这里面有需要C#项目所忽略的设置。创建完项目之后,项目下面就有个.gitignore文件,这个文件是可以修改的。

忽视文件

例如:忽视后缀是.suo,.user的文件,*正式所有

  1. *.suo
  2. *.user

忽视文件夹

[Dd]用的是正则,匹配大小写d,两个**表示匹配任意中间目录

  1. [Dd]ebug/
  2. [Dd]ebugPublic/
  3. [Rr]elease/
  4. [Rr]eleases/
  5. **/[Pp]ackages/*

Git忽略文件的原则

1. 编译生成的文件,例如:dll,等
2. 配置文件,敏感的信息(密码,口令)
3. 项目工具生成的文件,例如:引用Nuget时生成的packages等
4. 项目测试的文件,日志文件等。

.gitignore忽略规则的匹配语法

1. “#"开头,是git上的注释说明
2. 以"/"开头表示根目录,其他位置的“/”表示的是忽视目录
3.“*”匹配多个字符,“**”匹配任意中间目录
4.“[]”匹配中括号中的字符
5.“!”表示不忽视,即:如果忽视某些文件,可以用!让这些文件在启用,如果文件夹的父目录被忽略了,那么这个文件在启用,也就没有什么用了。
注意:git对于.ignore配置文件是从上到下进行规则匹配的,如果前面的规则匹配的范围更大,则后面的规则将不会生效;git上的忽视文件.ignore要在创建git的时候创建,如果是之后创建的,已经push过的,在忽视将不起作用,因为git已经开始管理这些文件了。

.gitignore查看忽视文件和文件夹

  1. git check-ignore -v bin
  2. .gitignore:23:[Bb]in/ bin

查看忽视文件夹,可以看到,第23行的忽视规则把bin给忽视掉了,如果运行指令,没有返回值则表示没有忽视此文件或者文件夹的规则。

git忽视已经上传的文件解决办法

在使用git的时候,创建了一个.gitignore文件,但是后来发现有一个文件已经上传了,没有写忽视规则,但是又想这个文件【本地保存】,【远程删除】,例如:忽视已经上传的文件【test.txt】或者文件夹,用git rm -r –cached directory
1. git rm -r --cached test.txt
2. commit和push
操作之后,你可以看到本地文件还在,远程的已经删除了

VS项目忽视文件(C#、.NET,MVC等)

  1. ## Ignore Visual Studio temporary files, build results, and
  2. ## files generated by popular Visual Studio add-ons.
  3. ##
  4. ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
  5. # User-specific files
  6. *.suo
  7. *.user
  8. *.userosscache
  9. *.sln.docstates
  10. # User-specific files (MonoDevelop/Xamarin Studio)
  11. *.userprefs
  12. # Build results
  13. [Dd]ebug/
  14. [Dd]ebugPublic/
  15. [Rr]elease/
  16. [Rr]eleases/
  17. x64/
  18. x86/
  19. bld/
  20. [Bb]in/
  21. [Oo]bj/
  22. [Ll]og/
  23. # Visual Studio 2015/2017 cache/options directory
  24. .vs/
  25. # Uncomment if you have tasks that create the project's static files in wwwroot
  26. #wwwroot/
  27. # Visual Studio 2017 auto generated files
  28. Generated\ Files/
  29. # MSTest test Results
  30. [Tt]est[Rr]esult*/
  31. [Bb]uild[Ll]og.*
  32. # NUNIT
  33. *.VisualState.xml
  34. TestResult.xml
  35. # Build Results of an ATL Project
  36. [Dd]ebugPS/
  37. [Rr]eleasePS/
  38. dlldata.c
  39. # Benchmark Results
  40. BenchmarkDotNet.Artifacts/
  41. # .NET Core
  42. project.lock.json
  43. project.fragment.lock.json
  44. artifacts/
  45. **/Properties/launchSettings.json
  46. # StyleCop
  47. StyleCopReport.xml
  48. # Files built by Visual Studio
  49. *_i.c
  50. *_p.c
  51. *_i.h
  52. *.ilk
  53. *.meta
  54. *.obj
  55. *.iobj
  56. *.pch
  57. *.pdb
  58. *.ipdb
  59. *.pgc
  60. *.pgd
  61. *.rsp
  62. *.sbr
  63. *.tlb
  64. *.tli
  65. *.tlh
  66. *.tmp
  67. *.tmp_proj
  68. *.log
  69. *.vspscc
  70. *.vssscc
  71. .builds
  72. *.pidb
  73. *.svclog
  74. *.scc
  75. # Chutzpah Test files
  76. _Chutzpah*
  77. # Visual C++ cache files
  78. ipch/
  79. *.aps
  80. *.ncb
  81. *.opendb
  82. *.opensdf
  83. *.sdf
  84. *.cachefile
  85. *.VC.db
  86. *.VC.VC.opendb
  87. # Visual Studio profiler
  88. *.psess
  89. *.vsp
  90. *.vspx
  91. *.sap
  92. # Visual Studio Trace Files
  93. *.e2e
  94. # TFS 2012 Local Workspace
  95. $tf/
  96. # Guidance Automation Toolkit
  97. *.gpState
  98. # ReSharper is a .NET coding add-in
  99. _ReSharper*/
  100. *.[Rr]e[Ss]harper
  101. *.DotSettings.user
  102. # JustCode is a .NET coding add-in
  103. .JustCode
  104. # TeamCity is a build add-in
  105. _TeamCity*
  106. # DotCover is a Code Coverage Tool
  107. *.dotCover
  108. # AxoCover is a Code Coverage Tool
  109. .axoCover/*
  110. !.axoCover/settings.json
  111. # Visual Studio code coverage results
  112. *.coverage
  113. *.coveragexml
  114. # NCrunch
  115. _NCrunch_*
  116. .*crunch*.local.xml
  117. nCrunchTemp_*
  118. # MightyMoose
  119. *.mm.*
  120. AutoTest.Net/
  121. # Web workbench (sass)
  122. .sass-cache/
  123. # Installshield output folder
  124. [Ee]xpress/
  125. # DocProject is a documentation generator add-in
  126. DocProject/buildhelp/
  127. DocProject/Help/*.HxT
  128. DocProject/Help/*.HxC
  129. DocProject/Help/*.hhc
  130. DocProject/Help/*.hhk
  131. DocProject/Help/*.hhp
  132. DocProject/Help/Html2
  133. DocProject/Help/html
  134. # Click-Once directory
  135. publish/
  136. # Publish Web Output
  137. *.[Pp]ublish.xml
  138. *.azurePubxml
  139. # Note: Comment the next line if you want to checkin your web deploy settings,
  140. # but database connection strings (with potential passwords) will be unencrypted
  141. *.pubxml
  142. *.publishproj
  143. # Microsoft Azure Web App publish settings. Comment the next line if you want to
  144. # checkin your Azure Web App publish settings, but sensitive information contained
  145. # in these scripts will be unencrypted
  146. PublishScripts/
  147. # NuGet Packages
  148. *.nupkg
  149. # The packages folder can be ignored because of Package Restore
  150. **/[Pp]ackages/*
  151. # except build/, which is used as an MSBuild target.
  152. !**/[Pp]ackages/build/
  153. # Uncomment if necessary however generally it will be regenerated when needed
  154. #!**/[Pp]ackages/repositories.config
  155. # NuGet v3's project.json files produces more ignorable files
  156. *.nuget.props
  157. *.nuget.targets
  158. # Microsoft Azure Build Output
  159. csx/
  160. *.build.csdef
  161. # Microsoft Azure Emulator
  162. ecf/
  163. rcf/
  164. # Windows Store app package directories and files
  165. AppPackages/
  166. BundleArtifacts/
  167. Package.StoreAssociation.xml
  168. _pkginfo.txt
  169. *.appx
  170. # Visual Studio cache files
  171. # files ending in .cache can be ignored
  172. *.[Cc]ache
  173. # but keep track of directories ending in .cache
  174. !*.[Cc]ache/
  175. # Others
  176. ClientBin/
  177. ~$*
  178. *~
  179. *.dbmdl
  180. *.dbproj.schemaview
  181. *.jfm
  182. *.pfx
  183. *.publishsettings
  184. orleans.codegen.cs
  185. # Including strong name files can present a security risk
  186. # (https://github.com/github/gitignore/pull/2483#issue-259490424)
  187. #*.snk
  188. # Since there are multiple workflows, uncomment next line to ignore bower_components
  189. # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
  190. #bower_components/
  191. # RIA/Silverlight projects
  192. Generated_Code/
  193. # Backup & report files from converting an old project file
  194. # to a newer Visual Studio version. Backup files are not needed,
  195. # because we have git ;-)
  196. _UpgradeReport_Files/
  197. Backup*/
  198. UpgradeLog*.XML
  199. UpgradeLog*.htm
  200. ServiceFabricBackup/
  201. *.rptproj.bak
  202. # SQL Server files
  203. *.mdf
  204. *.ldf
  205. *.ndf
  206. # Business Intelligence projects
  207. *.rdl.data
  208. *.bim.layout
  209. *.bim_*.settings
  210. *.rptproj.rsuser
  211. # Microsoft Fakes
  212. FakesAssemblies/
  213. # GhostDoc plugin setting file
  214. *.GhostDoc.xml
  215. # Node.js Tools for Visual Studio
  216. .ntvs_analysis.dat
  217. node_modules/
  218. # Visual Studio 6 build log
  219. *.plg
  220. # Visual Studio 6 workspace options file
  221. *.opt
  222. # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
  223. *.vbw
  224. # Visual Studio LightSwitch build output
  225. **/*.HTMLClient/GeneratedArtifacts
  226. **/*.DesktopClient/GeneratedArtifacts
  227. **/*.DesktopClient/ModelManifest.xml
  228. **/*.Server/GeneratedArtifacts
  229. **/*.Server/ModelManifest.xml
  230. _Pvt_Extensions
  231. # Paket dependency manager
  232. .paket/paket.exe
  233. paket-files/
  234. # FAKE - F# Make
  235. .fake/
  236. # JetBrains Rider
  237. .idea/
  238. *.sln.iml
  239. # CodeRush
  240. .cr/
  241. # Python Tools for Visual Studio (PTVS)
  242. __pycache__/
  243. *.pyc
  244. # Cake - Uncomment if you are using it
  245. # tools/**
  246. # !tools/packages.config
  247. # Tabs Studio
  248. *.tss
  249. # Telerik's JustMock configuration file
  250. *.jmconfig
  251. # BizTalk build output
  252. *.btp.cs
  253. *.btm.cs
  254. *.odx.cs
  255. *.xsd.cs
  256. # OpenCover UI analysis results
  257. OpenCover/
  258. # Azure Stream Analytics local run output
  259. ASALocalRun/
  260. # MSBuild Binary and Structured Log
  261. *.binlog
  262. # NVidia Nsight GPU debugger configuration file
  263. *.nvuser
  264. # MFractors (Xamarin productivity tool) working folder
  265. .mfractor/

 

原文链接:http://www.cnblogs.com/zhao123/p/11262540.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号