Git 歷史相關(guān)和 git config 高級進階
前一段時間分享了一篇《更好的 git log》簡要介紹怎么美化 git log 命令,其中提到了 alias
命令,今天再繼續(xù)談?wù)?git相關(guān), 看看如何通過配置自己的 git config 讓自己的 git 用起來更順手。
git log 進階
在一行顯示 git log
git log --pretty =oneline
在一行顯示 git log.
git log --oneline --stat
,顯示每個文件的變化行數(shù),其中后面跟的--start
參數(shù)是用來統(tǒng)計哪些文件被改動,有多少行被改動。
git log --oneline --graph
,則可以圖形化地顯示 branch 的變化(方便查看 merge 變化)。
查看不同時間段的 git log
git log --until=1.minute.ago // 一分鐘之前的所有 log
git log --since=1.day.ago //一天之內(nèi)的log
git log --since=1.hour.ago //一個小時之內(nèi)的 log
git log --since=`.month.ago --until=2.weeks.ago //一個月之前到半個月之前的log
git log --since ==2013-08.01 --until=2013-09-07 //某個時間段的 log
如果你想更加個性化地設(shè)置 git log 輸出的不同參數(shù)的顏色,可以使用如下命令
git log --pretty=format:"%h %ad- %s [%an]"
其中的參數(shù)對應(yīng)的是
%ad author data // 日期
%an author name // 作者名
%cn committer name //提交者姓名
%h SHA hash // hash值
%s subject //commit的描述
%d ref names //對應(yīng)的 branch 分支名
更多的選項可以使用git help log
來看看,如我在《更好的 git log》中提到的,你可以通過alias
來創(chuàng)建一個簡潔的git lg
命令來定制自己喜歡的輸出方案。
git diff 進階
git diff
是用來比較版本之間的差異變化 balbalbala 之類的,哪里修改了,哪里添加了 balbalbala
git diff HEAD //與上次 commit 之間的差別(爸爸)
git diff HEAD^ //與上上次(爺爺)
git diff HEAD^^ //與上上上次(曾祖父)
git diff HEAD~5 //與前面第5次commit(好吧…祖先吧)
git diff HEAD^..HEAD //中間是兩個點比較(爸爸)和(爺爺)的差別
git diff f5fdjsalfjdskaf..4fdklsajfdksaf //比較 兩個不同 hash 值記錄之間的不同
git diff master bird //比較 branch 之間的不同
git diff --since=1.week.ago --until=1.minute.ago //還可以根據(jù)時間來比較哦
git blame
如果你想要看看某一個文件的相關(guān)歷史記錄,可以使用git blame
命令。
如git blame index.html --data short

git config進階
config 就是配置的意思,git config 字如其意,就是 git 的配置文件,git config 文件有三層。
第一層config是在系統(tǒng)層etc/gitconfig
, 可以通過使用 git config --system
來進行配置,此層gitcofnig 配置針對系統(tǒng)所有用戶的分支都有效。
第二層是~/.gitconfig
,針對某個用戶有效,針對此層設(shè)置使用的是git config --global
命令。
第三層是項目文件夾中的配置文件,比如說我有一個 project1的文件夾,project1/.git/config
就是在這個 repo 中使用的 gitconfig 配置。直接使用git config
就能進行配置。
基礎(chǔ)配置
git config --help
,使用這個命令可以列出 git config 的幫助列表。
而使用git config --list
,則可以顯示你目前的 git config 配置。
用戶信息配置
git config --global user.name "will luo" //
git config --global user.emal "i@luolei.org" //
顏色設(shè)置
git config --global color.ui true //
git 在終端顯示會有顏色
color.*
如果你想針對不同的 git 命令輸出不同的配色,你可以使用 color.[command-type]
命令。
color.branch
color.diff
color.interactive
color.status
以上參數(shù)都能設(shè)置true
,false
,always
。
git config --global color.diff.meta "blue black bold"
這段代碼的意思就是git diff 輸出的「git 改變的信息(diff)」 以粗體、藍色前景、黑色背景的形式展現(xiàn)。
能設(shè)置的顏色值包括
normal, black, red, green, yellow, blue, magenta, cyan, white.
,字體屬性則有bold, dim, ul, blink, reverse
.
ALIASES
git config --global alias.mylog "log --pretty=format:'%h %ad- %s [%an]' --graph "
git config --global --add alias.lol "log --graph --decorate --pretty=oneline --abbrev-commit --all"
這樣就自定義git mylog
,和git lol
兩個命令,至于是什么樣的效果,大家就根據(jù)代碼的意思領(lǐng)悟和學(xué)習(xí)一下吧。
對于我們?nèi)粘3S玫?git 命令,我們可以通過
git config --global alias.st status //status 縮寫成 st
git config --global alias.co checkout //checkout 縮寫成 co
git config --global alias.br branch //branch 縮寫成 br
git config --global alias.ci commit //commit 縮寫成 ci
大家可以根據(jù)自己的喜好來縮寫,當(dāng)然,到時候別忘了備份自己的 config 文件(可參考我的博文dotfiles新手教程)。