我的 Git 偏好設定

. 讓 Command Line 指令列顯示目前處在哪一個 Git Branch 分支,最早是在 RGBA 看到這一招,非常方便。另外我最近看到一個點子是顯示從上一次 commit 之後過了多久時間,這可以提醒你是不是該 commit 了 XD

請修改家目錄的 ~/.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\]$ "
view raw gistfile1.txt hosted with ❤ by  GitHub

 

 

結果如下,各位可以看到目前處在 master 分支,並且這個專案已經過了 1821 個小時沒有 commit 了.... :p

2. 安裝 Git 的 Bash autocompletion,這樣按 tab 就會有自動完成的效果,它甚至包括 git checkout 時都可以抓到你的 branch 名稱。這裡我用 Homebrew 來安裝 bash-completion,這套件其實包括很多 autocompletion script,你可以去 /usr/local/etc/bash_completion.d 這個目錄找找看。

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. 打開 Git 的 color 顏色設定,這樣 Git 指令的輸出結果才會加上顏色,像是 git status 等:

git config --global color.ui true

4. 設定你偏好的文字編輯器和 diff 工具

git config --global core.editor
git config --global merge.tool opendiff

5. 最後,我個人喜歡以下的 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 設定檔的位置在 ~/.gitconfig,你也可以直接修改這個檔案。

<iframe id="I1_1393291131494" style="line-height: inherit; box-sizing: border-box; max-width: 100%; position: static; top: 0px; width: 90px; margin: 0px; border-style: none; left: 0px; visibility: visible; height: 20px;" title="+1" name="I1_1393291131494" src="https://apis.google.com/_/+1/fastbutton?usegapi=1&amp;bsv=o&amp;size=medium&amp;hl=en&amp;origin=http%3A%2F%2Fihower.tw&amp;url=http%3A%2F%2Fihower.tw%2Fblog%2Farchives%2F5436&amp;gsrc=3p&amp;jsh=m%3B%2F_%2Fscs%2Fapps-static%2F_%2Fjs%2Fk%3Doz.gapi.zh_CN.nIkyQSvaFLI.O%2Fm%3D__features__%2Fam%3DIQ%2Frt%3Dj%2Fd%3D1%2Ft%3Dzcms%2Frs%3DAItRSTNNF2S0ETvQLwi11103nlAaJ4ReHg#_methods=onPlusOne%2C_ready%2C_close%2C_open%2C_resizeMe%2C_renderstart%2Concircled%2Cdrefresh%2Cerefresh&amp;id=I1_1393291131494&amp;parent=http%3A%2F%2Fihower.tw&amp;pfname=&amp;rpctoken=28933760" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" width="100%" data-gapiattached="true"></iframe>

猜你喜欢

转载自huaonline.iteye.com/blog/2021455