『现学现忘』Git基础 — 19、在Git中进行忽略文件操作

🚀 优质资源分享 🚀

学习路线指引(点击解锁)知识定位人群定位

进阶级本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。

入门级手把手带你打造一个易扩展、更安全、效率更高的量化交易系统

1、忽略文件说明

有些时候,你必须把某些文件放到Git工作目录中,但又不能提交它们到本地版本库,通常都是些自动生成的文件、日志文件、系统缓存文件、编译过程中创建的临时文件,或者保存了数据库密码的配置文件、人隐私文件等等,每次执行 git status命令时,都会显示 Untracked files ...(未被跟踪),有强迫症的童鞋心里肯定不爽。

正常来说,这些文件都是不应该被提交到版本库,倘若一不留神提交到版本库,要么泄露机密信息,要么造成经济损失,要么对团队其他人工作造成不便。

好在Git考虑到了大家的感受,这个问题解决起来也很简单,在Git工作区的根目录下创建一个特殊的 .gitignore文件,然后把要忽略的文件名填进去,并且把 .gitignore文件提交到本地版本库中。之后Git就会自动忽略这些文件,再运行 git status命令时就不对其进行检测了。

即:”并不是所有的牛奶都叫特仑苏”,在 版本控制系统中也有相似的表达,那就是”并不是所有的文件都需要提交到本地版本库”。

2、忽略文件的原则

目标:只提交必要文件,忽略无用文件,尽可能考虑多种情况,不给他人制造麻烦。

3、 .gitignore 忽略规则

文件 .gitignore 的格式规范如下:

  • 所有空行或者以 # 开头的行都会被 Git 忽略。
  • 可以使用标准的 glob模式匹配,它会递归地应用在整个工作区中。
  • 匹配模式可以以( /)开头,防止递归。
  • 匹配模式最后跟 /说明要忽略的是目录。
  • 要忽略指定模式以外的文件或目录,可以在模式前加上叹号( !)取反。

所谓的 glob 模式是指 shell 所使用的简化了的正则表达式。

  • 以#开头的行用作注释。
  • 星号( *)匹配零个或多个任意字符。
  • [abc]匹配任何一个列在方括号中的字符 (这个例子要么匹配一个 a,要么匹配一个 b,要么匹配一个 c);
  • 问号( ?)只匹配一个任意字符。
  • 如果在方括号中使用短划线分隔两个字符, 表示所有在这两个字符范围内的都可以匹配(比如 [0-9] 表示匹配所有 0 到 9 的数字)。
  • 使用两个星号( **)表示匹配任意中间目录,比如 a/**/z 可以匹配 a/za/b/za/b/c/z 等。

我们再看一个 .gitignore 文件的例子:

shell

1.忽略public下的所有目录及文件
/public/*

2.不忽略/public/assets,就是特例的意思,assets文件不忽略
!/public/assets

3.忽略具体的文件
index.html

4.忽略所有的java文件
*.java

5.忽略 a.java b.java
[ab].java

6.忽略 doc/ 目录及其所有子目录下的 .pdf 文件
doc/**/*.pdf

7.忽略 doc/notes.txt,但不忽略 doc/server/arch.txt
doc/*.txt

8.忽略任何目录下名为 build 的文件夹
build/

9.只忽略当前目录下的 TODO 文件,而不忽略 subdir/TODO文件
/TODO

提示:要养成一开始就为你的新仓库设置好 .gitignore文件的习惯,以免将来误提交这类无用的文件。

4、忽略文件的三种方式

有鉴于此,我们应该寻求一种机制来规避这种事故的发生,在 git 版本控制系统中一般有三种不同的解决方案。

最常用也是最简单的当属 .gitignore 文件。

该方式是在工作中最常用的方式,配置方法是在仓库的根目录下新建 .gitignore 文件,在文件里配置忽略规则,同时把 .gitignore 文件加入版本管理。之后可以同步该配置到远程仓库,其他开发者可更新该文件到本地仓库,与其他克隆仓库共享同一套忽略规则。

.gitignore文件对其所在的目录,及所在目录的全部子目录均有效。

1)查看当前Git工作目录中文件状态。

bash

L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git status
On branch master
Untracked files:
  (use "git add ..." to include in what will be committed)
        hello.java
        local.txt
        test.java
        xyj-sha\_hs.py
        xyj-sun\_wk.py
        xyj-zhu\_bj.py

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

可以看到有6个未被追踪的文件。

2)在Git仓库的根目录下新建一个名为 .gitignore 的文件。

因为是点开头,没有文件名,没办法直接在Windows系统中直接创建,要通过Git Bash客户端,用Linux系统的方式新建 .gitignore文件。

bash

创建.gitignore文件。
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ touch .gitignore

查看本地版本库中是否成功创建.gitignore文件。
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ ll -a
total 12
drwxr-xr-x 1 L 197121 0  4月 10 11:52 ./
drwxr-xr-x 1 L 197121 0  4月  2 21:54 ../
drwxr-xr-x 1 L 197121 0  4月 10 11:51 .git/
-rw-r--r-- 1 L 197121 0  4月 10 11:52 .gitignore
-rw-r--r-- 1 L 197121 0  4月  4 10:37 hello.java
-rw-r--r-- 1 L 197121 0  4月 10 11:46 local.txt
-rw-r--r-- 1 L 197121 0  4月  4 01:20 test.java
-rw-r--r-- 1 L 197121 0  4月  4 10:38 xyj-sha\_hs.py
-rw-r--r-- 1 L 197121 0  4月  4 10:38 xyj-sun\_wk.py
-rw-r--r-- 1 L 197121 0  4月  4 10:38 xyj-zhu\_bj.py

3)将需要忽略的文件写入 .gitignore 文件中。

提示:每个文件或者正则匹配占一行。

bash

配置.gitignore文件内容
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ echo "xyj-*.py" > .gitignore

查看.gitignore文件内容
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ cat .gitignore
xyj-*.py

4)将 .gitignore 文件提交到本地版本库。

bash

1.查看当前Git工作目录中文件状态
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git status
On branch master
Untracked files:
  (use "git add ..." to include in what will be committed)
        .gitignore  # 文件名都为红色
        hello.java
        local.txt
        test.java

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

我们可以看到,xyj开头的文件已经被忽略掉了,但只是在本地起作用。

2.追踪.gitignore文件
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git add .gitignore

L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged ..." to unstage)
        new file:   .gitignore  # 文件名为绿色

Untracked files:
  (use "git add ..." to include in what will be committed)
        hello.java  # 文件名都为红色
        local.txt
        test.java

3.提交.gitignore文件到本地版本库
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git commit -m 'create ignore file'
[master d3ceba8] create ignore file
 1 file changed, 1 insertion(+)
 create mode 100644 .gitignore

L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git status
On branch master
Untracked files:
  (use "git add ..." to include in what will be committed)
        hello.java  # 文件名都为红色
        local.txt
        test.java

这样就完成可忽略文件的整个过程了。

用这种方式配置忽略文件,不会同步该设置至远程仓库,只在本机起作用。

配置方法是直接编辑Git仓库根目录下的 .git/info/exclude文件,把要忽略的规则直接写入,这个方法只在本机当前仓库起效,不会对其他的克隆仓库起效。

1)查看当前Git工作目录中文件状态。

bash

L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git status
On branch master
Untracked files:
  (use "git add ..." to include in what will be committed)
        hello.java  # 文件名都为红色
        local.txt
        test.java

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

可以看到有3个未被追踪的文件。

2)编辑Git仓库根目录下的 .git/info/exclude 文件,配置忽略。

bash

1.我们先来查看一下.git/info/exclude文件的默认内容
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ cat .git/info/exclude
git ls-files --others --exclude-from=.git/info/exclude
Lines that start with '#' are comments.

For a project mostly in C, the following would be a good set of
exclude patterns (uncomment them if you want to use them):
*.[oa]
*~

2.配置忽略文件,把local.txt文件进行忽略
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ echo "local.txt" >> .git/info/exclude

L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ cat .git/info/exclude
git ls-files --others --exclude-from=.git/info/exclude
Lines that start with '#' are comments.

For a project mostly in C, the following would be a good set of
exclude patterns (uncomment them if you want to use them):
*.[oa]
*~
local.txt

3)再次查看当前Git工作目录中文件状态。

bash

L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git status
On branch master
Untracked files:
  (use "git add ..." to include in what will be committed)
        hello.java
        test.java

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

我们可以看到 local.txt文件被忽略了。

忽略规则由本机所有仓库共用(也就是对本机的所有Git仓库都起作用),配置方法如下:

Linux中,这时把全局要忽略的文件列表 .gitignore 放当前用户根目录下( ~/.gitconfig ):

autoit

git config --global core.excludesfile '~/.gitignore'

Windows中,这时把全局要忽略的文件列表 .gitignore 放当前用户根目录下:

verilog

git config --global core.excludesfile "%USERPROFILE%\.gitignore(可自定义)"

我们以Windows系统为例:

1)查看当前Git工作目录中文件状态。

bash

L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git status
On branch master
Untracked files:
  (use "git add ..." to include in what will be committed)
        hello.java
        test.java

2)创建 .gitignore 忽略文件。

我们可以复制用户根目录下的 .gitconfig文件,并改名为 .gitignore文件。

注意:

  • .gitconfig文件可以改成其他名称,如 .gitignore_global,这样好区分工作目录中的 .gitignore忽略文件。
  • .gitconfig文件也可以使用 .txt格式的文本文件代替,例如 gitignore_global.txt

如下:

bash

复制文件,生成gitignore\_global.txt文件
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ cp /c/Users/L/.gitconfig /c/Users/L/gitignore\_global.txt

查看gitignore\_global.txt文件是否生成
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ ll /c/Users/L/.git*
-rw-r--r-- 1 L 197121 90  4月  5 20:52 /c/Users/L/.gitconfig
-rw-r--r-- 1 L 197121 90  4月  5 20:59 /c/Users/L/gitignore\_global.txt

清空gitignore\_global.txt文件中的内容
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ echo "" > /c/Users/L/gitignore\_global.txt

3)将忽略文件配置到Git全局配置文件 .gitconfig 文件中。

通过如下命令进行配置。

bash

git config --global core.excludesfile /c/Users/L/gitignore\_global.txt

执行命令后,查看 .gitconfig文件内容,如下:

bash

[user]
    name = sun\_wk
    email = sun\_wk@126.com
下面两行是生成的
[core]
    excludesfile = C:/Users/L/gitignore\_global.txt

提示:如果之前配置 global级别的配置签名, .gitconfig文件就已经被创建。

至此之后,所有Git仓库都会自动应用这个 gitignore_global.txt文件中,所配置忽略的文件。

当然本例子是存放于 当前用户的根目录下,你可以改成其他路径,但是不推荐。

4)把需要忽略的文件加入到 gitignore_global.txt 文件中。

gitignore_global.txt文件中的内容清空,之后添加要忽略的文件。

gitignore_global.txt文件内容如下:

bash

忽略所有.java文件,也可以单个文件写
*.java

5)查看 learngit 仓库工作目录文件状态。

bash

L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git status
On branch master
nothing to commit, working tree clean

可以看到 hello.javatest.java两个未被追踪的文件被忽略掉了。

提示:其实可以直接将 gitignore_global.txt文件中的内容直接写到 .gitconfig文件中,效果也一样的。只不过这样会比较混乱一点,还是推荐上面详细叙述的方法来设置Git全局忽略文件。

Original: https://blog.csdn.net/u013190417/article/details/124493395
Author: 虚幻私塾
Title: 『现学现忘』Git基础 — 19、在Git中进行忽略文件操作

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/747184/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球