Git中使用.gitignore忽略文件的推送方式
1 簡介
在使用Git管理自己的代碼版本時,由于編譯生成的中間文件,Git使用SHA-1算法來對文件進行加密,進而得出來一個40位的十六進制加密字符串。
325525d8b1f67b5ddd37956a8a728fd26c4ba5ce
但這種算法對于文本文件有效,對于二進制之類的文件則無法正常的進行加密。
因此Git版本管理多管理文本文件,而非二進制之類的文件,例如obj文件、.class文件,,并且一些敏感文件和臨時文件、日志文件是不能上傳到Git遠程倉庫中的。
在Git中提供了.gitignore文件,可以制定自己忽略文件。
比如說使用IDEA集成開發(fā)環(huán)境編寫一個項目,在項目根路徑下,文件結(jié)構(gòu)如下:
在上圖中,由IDEA開發(fā)的項目的目錄結(jié)構(gòu)如上圖所示,其中target目錄存放的是項目編譯產(chǎn)生的文件,而.idea目錄則是特定于IDEA集成開發(fā)環(huán)境的文件。
demo.iml文件也不需要上傳到Git。
2 Git忽略文件提交方法
由于作者在撰寫本文時使用IDEA開發(fā),因此以忽略某些IDEA開發(fā)環(huán)境的特定文件做例子演示
2.1 在Git項目中定義 .gitignore 文件
2.1.1 初始化git倉庫
首先打開Git Bash,并且切換到demo根目錄,執(zhí)行g(shù)it 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目錄執(zhí)行命令如下:
全恒@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)
然后執(zhí)行g(shù)it add .,和執(zhí)行g(shù)it commit –m “first commit”,表示該項目的所有文件均被git管理。
2.1.3 新建.gitignore配置文件
在當前目錄生成文件.gitignore,并在其中添加要忽略的文件或目錄,每行表示一個忽略規(guī)則。
全恒@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結(jié)尾的文件。
$ 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,并且執(zhí)行了一次提交,提交到本地倉庫。
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 網(wǎng)頁查看上傳的文件
在這里我們發(fā)現(xiàn),.idea目錄,target目錄,demo.iml文件等我們想要忽略的文件。
2.1.8 .gitignore不生效
.gitignore只能忽略那些原來沒有被track的文件,如果某些文件已經(jīng)被納入了版本管理中,則修改.gitignore是無效的。這是因為在之前,自己直接使用git add .把所有的文件,包括target目錄,.idea目錄,然后執(zhí)行了
git config core.excludesfile ***
.gitignore只能忽略原來沒有被跟蹤的文件,因此跟蹤過的文件是無法被忽略的。因此在網(wǎng)頁上可以看到target等目錄的存在。
解決方法就是先把本地緩存刪除(改變成未track狀態(tài)),然后再提交:
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 驗證
登陸網(wǎng)頁,查看本次提交:
2.2 定義Git全局的.gitignore文件
如果一直使用某個開發(fā)工具進行開發(fā)項目,則相對于特定項目的忽略文件,所有的項目均要忽略的文件,則可以使用配置全局忽略文件。
使用命令
git config --global core.excludesfile ~/.gitignore
該配置信息位于~/.gitignore。
整體的操作步驟與上述特定于項目的.gitignore是一致的,不再贅述。
2.3 在Git項目的設(shè)置中指定排除文件
這種方式只是臨時指定該項目的行為,需要編輯當前項目下的 .git/info/exclude 文件,然后將需要忽略提交的文件寫入其中。
需要注意的是,這種方式指定的忽略文件的根目錄是項目根目錄。
3 忽略規(guī)則
在 .gitignore 文件中,每一行的忽略規(guī)則的語法如下:
- 空格不匹配任意文件,可作為分隔符,可用反斜杠轉(zhuǎn)義
- #開頭的文件標識注釋,可以使用反斜杠進行轉(zhuǎn)義
- ! 開頭的模式標識否定,該文件將會再次被包含,如果排除了該文件的父級目錄,則使用 ! 也不會再次被包含??梢允褂梅葱备苓M行轉(zhuǎn)義
- / 結(jié)束的模式只匹配文件夾以及在該文件夾路徑下的內(nèi)容,但是不匹配該文件
- / 開始的模式匹配項目跟目錄
- 如果一個模式不包含斜杠,則它匹配相對于當前 .gitignore 文件路徑的內(nèi)容,如果該模式不在 .gitignore 文件中,則相對于項目根目錄
- ** 匹配多級目錄,可在開始,中間,結(jié)束
- ? 通用匹配單個字符
- [] 通用匹配單個字符列表
4 總結(jié)
Git在程序員開發(fā)過程中,不可或缺,因此熟練掌握Git的方方面面,對于提升自己的個人素養(yǎng)和開發(fā)效率,不可或缺。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Chrome瀏覽器中清除特定網(wǎng)站的Cookie數(shù)據(jù)三種方法
當我們在使用電腦瀏覽網(wǎng)頁時,服務(wù)器會生成一個證書并將其返回給電腦,這個證書是cookie,也可以稱為瀏覽器緩存,這篇文章主要給大家介紹了關(guān)于Chrome瀏覽器中清除特定網(wǎng)站的Cookie數(shù)據(jù)三種方法,需要的朋友可以參考下2023-10-10解決git配置錯誤ssh:connect to host github.com
學(xué)習(xí)git時,可能會碰到遠程倉庫連接問題,解決方法是確保.ssh目錄正確配置,首先,確認.ssh是隱藏文件夾,可通過ctrl+h顯示,然后,創(chuàng)建無后綴的config文件,并填寫正確的git郵箱賬號,最后,通過終端驗證配置是否成功,這些步驟有助于解決git遠程連接的常見問題2024-10-10網(wǎng)站性能提高實戰(zhàn)經(jīng)驗點滴記錄
網(wǎng)站性能提高實戰(zhàn)經(jīng)驗點滴記錄,需要的朋友可以參考下。2011-02-02vscode使用editorconfig插件以及.editorconfig配置文件說明詳解
這篇文章主要介紹了vscode使用editorconfig插件以及.editorconfig配置文件說明詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Idea 2019.3 本應(yīng)該搜索到的插件卻搜索不到的解決方法
這篇文章主要介紹了Idea 2019.3 本應(yīng)該搜索到的插件卻搜索不到,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06VsCode配置ssh免密遠程連接服務(wù)器的實現(xiàn)步驟
現(xiàn)在,可以在VSCode中直接通過SSH連接到服務(wù)器,而無需每次輸入密碼,本文主要介紹了VsCode配置ssh免密遠程連接服務(wù)器的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08