本文参考于廖雪峰老师的博客Git教程。依照其博客进行学习和记录,感谢其无私分享,也欢迎各位查看原文。git
配置Git显示颜色git config --global color.ui true
github
.gitignore
配置忽略文件,且要提交到版本库web
git add -f <file>
,强制添加file
,git check-ignore
检查忽略规则数据库
在安装Git时,有对Git进行全局配置,用户名和邮箱(user.name
和user.email
) 也能够配置其余选项windows
$ git config --global color.ui true
复制代码
实际开发中,总有一些特殊文件不能提交。好比保存了数据库密码的配置文件等。编辑器
可是这些文件建立后,在git status
时会显示Untracked files ...
,并且不当心添加上后就会被提交。学习
.gitignore
忽略文件Git中,能够在Git工做区的根目录下,建立一个.gitignore
文件,写上须要忽略的文件名,Git就会自动忽略这些文件。测试
关于不一样语言和环境中推荐的忽略文件的配置,能够查看github
项目gitignore
:https://github.com/github/gitignore 中示例。ui
忽略操做系统自动生成的文件,好比缩略图等;url
忽略编译生成的中间文件、可执行文件等,也就是若是一个文件是经过另外一个文件自动生成的,那自动生成的文件就不必放进版本库,好比
Java
编译产生的.class
文件;忽略带有敏感信息的配置文件,好比存放口令的配置文件。
好比,忽略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.iniPython:
*.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
。**此限制和问题,已经在Win10最新版中不存在了。**其余版本和更具体的版本号未测试
.gitignore
文件查看和强制添加.gitignore
忽略后,便不能添加到git暂存区
$ 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. 复制代码
如Git提示,加上-f
参数,能够强制添加文件到Git
$ git add -f App.class
复制代码
git check-ignore
命令检查忽略规则,从而修改规则
$ git check-ignore -v App.class .gitignore:3:*.class App.class 复制代码
Git提示.gitignore
的第3行规则忽略了该文件。
本文使用 mdnice 排版