浅谈 .gitignore文件的使用

浅谈 .gitignore文件的使用,第1张

浅谈 .gitignore文件的使用 CONTEXT:

​ 在mytest项目里存在mytest/test.sh,并且push到了远程仓库。后面需要不把这个test.sh文件push到远程仓库,需要让test.sh不被git管理。试过在.gitignore文件里添加规则/test.sh去忽略,但是每次对test.sh修改之后,执行git status还能看到test.sh的状态,说明没有被git忽略。

git check-ignore -v 定位到文件在 .gitignore 文件中的具体位置,可以用来查看哪里写了忽略规则把它忽略掉了,另外写相对于 当前工作目录mytest(mytest是项目名) 的相对路径。

  1. 在.gitignore文件中添加了规则/test.sh,忽略这个shell脚本。在.gitignore中写规则的时候写的是绝对路径,根目录对应的是mytest项目文件夹。
input:
git check-ignore -v ./test.sh

output:
.gitignore:12:/test.sh  ./test.sh

  1. 在.gitignore文件中没有添加规则/Cargo.toml
input:
git check-ignore -v ./Cargo.toml
output为空
  1. 查看当前项目里哪些文件/文件夹被git忽略管理

    git status --ignorede

最终解决方案
[xxx@ubuntu mytest]$ git status  //在.gitignore文件中添加了规则/test.sh后,对test.sh修改后,执行git status
# On branch feature/3.0/270
# Changes not staged for commit:
#   (use "git add ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#
#       modified:   test.sh     //说明未被git忽略管理
#
no changes added to commit (use "git add" and/or "git commit -a")
[xxx@ubuntu mytest]$ git rm --cached ./test.sh   //关键的一步
rm 'test.sh'
[xxx@ubuntu mytest]$ git status
# On branch feature/3.0/270
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
#       deleted:    test.sh          //这种状态就是这个test.sh被git忽略管理了
#
[xxx@ubuntu mytest]$ git status   //对test.sh文件做修改,然后再执行git status
# On branch feature/3.0/270
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
#       deleted:    test.sh       
#

.gitignore语法参考

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/5619537.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存