1 简介
在使用Git管理自己的代码版本时,由于编译生成的中间文件,Git使用SHA-1算法来对文件进行加密,进而得出来一个40位的十六进制加密字符串。
d8b1f67b5ddd37956a8a728fd26c4ba5ce
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 全恒 0 9月 27 09:44 ./ drwxr-xr-x 1 全恒 0 9月 27 09:45 ../ drwxr-xr-x 1 全恒 0 9月 27 09:38 .idea/ drwxr-xr-x 1 全恒 0 8月 29 23:52 .mvn/ -rw-r--r-- 1 全恒 8205 9月 18 17:08 demo.iml -rwxr-xr-x 1 全恒 6468 8月 22 09:03 mvnw* -rw-r--r-- 1 全恒 4994 8月 22 09:03 mvnw.cmd -rw-r--r-- 1 全恒 2707 9月 18 17:06 pom.xml drwxr-xr-x 1 全恒 0 8月 29 23:52 src/ drwxr-xr-x 1 全恒 0 8月 29 23:52 target/ -rw-r--r-- 1 全恒 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。拷贝远程仓库目录:
:yanchenmochen/demo.git
在demo目录执行命令如下:
全恒@Lenovo-PC MINGW64 /d/Git/demo/demo (master) $ git remote add origin :yanchenmochen/demo.git 全恒@Lenovo-PC MINGW64 /d/Git/demo/demo (master) $ git remote -v origin :yanchenmochen/demo.git (fetch) origin :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
..." 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
..." 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 .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 = :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 = :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 *
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文件
2.3 在Git项目的设置中指定排除文件
3 忽略规则
在 .gitignore 文件中,每一行的忽略规则的语法如下:
- 空格不匹配任意文件,可作为分隔符,可用反斜杠转义
- #开头的文件标识注释,可以使用反斜杠进行转义
- ! 开头的模式标识否定,该文件将会再次被包含,如果排除了该文件的父级目录,则使用 ! 也不会再次被包含。可以使用反斜杠进行转义
- / 结束的模式只匹配文件夹以及在该文件夹路径下的内容,但是不匹配该文件
- / 开始的模式匹配项目跟目录
- 如果一个模式不包含斜杠,则它匹配相对于当前 .gitignore 文件路径的内容,如果该模式不在 .gitignore 文件中,则相对于项目根目录
- 匹配多级目录,可在开始,中间,结束
- ? 通用匹配单个字符
- [] 通用匹配单个字符列表
4 引用
5 总结
Git在程序员开发过程中,不可或缺,因此熟练掌握Git的方方面面,对于提升自己的个人素养和开发效率,不可或缺。
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/212984.html原文链接:https://javaforall.net
