创建.gitignore文件并使用

创建 .gitignore文件

第一种方式

在项目根目录下直接创建一个文件,后缀改成 .gitignore 即可。

第二种方式

用git创建,到根目录下,执行 touch .gitignore,即可看见目录下已经出现了该忽略文件。

添加忽略规则

# 忽略所有以 .a 结尾的文件
*.a

# 不能忽略所有 lib.a 文件
!lib.a

# 仅仅忽略当前目录下的 TODO 文件
/TODO

# 忽略 build 目录下的所有文件
build/

# 仅仅忽略 doc 一个目录下的所有 .txt 文件
doc/*.txt

# 忽略 doc 目录下(包括子目录)的所有 .pdf 文件
doc/**/*.pdf

比如我这里想忽略掉available_models文件夹下的所有pt文件,
在这里插入图片描述
则我的 “.gitignore”文件里面的内容如下:
在这里插入图片描述

测试忽略文件是否有效

因为这个忽略文件是项目早已创建,并进行了git管理,忽略文件是后续根据需求加的,因此需要先删除git里面的跟踪缓存,不然忽略文件不会起作用。
删除缓存:git rm -r -f --cached .
然后 git add .
接着查看缓存区域,看.gitignore忽略的内容是否没有在: git ls-files
没有在就表示其作用了,然后再提交:git commit -m ".gitignore重写缓存"
最后push:git push

整个过程可能出现的问题

(1) 在 git add . 时报错

$ git add .
warning: adding embedded git repository: available_models
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.
hint: If you meant to add a submodule, use:
hint:
hint:   git submodule add <url> available_models
hint:
hint: If you added this path by mistake, you can remove it from the
hint: index with:
hint:
hint:   git rm --cached available_models
hint:
hint: See "git help submodule" for more information.

这个表示在这个目录下有一个git文件,应该删除。点击Windows文件资源管理器,点击上方查看,勾选隐藏的项目,既可以看见 .git文件夹,右键删除即可。
在这里插入图片描述

其他错误

push时报错

 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:taozhuowei/StoreMyToys.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解决办法,先pull,将项目拉下来。git pull --rebase origin master
然后再推,git push

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值