浅谈 .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 <filename> 定位到文件在 .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 <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." 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 <file>..." 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 <file>..." to unstage)
#
#       deleted:    test.sh       
#

.gitignore语法参考

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值