11 Git —— 自定义Git

11 Git —— 自定义Git

忽略特殊文件

有些时候,你必须把某些文件放到Git工做目录中,但又不能提交它们,好比保存了数据库密码的配置文件啦,等等,每次git status都会显示Untracked files ...,有强迫症的童鞋内心确定不爽。git

好在Git考虑到了你们的感觉,这个问题解决起来也很简单,在Git工做区的根目录下建立一个特殊的.gitignore文件,而后把要忽略的文件名填进去,Git就会自动忽略这些文件。github

不须要从头写.gitignore文件,GitHub已经为咱们准备了各类配置文件,只须要组合一下就可使用了。全部配置文件能够直接在线浏览:https://github.com/github/gitignore数据库

忽略文件的原则是:fetch

  1. 忽略操做系统自动生成的文件,好比缩略图等;
  2. 忽略编译生成的中间文件、可执行文件等,也就是若是一个文件是经过另外一个文件自动生成的,那自动生成的文件就不必放进版本库,好比Java编译产生的.class文件;
  3. 忽略你本身的带有敏感信息的配置文件,好比存放口令的配置文件。

配置别名

有没有常常敲错命令?好比git statusstatus这个单词真心很差记。ui

若是敲git st就表示git status那就简单多了,固然这种偷懒的办法咱们是极力同意的。url

咱们只须要敲一行命令,告诉Git,之后st就表示status操作系统

lwenhaodeMacBook-Pro:TestGit lwenhao$ git config --global alias.st status
lwenhaodeMacBook-Pro:TestGit lwenhao$ git st
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")
lwenhaodeMacBook-Pro:TestGit lwenhao$

--global参数是全局参数,也就是这些命令在这台电脑的全部Git仓库下都有用。code

配置文件

配置Git的时候,加上--global是针对当前用户起做用的,若是不加,那只针对当前的仓库起做用。orm

配置文件放哪了?每一个仓库的Git配置文件都放在.git/config文件中:unicode

lwenhaodeMacBook-Pro:TestGit lwenhao$ cat .git/config 
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
	ignorecase = true
	precomposeunicode = true
[remote "origin"]
	url = https://github.com/liuwenhaoCN/TestGit.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master
[branch "dev"]
	remote = origin
	merge = refs/heads/dev
lwenhaodeMacBook-Pro:TestGit lwenhao$

而当前用户的Git配置文件放在用户主目录下的一个隐藏文件.gitconfig中:

lwenhaodeMacBook-Pro:TestGit lwenhao$ cd ~
lwenhaodeMacBook-Pro:~ lwenhao$ cat .gitconfig 
[user]
	name = lwenhao
	email = my@lwenhao.com
[color]
	ui = true
[alias]
	st = status
	lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
lwenhaodeMacBook-Pro:~ lwenhao$

配置别名也能够直接修改这个文件,若是改错了,能够删掉文件从新经过命令配置。

相关文章
相关标签/搜索