博客
关于我
git在commit时候不commit某些文件
阅读量:683 次
发布时间:2019-03-17

本文共 887 字,大约阅读时间需要 2 分钟。

前言

不多个版本控制系统的用户都会遇到这样的问题:某些文件不应该提交到版本控制系统中。本文将详细介绍两种常见情况,并提供相应的解决方案。
  • 这种文件从未推送到仓库
  • 该文件已被推送到仓库,但不希望后续更改生效(如包含密码的配置文件)

第一种情况:文件从未推送到仓库

想避免某些文件被提交到仓库,可以通过创建一个.gitignore文件来实现。添加相应的规则后,运行git add .时,这些文件将被排除在外,从而不会提交到仓库。此外,IDE(如IntelliJ IDEA)自动生成的Spring Boot项目通常会自动生成一个.gitignore文件,内容如下:
HELP.md  target/!  .mvn/wrapper/maven-wrapper.jar  **/src/main/**/target/  **/src/test/**/target/  注释:#**/src/main/java/com/Property.java  注释:#**/src/main/resources/application.properties
如果已经将文件提交到缓存区但希望撤销,可以使用以下命令:
git rm -r --cached src/main/java/test.java git add .
这样提交后,该文件将不再出现在仓库中。

第二种情况:文件已推送到仓库,但不希望后续更改生效

有时候你想要保留仓库中的某些properties文件,但又不想提交包含密码的文件。这时可以使用以下方法:
git update-index --assume-unchanged src/java/test.java
这样再次提交时,该文件将被忽略。当想恢复跟踪时,可以运行:
git update-index --no-assume-unchanged src/java/test.java
需要注意的是,在IDE中,设置成功后,文件修改将不会显示未提交状态,而是显示为白色表示已提交。
总之,Git提供了灵活的版本控制工具,可以根据实际需求选择适当的方法来管理文件提交和忽略问题。

转载地址:http://yczhz.baihongyu.com/

你可能感兴趣的文章
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>