天天看點

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檔案

tmpfoo

:可以忽略/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/