https://git-scm.com/docs/gitignore
# 2.2 Git 基础 - 记录每次更新到仓库
https://git-scm.com/book/zh/v2/Git-%E5%9F%BA%E7%A1%80-%E8%AE%B0%E5%BD%95%E6%AF%8F%E6%AC%A1%E6%9B%B4%E6%96%B0%E5%88%B0%E4%BB%93%E5%BA%93
# github 提供的文件样例
https://github.com/github/gitignore
.idea
*.iml
.log/
/order
/customer/
.gitignore
.idea
忽略 .gitignore 所在目录下所有路径中的 .idea 文件和文件夹
*.iml
忽略 .gitignore 所在目录下所有路径中的 .iml 为后缀的文件和文件夹
.log/
忽略 .gitignore 所在目录下所有路径中的 .log 文件夹
/order
忽略 .gitignore 所在目录下的 order 文件和文件夹
/customer/
忽略 .gitignore 所在目录下的 customer 文件夹
.gitignore
可以忽略自己
一般我们总会有些文件无需纳入 Git 的管理,也不希望它们总出现在未跟踪文件列表。 通常都是些自动生成的文件,比如日志文件,或者编译过程中创建的临时文件等。 在这种情况下,我们可以创建一个名为 .gitignore
的文件,列出要忽略的文件模式。 来看一个实际的例子:
$ cat .gitignore
*.[oa]
*~
第一行告诉 Git 忽略所有以 .o
或 .a
结尾的文件。一般这类对象文件和存档文件都是编译过程中出现的。 第二行告诉 Git 忽略所有以波浪符(~)结尾的文件,许多文本编辑软件(比如 Emacs)都用这样的文件名保存副本。 此外,你可能还需要忽略 log,tmp 或者 pid 目录,以及自动生成的文档等等。 要养成一开始就设置好 .gitignore 文件的习惯,以免将来误提交这类无用的文件。
文件 .gitignore
的格式规范如下:
#
开头的行都会被 Git 忽略。/
)开头防止递归。/
)结尾指定目录。!
)取反。所谓的 glob 模式是指 shell 所使用的简化了的正则表达式。 星号(*
)匹配零个或多个任意字符;[abc]
匹配任何一个列在方括号中的字符(这个例子要么匹配一个 a,要么匹配一个 b,要么匹配一个 c);问号(?
)只匹配一个任意字符;如果在方括号中使用短划线分隔两个字符,表示所有在这两个字符范围内的都可以匹配(比如 [0-9]
表示匹配所有 0 到 9 的数字)。 使用两个星号(*
) 表示匹配任意中间目录,比如 a/**/z
可以匹配 a/z
, a/b/z
或 a/b/c/z
等。
我们再看一个 .gitignore 文件的例子:
# no .a files
*.a
# but do track lib.a, even though you're ignoring .a files above
!lib.a
# only ignore the TODO file in the current directory, not subdir/TODO
/TODO
# ignore all files in the build/ directory
build/
# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt
# ignore all .pdf files in the doc/ directory
doc/**/*.pdf
GitHub 有一个十分详细的针对数十种项目及语言的 .gitignore
文件列表,你可以在 https://github.com/github/gitignore 找到它.
添加后这个文件的修改,删除都会被管理。
1@DESKTOP-3H9092J MINGW64 /e/x1/x1/demo1 (master)
$ git add .log/1.txt
The following paths are ignored by one of your .gitignore files:
.log/1.txt
Use -f if you really want to add them.
1@DESKTOP-3H9092J MINGW64 /e/x1/x1/demo1 (master)
$ git add -f .log/1.txt
1@DESKTOP-3H9092J MINGW64 /e/x1/x1/demo1 (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .log/1.txt
1@DESKTOP-3H9092J MINGW64 /e/x1/x1/demo1 (master)
$ git status
On branch master
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: .log/1.txt
1@DESKTOP-3H9092J MINGW64 /e/x1/x1/demo1 (master)
$ git add .log/1.txt
1@DESKTOP-3H9092J MINGW64 /e/x1/x1/demo1 (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: .log/1.txt
1@DESKTOP-3H9092J MINGW64 /e/x1/x1/demo1 (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: .log/1.txt
1@DESKTOP-3H9092J MINGW64 /e/x1/x1/demo1 (master)
$ git add .log/1.txt
1@DESKTOP-3H9092J MINGW64 /e/x1/x1/demo1 (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: .log/1.txt
在 .gitignore 中忽略原本被管理的 *.iml
文件。然后删除被管理的 *.iml
文件。提交这次删除。那么下次如果
idea再次生成了 *.iml
文件,这时就会被忽略了。如果你想强制添加,则可以使用上面的。
原文:https://www.cnblogs.com/mozq/p/11718506.html