当前位置:   article > 正文

Visual Studio使用Git忽略不想上传到远程仓库的文件_vscode git忽略上传

vscode git忽略上传

前言

作为一个.NET开发者而言,有着宇宙最强IDE:Visual Studio加持,让我们的开发效率得到了更好的提升。我们不需要担心环境变量的配置和其他代码管理工具,因为Visual Studio有着众多的拓展工具。废话不多说,直接进入正题。我们日常在使用Visual Studio开发相关的.NET项目时,经常会发现刚拉取下拉的代码什么都没有改动,就是运行了一下就会产生一些需要提交的文件,比如说最常见的bin/Debug, bin/Release,obj/Debug,obj/Release文件。但是我不想把这些文件提交到远程的git代码远程仓库中去,其实这个很简单只需要我们在初次创建项目的时候在项目目录下新增一个忽略文本文件(.gitignore),然后在使用git推送到远程仓库中就好了。

需要忽略提交的文件

图片

配置.gitignore忽略文件,忽略不需要提交的文件

添加Visual Studio的.gitgnore模板到项目中

图片

C#/.NET/.NET Core相关项目的忽略文件模板(VisualStudio.gitignore)

只要是使用VisualStudio开发的项目都可以使用该模板。

  • VisualStudio.gitignore模板GitHub开源地址(该项目中有众多的编程语言或IDE相关.gitgnore配置,可以下载下来直接使用):https://github.com/github/gitignore/blob/master/VisualStudio.gitignore

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

.gitignore忽略规则

基本语法:

  • 以”#”号开头表示注释;

  • 以斜杠“/”开头表示目录;

  • 以星号“*”通配多个字符;

  • 以问号“?”通配单个字符;

  • 以方括号“[]”包含单个字符的匹配列表;

  • 以叹号“!”表示不忽略(跟踪)匹配到的文件或目录;

  • 此外,git 对于 .ignore 配置文件是按行从上到下进行规则匹配的,意味着如果前面的规则匹配的范围更大,则后面的规则将不会生效;

代码示例

  1. # 此为注释 – 将被 Git 忽略
  2. *.a # 忽略所有 .a 结尾的文件
  3. !lib.a # 但 lib.a 除外
  4. /TODO # 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
  5. build/ # 忽略 build/ 目录下的所有文件
  6. doc/*.txt # 会忽略 doc/notes.txt 但不包括 doc/server/arch.txt

处理已提交到远程仓库中的文件

可能在开发的时候忘记添加忽略文件配置(gitignore),把这些文件推送到远程仓库中,我们有以下两种做法解决:

  • 1、把本地这些文件删除再提交到远端(前提是这些文件是环境变量文件,假如是无法自动生成的不推荐)。

  • 2、使用以下git 命令:

  1. //当我们需要删除暂存区或分支上的文件, 但本地又需要使用, 只是不希望这个文件被版本控制, 可以使用
  2. git rm -r --cached file_path(文件路径)  //file_path文件在当前分支的暂存区中删除
  3. git add .                               //提交当前操作本地暂存区
  4. git commit -m 'delete remote somefile'  //提交到本地git仓库
  5. git push                                //提交到远程代码库

如我需要移除某个项目中的obj文件然后提交:

该文件路径为:D:\开发学习-Master\我的训练营\DailyLearning\工作&学习.Net训练营\Application\obj

输入以下命令移除相关不需要跟踪的文件:

  1. git rm -r --cached Application/obj

执行成功后,文件将从该分支中删除(本地还会存在),最后我们提交到远程仓库即可。

图片

图片

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号