網站首頁 編程語言 正文
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 文件中,每一行的忽略規則的語法如下:
- 空格不匹配任意文件,可作為分隔符,可用反斜杠轉義
- #開頭的文件標識注釋,可以使用反斜杠進行轉義
- ! 開頭的模式標識否定,該文件將會再次被包含,如果排除了該文件的父級目錄,則使用 ! 也不會再次被包含。可以使用反斜杠進行轉義
- / 結束的模式只匹配文件夾以及在該文件夾路徑下的內容,但是不匹配該文件
- / 開始的模式匹配項目跟目錄
- 如果一個模式不包含斜杠,則它匹配相對于當前 .gitignore 文件路徑的內容,如果該模式不在 .gitignore 文件中,則相對于項目根目錄
- ** 匹配多級目錄,可在開始,中間,結束
- ? 通用匹配單個字符
- [] 通用匹配單個字符列表
4 總結
Git在程序員開發過程中,不可或缺,因此熟練掌握Git的方方面面,對于提升自己的個人素養和開發效率,不可或缺。
原文鏈接:https://blog.csdn.net/lk142500/article/details/82869018
相關推薦
- 2022-08-21 golang數組內存分配原理_Golang
- 2023-05-06 python?字典的概念敘述和使用方法_python
- 2022-04-07 Kotlin原理詳析之拓展函數_Android
- 2023-11-20 python設置matplotlib.plot的坐標刻度和坐標范圍
- 2022-12-04 C++?Boost?Graph算法超詳細精講_C 語言
- 2022-06-28 go?micro集成鏈路跟蹤的方法和中間件原理解析_Golang
- 2022-11-21 基于C++實現一個日期計算器_C 語言
- 2023-01-15 Python?pytest.main()運行測試用例_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支