Git 忽略某些本地某些文件

Git 提交代码时经常被一些本地文件困扰,比如一些log, 用户上传的头像(jpg),还有拉到本地lib 等
这篇blog主要介绍通过 配置 .gitignore文件 解决这些问题。

1 测试环境

首先我们待测试文件结构如下:

2018/08/03  17:55    <DIR>          .
2018/08/03  17:55    <DIR>          ..
2018/08/03  17:43                 0 hello.log
2018/06/12  10:26            33,685 icon.jpg
2018/08/03  17:50    <DIR>          node_modules
2018/08/03  17:50               305 package-lock.json
2018/08/03  17:43                 0 test.js
2018/06/12  10:26            19,850 user.jpg

建立git

git init 
git status

查看待加入git的文件

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        hello.log
        icon.jpg
        node_modules/
        package-lock.json
        test.js
        user.jpg
nothing added to commit but untracked files present (use "git add" to track)

我们主要用git 跟踪的只有test.js icon.jpg这两个文件

2 使用.gitignore忽略本地文件

gitignore:
在Git工作区的根目录下创建一个特殊的.gitignore文件,可以把要忽略的文件名填进去,Git就会自动忽略这些文件。

.gitignore

# My configurations:
node_modules
*.log
*.jpg
!icon.jpg

语法

# 注释
*.log  *为通配符,所有.log都会被忽略
!icon.jpg  !为该文件不被忽略 取反
node_modules文件夹整个都被忽略

测试结果:

git status

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .gitignore
        icon.jpg
        package-lock.json
        test.js

nothing added to commit but untracked files present (use "git add" to track)

3 参考链接

https://www.cnblogs.com/wuchanming/p/5429763.html

猜你喜欢

转载自blog.csdn.net/m0_37263637/article/details/81392594
今日推荐