• 创建.gitignore文件并使用


    创建 .gitignore文件

    第一种方式

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

    第二种方式

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

    添加忽略规则

    # 忽略所有以 .a 结尾的文件
    *.a
    
    # 不能忽略所有 lib.a 文件
    !lib.a
    
    # 仅仅忽略当前目录下的 TODO 文件
    /TODO
    
    # 忽略 build 目录下的所有文件
    build/
    
    # 仅仅忽略 doc 一个目录下的所有 .txt 文件
    doc/*.txt
    
    # 忽略 doc 目录下(包括子目录)的所有 .pdf 文件
    doc/**/*.pdf
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    比如我这里想忽略掉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  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.
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    这个表示在这个目录下有一个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.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

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

  • 相关阅读:
    倍福触摸屏维修倍福显示屏维修CP3916-0000故障概述
    Redis7-分布式锁
    『现学现忘』Git基础 — 37、标签tag(二)
    对‘QBasicAtomicInt_fetchAndAddOrdered(int volatile*, int)’未定义的引用
    DALL E2【论文阅读】
    【活着活着就老了-冯唐】阅后
    elasticsearch安装步骤
    Charles抓取接口报文并修改各种参数信息调试
    面试官为啥总是喜欢问前端路由实现方式?
    NNLM、RNNLM等语言模型 实现 下一单词预测(next-word prediction)
  • 原文地址:https://blog.csdn.net/erdaidai/article/details/127924119