1.修改 .gitignore 文件
在git中如果想忽略掉某個文件,不讓這個文件提交到版本庫中,可以使用修改 .gitignore 文件的方法。
舉例:.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 目錄了。