因為每次從新增local repository 到commit和push上去到github上,都要經過很多指令,而且不一定背的起來,所以我就在我的.bashrc alias了一些自訂的function。
初始一個local repository:
[code]
ginit(){
git init;
git remote add origin "https://github.com/i314i/$1.git"
}
[/code]
git add。”$#” -eq 0 代表指令後面沒有接任何參數。-eq是整數的比較。
[code]gadd(){
if [ "$#" -eq 0 ] ; then #if no filename then add all directory
git add .
else
git add $1
fi
}[/code]
git commit:
[code] alias gcommit=’git commit -a'[/code]
git push。預設無參數push到origin master,也可以依照參數做更改。因為以前不常寫shell script,在寫if [ “$1” == ‘-o’ ]這個的時候遇到一點小麻煩。每個東東,都要相隔一個空格,否則shell會認不出來。==也可以用=代替,不過習慣了,就用==。
[code] 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
}[/code]
git pull:
[code] 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
}[/code]
add、commit、push懶人包:
[code] gquick(){
gadd
gcommit
gpush
}[/code]
git status:
[code] gstatus(){
if [ "$#" -eq 0 ] ; then
git status
elif [ "$1" == ‘-s’ ] || [ "$1" == ‘-S’ ] || [ "$1" == ‘s’ ] || [ "$1" == ‘S’ ]; then
git status -s
fi
}[/code]
解除add的狀態
[code] gunstage(){
git reset HEAD —
}[/code]
查看log
[code] glast(){
git log -1 HEAD
}[/code]
寫這次的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
關於我:
我是沒一村,專長和興趣是程式、主動投資、科技商業模式。可以參考我的書單和比較熱門的文章: