有些時候,你必須把某些文件放到Git工做目錄中,但又不能提交它們,好比保存了數據庫密碼的配置文件啦,等等,每次git status都會顯示Untracked files ...,有強迫症的童鞋內心確定不爽. 好在Git考慮到了你們的感覺,這個問題解決起來也很簡單,在Git工做區的根目錄下建立一個特殊的.gitignore文件,而後把要忽略的文件名填進去,Git就會自動忽略這些文件. 不須要從頭寫.gitignore文件,GitHub已經爲咱們準備了各類配置文件,只須要組合一下就可使用了.全部配置文件能夠直接在線瀏覽: https://github.com/github/gitignore
忽略文件的原則是: 忽略操做系統自動生成的文件,好比縮略圖等: 忽略編譯生成的中間文件、可執行文件等,也就是若是一個文件是經過另外一個文件自動生成的,那自動生成的文件就不必放進版本庫,好比Java編譯產生的.class文件; 忽略你本身的帶有敏感信息的配置文件,好比存放口令的配置文件.
舉個例子: 假設你在Windows下進行Python開發,Windows會自動在有圖片的目錄下生成隱藏的縮略圖文件,若是有自定義目錄,目錄下就會有Desktop.ini文件,所以你須要忽略Windows自動生成的垃圾文件: # Windows: Thumbs.db ehthumbs.db Desktop.ini
而後,繼續忽略Python編譯產生的.pyc、.pyo、dist等文件或目錄: # Python: *.py[cod] *.so *.egg *.egg-info dist build
加上你本身定義的文件,最終獲得一個完整的.gitignore文件,內容以下: # Windows: Thumbs.db ehthumbs.db Desktop.ini # Python: *.py[cod] *.so *.egg *.egg-info dist build # My configurations: db.ini deploy_key_rsa
最後一步就是把.gitignore也提交到Git,就完成了!固然檢驗.gitignore的標準是git status命令是否是說working directory clean. 使用Windows的童鞋注意了,若是你在資源管理器裏新建一個.gitignore文件,它會很是弱智地提示你必須輸入文件名,可是在文本編輯器裏「保存」或者「另存爲」就能夠把文件保存爲.gitignore了。 有些時候,你想添加一個文件到Git,但發現添加不了,緣由是這個文件被.gitignore忽略了:
$ git add App.class The following paths are ignored by one of your .gitignore files: App.class Use -f if you really want to add them.
若是你確實想添加該文件,能夠用-f強制添加到Git: $ git add -f App.class
或者你發現,多是.gitignore寫得有問題,須要找出來到底哪一個規則寫錯了,能夠用git check-ignore命令檢查: $ git check-ignore -v App.class .gitignore:3:*.class App.class Git會告訴咱們,.gitignore的第3行規則忽略了該文件,因而咱們就能夠知道應該修訂哪一個規則.
小結 忽略某些文件時,須要編寫.gitignore; .gitignore文件自己要放到版本庫裏,而且能夠對.gitignore作版本管理! .gitignore只能忽略那些原來沒有被track的文件,若是某些文件已經被歸入了版本管理中,則修改.gitignore是無效的. 添加忽略以後,已經提交到版本庫中的文件是沒法忽略的.只能clone到本地,刪除後,再進行忽略. 正確的作法是在每一個clone下來的倉庫中手動設置不要檢查特定文件的更改狀況。 git update-index --assume-unchanged PATH 在PATH處輸入要忽略的文件。 另外git還提供了另外一種exclude的方式來作一樣的事情,不一樣的是.gitignore這個文件自己會提交到版本庫中去,用來保存的是公共的須要排除的文件.而.git/info/exclude這裏設置的則是你本身本地須要排除的文件.他不會影響到其餘人,也不會提交到版本庫中去. # 此爲註釋 – 將被 Git 忽略 *.a # 忽略全部 .a 結尾的文件 !lib.a # 但 lib.a 除外 /TODO # 僅僅忽略項目根目錄下的 TODO 文件,不包括 subdir/TODO build/ # 忽略 build/ 目錄下的全部文件 doc/*.txt # 會忽略 doc/notes.txt 但不包括 doc/server/arch.txt
# Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] *$py.class # C extensions *.so # Distribution / packaging .Python build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ wheels/ share/python-wheels/ *.egg-info/ .installed.cfg *.egg MANIFEST # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .nox/ .coverage .coverage.* .cache nosetests.xml coverage.xml *.cover .hypothesis/ .pytest_cache/ # Translations *.mo *.pot # Django stuff: *.log local_settings.py db.sqlite3 # Flask stuff: instance/ .webassets-cache # Scrapy stuff: .scrapy # Sphinx documentation docs/_build/ # PyBuilder target/ # Jupyter Notebook .ipynb_checkpoints # IPython profile_default/ ipython_config.py # pyenv .python-version # celery beat schedule file celerybeat-schedule # SageMath parsed files *.sage.py # Environments .env .venv env/ venv/ ENV/ env.bak/ venv.bak/ # Spyder project settings .spyderproject .spyproject # Rope project settings .ropeproject # mkdocs documentation /site # mypy .mypy_cache/ .dmypy.json dmypy.json # Pyre type checker .pyre/