1. 讓 Command Line 指令列顯示目前處在哪一個(gè) Git Branch 分支,最早是在 RGBA 看到這一招,非常方便。另外我最近看到一個(gè)點(diǎn)子是顯示從上一次 commit 之後過(guò)了多久時(shí)間,這可以提醒你是不是該 commit 了 XD
請(qǐng)修改家目錄的 ~/.bash_profile 檔案 (我是用 Bash)。
1234567891011121314151617 | function git_branch { ref=$(git symbolic-ref HEAD 2> /dev/null) || return; echo "("${ref#refs/heads/}") "; } function git_since_last_commit { now=`date +%s`; last_commit=$(git log --pretty=format:%at -1 2> /dev/null) || return; seconds_since_last_commit=$((now-last_commit)); minutes_since_last_commit=$((seconds_since_last_commit/60)); hours_since_last_commit=$((minutes_since_last_commit/60)); minutes_since_last_commit=$((minutes_since_last_commit%60)); echo "${hours_since_last_commit}h${minutes_since_last_commit}m "; } PS1="[\[\033[1;32m\]\w\[\033[0m\]] \[\033[0m\]\[\033[1;36m\]\$(git_branch)\[\033[0;33m\]\$(git_since_last_commit)\[\033[0m\]$ "
|
結(jié)果如下,各位可以看到目前處在 master 分支,並且這個(gè)專(zhuān)案已經(jīng)過(guò)了 1821 個(gè)小時(shí)沒(méi)有 commit 了…. :p

2. 安裝 Git 的 Bash autocompletion,這樣按 tab 就會(huì)有自動(dòng)完成的效果,它甚至包括 git checkout 時(shí)都可以抓到你的 branch 名稱(chēng)。這裡我用 Homebrew 來(lái)安裝 bash-completion,這套件其實(shí)包括很多 autocompletion script,你可以去 /usr/local/etc/bash_completion.d 這個(gè)目錄找找看。
brew install bash-completion cp /usr/local/etc/bash_completion.d/git-completion.bash ~/.git-bash-completion.sh
編輯 ~/.bash_profile 加入
[ -f ~/.git-bash-completion.sh ] && . ~/.git-bash-completion.sh
3. 打開(kāi) Git 的 color 顏色設(shè)定,這樣 Git 指令的輸出結(jié)果才會(huì)加上顏色,像是 git status 等:
git config --global color.ui true
4. 設(shè)定你偏好的文字編輯器和 diff 工具
git config --global core.editor git config --global merge.tool opendiff
5. 最後,我個(gè)人喜歡以下的 alias:
git config --global alias.co checkout git config --global alias.ci commit git config --global alias.st status git config --global alias.br branch
這樣只要輸入 git st 就是 git status 了。
FYI,以上 git 設(shè)定檔的位置在 ~/.gitconfig,你也可以直接修改這個(gè)檔案。