Git中使用.gitignore忽略文件的推送 - Go语言中文社区

Git中使用.gitignore忽略文件的推送


1 简介

在使用Git管理自己的代码版本时,由于编译生成的中间文件,Git使用SHA-1算法来对文件进行加密,进而得出来一个40位的十六进制加密字符串。

325525d8b1f67b5ddd37956a8a728fd26c4ba5ce

但这种算法对于文本文件有效,对于二进制之类的文件则无法正常的进行加密。因此Git版本管理多管理文本文件,而非二进制之类的文件,例如obj文件、.class文件,,并且一些敏感文件和临时文件、日志文件是不能上传到Git远程仓库中的。在Git中提供了.gitignore文件,可以制定自己忽略文件。比如说使用IDEA集成开发环境编写一个项目,在项目根路径下,文件结构如下:
在这里插入图片描述
在上图中,由IDEA开发的项目的目录结构如上图所示,其中target目录存放的是项目编译产生的文件,而.idea目录则是特定于IDEA集成开发环境的文件。demo.iml文件也不需要上传到Git。

2 Git忽略文件提交方法

由于作者在撰写本文时使用IDEA开发,因此以忽略某些IDEA开发环境的特定文件做例子演示

2.1 在Git项目中定义 .gitignore 文件

2.1.1 初始化git仓库

首先打开Git Bash,并且切换到demo根目录,执行git init让git管理该目录。

$ ls -la
total 48
drwxr-xr-x 1 全恒 197609    0 9月  27 09:44 ./
drwxr-xr-x 1 全恒 197609    0 9月  27 09:45 ../
drwxr-xr-x 1 全恒 197609    0 9月  27 09:38 .idea/
drwxr-xr-x 1 全恒 197609    0 8月  29 23:52 .mvn/
-rw-r--r-- 1 全恒 197609 8205 9月  18 17:08 demo.iml
-rwxr-xr-x 1 全恒 197609 6468 8月  22 09:03 mvnw*
-rw-r--r-- 1 全恒 197609 4994 8月  22 09:03 mvnw.cmd
-rw-r--r-- 1 全恒 197609 2707 9月  18 17:06 pom.xml
drwxr-xr-x 1 全恒 197609    0 8月  29 23:52 src/
drwxr-xr-x 1 全恒 197609    0 8月  29 23:52 target/
-rw-r--r-- 1 全恒 197609 5162 8月  28 21:11 zioer5.iml

全恒@Lenovo-PC MINGW64 /d/Git/demo/demo
$ git init
Initialized empty Git repository in D:/Git/demo/demo/.git/

2.1.2 添加远端仓库路径

添加远端仓库,在GitHub上建立repository,demo。拷贝远程仓库目录:

git@github.com:yanchenmochen/demo.git

在demo目录执行命令如下:

全恒@Lenovo-PC MINGW64 /d/Git/demo/demo (master)
$ git remote add origin git@github.com:yanchenmochen/demo.git

全恒@Lenovo-PC MINGW64 /d/Git/demo/demo (master)
$ git remote -v
origin  git@github.com:yanchenmochen/demo.git (fetch)
origin  git@github.com:yanchenmochen/demo.git (push)

然后执行git add .,和执行git commit –m “first commit”,表示该项目的所有文件均被git管理。

2.1.3 新建.gitignore配置文件

在当前目录生成文件.gitignore,并在其中添加要忽略的文件或目录,每行表示一个忽略规则。

全恒@Lenovo-PC MINGW64 /d/Git/demo/demo (master)
$ vim .gitignore

全恒@Lenovo-PC MINGW64 /d/Git/demo/demo (master)
$ cat .git
.git/       .gitignore

全恒@Lenovo-PC MINGW64 /d/Git/demo/demo (master)
$ cat .gitignore
target/
*.iml
.idea/

2.1.4 git管理.gitignore

在上述的代码片段中新建了配置文件.gitignore,然后忽略了target目录,.idea目录,以后缀.iml结尾的文件。

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

        .gitignore

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

全恒@Lenovo-PC MINGW64 /d/Git/demo/demo (master)
$ git add .gitignore
warning: LF will be replaced by CRLF in .gitignore.
The file will have its original line endings in your working directory.

全恒@Lenovo-PC MINGW64 /d/Git/demo/demo (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   .gitignore


全恒@Lenovo-PC MINGW64 /d/Git/demo/demo (master)
$ git commit -m "[ADD]添加.gitignore配置文件"
[master 202e7b0] [ADD]添加.gitignore配置文件
 1 file changed, 3 insertions(+)
 create mode 100644 .gitignore

上述的代码片段让Git管理了文件.gitignore,并且执行了一次提交,提交到本地仓库。

2.1.5 让Git识别该配置文件

使用命令git config配置忽略配置文件.gitignore。

git config core. excludesfile .gitignore

与配置用户名和邮箱是一样的。

$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
[remote "origin"]
        url = git@github.com:yanchenmochen/demo.git
        fetch = +refs/heads/*:refs/remotes/origin/*


全恒@Lenovo-PC MINGW64 /d/Git/demo/demo (master)
$ git config core.excludesfile .gitignore

全恒@Lenovo-PC MINGW64 /d/Git/demo/demo (master)
$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
        excludesfile = .gitignore
[remote "origin"]
        url = git@github.com:yanchenmochen/demo.git
        fetch = +refs/heads/*:refs/remotes/origin/*

2.1.6 推送到远端

全恒@Lenovo-PC MINGW64 /d/Git/demo/demo (master)
$ git push origin master
Enumerating objects: 155, done.
Counting objects: 100% (155/155), done.
Delta compression using up to 8 threads.
Compressing objects: 100% (138/138), done.
Writing objects: 100% (155/155), 83.41 KiB | 749.00 KiB/s, done.
Total 155 (delta 69), reused 0 (delta 0)
remote: Resolving deltas: 100% (69/69), done.
remote:
remote: Create a pull request for 'master' on GitHub by visiting:
remote:      https://github.com/yanchenmochen/demo/pull/new/master
remote:
To github.com:yanchenmochen/demo.git
 * [new branch]      master -> master

2.1.7 网页查看上传的文件

在这里插入图片描述
在这里我们发现,.idea目录,target目录,demo.iml文件等我们想要忽略的文件。

2.1.8 .gitignore不生效

.gitignore只能忽略那些原来没有被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的。这是因为在之前,自己直接使用git add .把所有的文件,包括target目录,.idea目录,然后执行了

git config core.excludesfile ***

.gitignore只能忽略原来没有被跟踪的文件,因此跟踪过的文件是无法被忽略的。因此在网页上可以看到target等目录的存在。
解决方法就是先把本地缓存删除(改变成未track状态),然后再提交:

git rm -r --cached .
git add .
git commit -m ‘update .gitignore’

2.1.9 再次推送

$ git push origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 232 bytes | 232.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:yanchenmochen/demo.git
   202e7b0..9f4fc9c  master -> master

2.1.10 验证

登陆网页,查看本次提交:
在这里插入图片描述

2.2 定义Git全局的.gitignore文件

如果一直使用某个开发工具进行开发项目,则相对于特定项目的忽略文件,所有的项目均要忽略的文件,则可以使用配置全局忽略文件。
使用命令
git config --global core.excludesfile ~/.gitignore
该配置信息位于~/.gitignore。
整体的操作步骤与上述特定于项目的.gitignore是一致的,不再赘述。

2.3 在Git项目的设置中指定排除文件

这种方式只是临时指定该项目的行为,需要编辑当前项目下的 .git/info/exclude 文件,然后将需要忽略提交的文件写入其中。
需要注意的是,这种方式指定的忽略文件的根目录是项目根目录。

3 忽略规则

在 .gitignore 文件中,每一行的忽略规则的语法如下:

  1. 空格不匹配任意文件,可作为分隔符,可用反斜杠转义
  2. #开头的文件标识注释,可以使用反斜杠进行转义
  3. ! 开头的模式标识否定,该文件将会再次被包含,如果排除了该文件的父级目录,则使用 ! 也不会再次被包含。可以使用反斜杠进行转义
  4. / 结束的模式只匹配文件夹以及在该文件夹路径下的内容,但是不匹配该文件
  5. / 开始的模式匹配项目跟目录
  6. 如果一个模式不包含斜杠,则它匹配相对于当前 .gitignore 文件路径的内容,如果该模式不在 .gitignore 文件中,则相对于项目根目录
  7. ** 匹配多级目录,可在开始,中间,结束
  8. ? 通用匹配单个字符
  9. [] 通用匹配单个字符列表

4 引用

https://www.cnblogs.com/youyoui/p/8337147.html
https://download.csdn.net/download/lk142500/10692479

5 总结

Git在程序员开发过程中,不可或缺,因此熟练掌握Git的方方面面,对于提升自己的个人素养和开发效率,不可或缺。

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/lk142500/article/details/82869018
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢