資工問題雜筆

[資工雜筆] bash shell alias常用的git指令

因為每次從新增local repository 到commit和push上去到github上,都要經過很多指令,而且不一定背的起來,所以我就在我的.bashrc alias了一些自訂的function。


初始一個local repository:

ginit(){
 git init;
 git remote add origin "https://github.com/i314i/$1.git"
}

git add。"$#" -eq 0 代表指令後面沒有接任何參數。-eq是整數的比較。

gadd(){
 if [ "$#" -eq 0 ] ; then #if no filename then add all directory
     git add .
 else
     git add $1
 fi
}

git commit:

 alias gcommit='git commit -a'

git push。預設無參數push到origin master,也可以依照參數做更改。因為以前不常寫shell script,在寫if [ “$1″ == ‘-o’ ]這個的時候遇到一點小麻煩。每個東東,都要相隔一個空格,否則shell會認不出來。==也可以用=代替,不過習慣了,就用==。

 gpush(){
 if [ "$#" -eq 0 ];then #default push origin master
     git push origin master
 elif [ "$1" == '-o' ];then
     git push origin $2
 elif [ "$1" == '-m' ];then
     git push $2 master
 else
     git push $1 $2
 fi
}

git pull:

 gpull(){
 if [ "$#" -eq 0 ] ; then #default push origin master
     git pull origin master
 elif [ "$1" == '-o' ];then
     git pull origin $2
 elif [ "$1" == '-m' ];then
     git pull $2 master
 else
     git pull $1 $2
 fi
 }

add、commit、push懶人包:

 gquick(){
     gadd
     gcommit
     gpush
 }

git status:

 gstatus(){
 if [ "$#" -eq 0 ] ; then
     git status
 elif [ "$1" == '-s' ] || [ "$1" == '-S' ] || [ "$1" == 's' ] || [ "$1" == 'S' ]; then
     git status -s
 fi
 }

解除add的狀態

 gunstage(){
 git reset HEAD --
 }

查看log

 glast(){
 git log -1 HEAD
 }

寫這次的shell script寫了好久,以後要多多練習了。

參考資料:

http://www.suse.url.tw/sles10/lesson10.htm#15

http://linux.vbird.org/linux_basic/0340bashshell-scripts.php

我的.bashrc:

https://github.com/i314i/mybashrc

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

Google+ photo

You are commenting using your Google+ account. Log Out / 變更 )

連結到 %s