1、首先我們通過git status來查看能被上傳的文件,如下圖所示:
我們查看到node_modules目錄默認是可以被上傳的,忽略方法有三個
1.修改 .gitignore 文件
我們需要在目錄總創建一個.gitignore文件,可以在目錄中右鍵選擇git bash Here,如下圖所示:
然后輸入touch .gitignore,如下圖所示:
此時就會在目錄中新建了一個.gitignore文件,如下圖所示
然后打開編輯器,將node_modules添加到文件中,如下圖所示
忽略規則
target //忽略這個target目錄 angular.json //忽略這個angular.json文件 log/* //忽略log下的所有文件 css/*.css //忽略css目錄下的.css文件
保存退出
然后在執行git status查看,就沒有node_modules目錄了,如下圖所示:
舉例:.gitignore文件內容如下:
# Android generated bin/ gen/ classes/ gen-external-apklibs/ # Ant local.properties # Maven target/ release.properties # Eclipse .classpath .project .externalToolBuilders/ .metadata .settings # IntelliJ *.iml *.ipr *.iws .idea/ out/ # Mac .DS_Store # gitignore .gitignore
2.使用命令
.gitignore只能忽略那些原來沒有被track的文件,如果某些文件已經被納入了版本管理中,則修改.gitignore是無效的。
正確的做法是在每個clone下來的倉庫中手動設置不要檢查特定文件的更改情況。
git update-index --assume-unchanged FILE 在FILE處輸入要忽略的文件。
如果要還原的話,使用命令:
git update-index --no-assume-unchanged FILE
3.使用.git/info/exclude
git 還提供了另一種 exclude 的方式來做同樣的事情,不同的是 .gitignore 這個文件本身會提交到版本庫中去。用來保存的是公共的需要排除的文件。而 .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] # *~ .gradle/ .idea/ .settings/ appcompat_v7/ bin/ build/ gen/ gradle/ out/ proguard/ ship/ target/ .classpath .gitignore .idea .project .readme .update-config *.iml local.properties
.gitignore 還有個有意思的小功能, 一個空的 .gitignore 文件 可以當作是一個 placeholder 。當你需要為項目創建一個空的 log 目錄時, 這就變的很有用。 你可以創建一個 log 目錄 在里面放置一個空的 .gitignore 文件。這樣當你 clone 這個 repo 的時候 git 會自動的創建好一個空的 log 目錄了。
文章參考:https://www.cnblogs.com/mafeng/p/7635228.html; https://blog.csdn.net/sunxiaoju/article/details/86495234