git設置忽略對臨時文件或文件夾的追蹤

問題描述:

在xcode開發app中,通過設置將DerivedData和build兩個文件夾顯示到了工程路徑下了

在每次經過改動和編譯後,這兩個文件夾裏邊的內容都會變動,git沒有任何設置情況下,會追蹤所有的變動,但這些追蹤沒什麼意義,他們是臨時生成的或經過編譯生成的

有沒有辦法讓  git  不追蹤這兩個文件夾?

 

解決:

  1. 在project創建並git init之後,在.git同級目錄下touch一個文件.gitignore
  2. GitHub已經爲我們準備了各種配置文件,根據自己的開發語言和平臺等,可單獨使用或組合使用
  3. 將編輯好的配置文件內容,寫入到.gitignore中,保存
  4. git add .gitignore,git commit -m 'add git ignore'

必須確保.gitignore是第一個被add的,是第一個被commit的。設置以後,git就不追蹤這些在.gitignore中過慮過的文檔了

.gitignore只能忽略那些原來沒有被track的文件,如果某些文件已經被納入了版本管理中,則修改.gitignore是無效的

解決方法就是先把本地緩存刪除(改變成未track狀態),然後再提交:

git rm -r --cached .
git add .
git commit -m "msg"

 

.gitignore語法:

  1. 所有空行或者以註釋符號 # 開頭的行都會被 Git 忽略
  2. 可以使用標準的 glob 模式匹配
  3. 匹配模式最後跟斜槓(/)說明要忽略的是目錄
  4. 要忽略指定模式以外的文件或目錄,可以在模式前加上感嘆號(!)進行取

glob 模式是指 shell 所使用的簡化了的正則表達式,簡單的匹配規則這這樣的:

  1. "*":星號匹配零個或多個任意字符
  2. []:匹配任何一個列在方括號中的字符,如[ab]匹配a或者匹配b
  3. "?":問號匹配一個任意字符
  4. [n-m]:匹配所有在這兩個字符範圍內的字符,如[0-9]表示匹配所有0到9的數字

示例:

build/:忽略當前路徑下的build目錄,包含build下的所有子目錄和文件
/logs.txt:忽略根目錄下的logs.txt文件
*.html:忽略所有後綴爲.html的文件
!/classes/a.txt:不忽略classes目錄下的a.txt文件
tmp/*.txt:只忽略tmp目錄下的.txt文件
**/foo:可以忽略/foo, a/foo, a/b/foo等

如果你打算把倉庫共享給其他人,但是又不想讓其他人忽略這個文件,或者得知這個文件的名字,可以寫到 .git/info/exclude 文件裏去。

xcode project中配置的.gitignore內容:

# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## User settings
xcuserdata/

## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
*.xcscmblueprint
*.xccheckout

## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4)
build/
DerivedData/
*.moved-aside
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3

## Obj-C/Swift specific
*.hmap

## App packaging
*.ipa
*.dSYM.zip
*.dSYM

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# Pods/
#
# Add this line if you want to avoid checking in source code from the Xcode workspace
# *.xcworkspace

# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build/

# fastlane
#
# It is recommended to not store the screenshots in the git repo.
# Instead, use fastlane to re-generate the screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/#source-control

fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots/**/*.png
fastlane/test_output

# Code Injection
#
# After new code Injection tools there's a generated folder /iOSInjectionProject
# https://github.com/johnno1962/injectionforxcode

iOSInjectionProject/

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章