【Git】git项目管理 忽略文件配置

git管理的时候我们并不需要把所有都推送到github上,比如切换的时候我就遇到显示错误
Your local changes to the following files would be overwritten by checkout,我的原因就是
project.xcworkspace/xcuserdata/username.xcuserdatad/userinterfacestate.xcuserstate,缓存文件路径,也被我推送的git上,切换分支的时候出发了缓存,git显示本地有更改,实际分支里面并没有添加新内容,这时我们需要对git配置文件.gitignore做过滤配置,过滤掉我们不需要上传的文件
1、进入到工程目录下,选择工程,shift+command+. 显示隐藏文件.gitignore文件,打开添加下面配置,直接复制粘贴就行,(如果没有.gitignore执行 touch .gitignore 创建,open .gitignore打开 )

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

## Build generated
build/
DerivedData/

## Various settings
*.xcuserstate
*.DS_Store
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata/

## Other
*.moved-aside
*.xccheckout
*.xcscmblueprint

## 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://docs.fastlane.tools/best-practices/source-control/#source-control

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/

2、 配置文件的一些规则
1)/mtk/ 过滤整个文件夹
2)*.zip 过滤所有.zip文件
3)/mtk/do.c 过滤某个具体文件

很简单吧,被过滤掉的文件就不会出现在git仓库中(gitlab或github)了,当然本地库中还有,只是push的时候不会上传。
需要注意的是,gitignore还可以指定要将哪些文件添加到版本管理中:
1)!*.zip
2)!/mtk/one.txt

唯一的区别就是规则开头多了一个感叹号,Git会将满足这类规则的文件添加到版本管理中。
为什么要有两种规则呢?想象一个场景:假如我们只需要管理/mtk/目录中的one.txt文件,这个目录中的其他文件都不需要管理,那么我们就需要使用:
1)/mtk/
2)!/mtk/one.txt
假设我们只有过滤规则,而没有添加规则,那么我们就需要把/mtk/目录下除了one.txt以外的所有文件都写出来!

最后需要强调的一点是,如果你不慎在创建.gitignore文件之前就push了项目,那么即使你在.gitignore文件中写入新的过滤规则,这些规则也不会起作用,Git仍然会对所有文件进行版本管理。
简单来说,出现这种问题的原因就是Git已经开始管理这些文件了,所以你无法再通过过滤规则过滤它们。因此一定要养成在项目开始就创建.gitignore文件的习惯,否则一旦push,处理起来会非常麻烦。

.gitignore配置文件用于配置不需要加入版本管理的文件,配置好该文件可以为版本管理带来很大的便利,以下是对于配置.gitignore的一些心得记录:
1)配置语法:
以斜杠“/”开头表示目录;
以星号“*”通配多个字符;
以问号“?”通配单个字符
以方括号“[]”包含单个字符的匹配列表;
以叹号“!”表示不忽略(跟踪)匹配到的文件或目录;

此外,git 对于 .ignore 配置文件是按行从上到下进行规则匹配的,意味着如果前面的规则匹配的范围更大,则后面的规则将不会生效;

2)示例说明
a)规则:fd1/*
说明:忽略目录 fd1 下的全部内容;注意,不管是根目录下的 /fd1/ 目录,还是某个子目录 /child/fd1/ 目录,都会被忽略;
b)规则:/fd1/*
说明:忽略根目录下的 /fd1/ 目录的全部内容;
c)规则:
/*
!.gitignore
!/fw/bin/
!/fw/sf/
说明:忽略全部内容,但是不忽略 .gitignore 文件、根目录下的 /fw/bin/ 和 /fw/sf/ 目录;
3、
如果你需要忽略的文件意境存在在远端中了,那么你需要将远端中的文件删除掉才可以:

使用 git rm -r --cached .DS_Store/ //进行删除
git rm –cached 把文件.DS_Store从git的索引库中移除,但是对文件.DS_Store本身并不进行任何操作也就是说本地还是有.DS_Store文件的,但是远端却没有了

之后再使用git commit /push //之后提交上去
这样就不会再用担心这个文件的冲突了

参考链接:https://www.cnblogs.com/YangJieCheng/p/8175761.html

你可能感兴趣的:(Git)