.gitignore的用法
在日常的开发中,当我们需要将一个项目提交到Git时,并不是所有的文件都需要提交,比如一些自动生成的文件,这时候就可以使用.gitignore来忽略一些不需要提交的文件,本文着重介绍一下.gitignore的配置语法。
- 1、配置语法:
以斜杠“/”开头表示目录;
以星号“*”通配多个字符;
以问号“?”通配单个字符
以方括号“[]”包含单个字符的匹配列表;
以叹号“!”表示不忽略(跟踪)匹配到的文件或目录;
此外,git 对于 .ignore 配置文件是按行从上到下进行规则匹配的,意味着如果前面的规则匹配的范围更大,则后面的规则将不会生效;
2、 忽略文件的原则是:
忽略操作系统自动生成的文件,比如缩略图等;
忽略编译生成的中间文件、可执行文件等,也就是如果一个文件是通过另一个文件自动生成的,那自动生成的文件就没必要放进版本库,比如Java编译产生的.class文件;
忽略你自己的带有敏感信息的配置文件,比如存放口令的配置文件。
3、示例:
(1)规则:fd1/*
说明:忽略目录 fd1 下的全部内容;注意,不管是根目录下的 /fd1/ 目录,还是某个子目录 /child/fd1/ 目录,都会被忽略;
(2)规则:/fd1/*
说明:忽略根目录下的 /fd1/ 目录的全部内容;
(3)规则:
/*
!.gitignore
!/fw/bin/
!/fw/sf/
说明:忽略全部内容,但是不忽略 .gitignore 文件、根目录下的 /fw/bin/ 和 /fw/sf/ 目录;
- Xcode下简便配置.gitignore文件,配置内容可复制一下内容:
# Xcode
.DS_Store
*/build/*
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
profile
*.moved-aside
DerivedData
.idea/
*.hmap
*.xccheckout
*.xcworkspace
!default.xcworkspace
#CocoaPods
Pods
!Podfile
!Podfile.lock
有列子如下:
➜ Desktop cd yueba
➜ yueba git:(master) ✗ cd yueba
➜ yueba git:(master) ✗ git st
On branch master
Your branch is up-to-date with 'origin/master'.
1 # Xcode
2 #
3 # gitignore contributors: remember to update Global/Xcode.gitignore, Objecti ve-C.gitignore & Swift.gitignore
4
5 ## Build generated
6 build/ 忽略build下的所有文件
7 DerivedData/ 同上
8
9 ## Various settings
10 *.pbxuser 忽略所有以 .pbxuser 结尾的文件
11 !default.pbxuser 不忽略 default.pbxuser这个文件
12 *.mode1v3
13 !default.mode1v3 12/13行同10/11行
14 *.mode2v3
15 !default.mode2v3
16 *.perspectivev3
17 !default.perspectivev3
18 xcuserdata/
19
20 ## Other
21 *.moved-aside
22 *.xcuserstate
23 */project.xcworkspace/* 忽略路径中带有 project.xcworkspace 的所有文件夹下的文件
-- INSERT -- 1,1 Top
以下是github中.gitignore针对objective-c的内容,原链接 https://github.com/github/gitignore
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
## Build generated
build/
DerivedData/
## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata/
## Other
*.moved-aside
*.xcuserstate
## Obj-C/Swift specific
*.hmap
*.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/
# 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://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md
fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots
fastlane/test_output
# Code Injection
#
# After new code Injection tools there's a generated folder /iOSInjectionProject
# https://github.com/johnno1962/injectionforxcode
iOSInjectionProject/