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$

配置別名也能夠直接修改這個文件,若是改錯了,能夠刪掉文件從新經過命令配置。

相關文章
相關標籤/搜索